终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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