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

86 lines
2.7 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. Thread.Sleep(100);
  44. }
  45. Thread.Sleep(1000);
  46. }), $"设备[{DeviceId}][{modbusTcp.IPAdress}]PLC读取线程", true);
  47. });
  48. modbusTcp.ModbusTcpConnect(communicationPar.IPAddress, communicationPar.IPPort);
  49. })); //PLC 设备连接
  50. Thread.Sleep(1000);
  51. //写入数据
  52. EventBus.EventBus.GetInstance().Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  53. {
  54. if (@event == null) return;
  55. var par = @event as WriteModel;
  56. modbusTcp.Write(par?.Address, par?.Value);
  57. });
  58. }
  59. public override void Start()
  60. {
  61. }
  62. public override void Stop()
  63. {
  64. }
  65. public override void WriteData(string address, object value)
  66. {
  67. if (address != null && value != null)
  68. modbusTcp.Write(address, value);
  69. }
  70. protected override void InitStatus()
  71. {
  72. }
  73. }
  74. }