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

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