终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

76 regels
2.3 KiB

  1. using BPASmartClient.Peripheral;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.Message;
  4. using BPASmartClient.EventBus;
  5. using BPASmartClient.Modbus;
  6. using static BPASmartClient.EventBus.EventBus;
  7. using BPASmartClient.Model;
  8. using BPASmartClient.Model.PLC;
  9. namespace BPASmartClient.PLC
  10. {
  11. public class PLCMachine : BasePeripheral
  12. {
  13. ModbusTcp modbusTcp = new ModbusTcp();
  14. public override void Init()
  15. {
  16. Task.Run(new Action(() => { modbusTcp.ModbusTcpConnect(communicationPar.IPAddress, communicationPar.IPPort); })); //PLC 设备连接
  17. ThreadManage.GetInstance().StartLong(new Action(() =>
  18. {
  19. IsConnected = modbusTcp.Connected;
  20. if (!IsConnected) IsWork = false;
  21. while (IsConnected)
  22. {
  23. IsWork = true;
  24. foreach (var par in variables)
  25. {
  26. if (par?.Address.Length > 0)
  27. {
  28. var res = modbusTcp.Read(par.Address, (ushort)par.ReadLeng);
  29. if (status.ContainsKey(par.Address))
  30. {
  31. status[par.Address] = res;
  32. }
  33. else
  34. {
  35. status.TryAdd(par.Address, res);
  36. }
  37. }
  38. }
  39. Thread.Sleep(500);
  40. }
  41. Thread.Sleep(1000);
  42. }), $"设备[{DeviceId}]PLC读取线程", true);
  43. //写入数据
  44. EventBus.EventBus.GetInstance().Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  45. {
  46. if (@event == null) return;
  47. var par = @event as WriteModel;
  48. modbusTcp.Write(par?.Address, par?.Value);
  49. });
  50. }
  51. public override void Start()
  52. {
  53. }
  54. public override void Stop()
  55. {
  56. }
  57. public override void WriteData(string address, object value)
  58. {
  59. if (address != null && value != null)
  60. modbusTcp.Write(address, value);
  61. }
  62. protected override void InitStatus()
  63. {
  64. }
  65. }
  66. }