终端一体化运控平台
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.
 
 
 

125 line
4.1 KiB

  1. using BPASmartClient.Peripheral;
  2. using BPA.Helper;
  3. using BPASmartClient.Modbus;
  4. using BPASmartClient.Model;
  5. using BPASmartClient.Model.PLC;
  6. using static BPA.Helper.EventBus;
  7. namespace BPASmartClient.PLC
  8. {
  9. public class PLCMachine : BasePeripheral
  10. {
  11. ModbusTcp modbusTcp = new ModbusTcp();
  12. public override void Init()
  13. {
  14. modbusTcp.IsReconnect = true;
  15. modbusTcp.Show += new Action<string>((s) =>
  16. {
  17. if (s != null) MessageLog.GetInstance.Show(s);
  18. });
  19. modbusTcp.ShowEx += new Action<string>((s) =>
  20. {
  21. if (s != null) MessageLog.GetInstance.ShowEx(s);
  22. });
  23. Task.Run(new Action(() =>
  24. {
  25. modbusTcp.ConnectOk = new Action(() =>
  26. {
  27. TaskManage.GetInstance.StartLong(new Action(() =>
  28. {
  29. IsConnected = modbusTcp.Connected;
  30. if (!IsConnected) IsWork = false;
  31. while (IsConnected)
  32. {
  33. IsWork = true;
  34. foreach (var par in variables)
  35. {
  36. if (par?.Address.Length > 0)
  37. {
  38. var res = modbusTcp.Read(par.Address, (ushort)par.ReadLeng);
  39. if (status.ContainsKey(par.Address))
  40. {
  41. status[par.Address] = res;
  42. }
  43. else
  44. {
  45. status.TryAdd(par.Address, res);
  46. }
  47. }
  48. }
  49. //foreach (var par in tempVar)
  50. //{
  51. // if (par.Value?.Address.Length > 0)
  52. // {
  53. // var res = modbusTcp.Read(par.Value.Address, (ushort)par.Value.ReadLeng);
  54. // if (status.ContainsKey(par.Value.Address))
  55. // {
  56. // status[par.Value.Address] = res;
  57. // }
  58. // else
  59. // {
  60. // status.TryAdd(par.Value.Address, res);
  61. // }
  62. // }
  63. //}
  64. Thread.Sleep(100);
  65. }
  66. Thread.Sleep(1000);
  67. }), $"设备[{DeviceId}][{modbusTcp.IPAdress}]PLC读取线程", true);
  68. });
  69. modbusTcp.ModbusTcpConnect(communicationPar.IPAddress, communicationPar.IPPort);
  70. })); //PLC 设备连接
  71. Thread.Sleep(1000);
  72. //写入数据
  73. EventBus.GetInstance.Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  74. {
  75. if (@event == null) return;
  76. var par = @event as WriteModel;
  77. modbusTcp.Write(par?.Address, par?.Value);
  78. });
  79. }
  80. public override void Start()
  81. {
  82. }
  83. public override void Stop()
  84. {
  85. }
  86. public override void WriteData(string address, object value)
  87. {
  88. if (address != null && value != null)
  89. modbusTcp.Write(address, value);
  90. }
  91. //public override void AddVarInfo(string add, int len)
  92. //{
  93. // if (!tempVar.ContainsKey(add) && !string.IsNullOrEmpty(add) && len > 0)
  94. // {
  95. // tempVar.TryAdd(add, new Variable()
  96. // {
  97. // Address = add,
  98. // ReadLeng = len
  99. // });
  100. // }
  101. //}
  102. protected override void InitStatus()
  103. {
  104. }
  105. }
  106. }