终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

105 linhas
3.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 MorksMachine : BasePeripheral
  12. {
  13. ModbusTcp modbusTcp = new ModbusTcp();
  14. public string IpAddress { get; set; }
  15. public int Port { get; set; }
  16. public string PLCReadAddress { get; set; }
  17. private List<PLCReadParameter> plcReadParameters;
  18. public override void Init()
  19. {
  20. plcReadParameters = PLCReadParameter.DeSerialize(PLCReadAddress);
  21. Task.Run(new Action(() => { modbusTcp.ModbusTcpConnect(IpAddress, Port); })); //PLC 设备连接
  22. ThreadManage.GetInstance().StartLong(new Action(() =>
  23. {
  24. IsConnected = modbusTcp.Connected;
  25. if (!IsConnected) IsWork = false;
  26. while (IsConnected)
  27. {
  28. IsWork = true;
  29. foreach (var par in plcReadParameters)
  30. {
  31. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  32. object readData = new object();
  33. switch (par.CmdType)
  34. {
  35. case CommandType.Coils:
  36. modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
  37. break;
  38. case CommandType.HoldingRegisters:
  39. readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
  40. break;
  41. }
  42. status[par.Address] = readData;
  43. }
  44. Thread.Sleep(500);
  45. }
  46. Thread.Sleep(1000);
  47. }), $"设备[{DeviceId}]PLC读取线程", true);
  48. //读取数据
  49. EventBus.EventBus.GetInstance().Subscribe<ReadModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  50. {
  51. if (@event == null) return;
  52. var par = @event as ReadModel;
  53. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  54. object readData = new object();
  55. if (par.Address.ToUpper().Contains("M"))
  56. {
  57. modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
  58. }
  59. else if (par.Address.ToUpper().Contains("VW"))
  60. {
  61. readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
  62. }
  63. callBack?.Invoke(readData);
  64. });
  65. //写入数据
  66. EventBus.EventBus.GetInstance().Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  67. {
  68. if (@event == null) return;
  69. var par = @event as WriteModel;
  70. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  71. if (par.Address.ToUpper().Contains("M"))
  72. {
  73. modbusTcp.Write(address, CommandType.Coils, par.Value);
  74. }
  75. else if (par.Address.ToUpper().Contains("VW"))
  76. {
  77. modbusTcp.Write(address, CommandType.HoldingRegisters, par.Value);
  78. }
  79. });
  80. }
  81. public override void Start()
  82. {
  83. }
  84. public override void Stop()
  85. {
  86. }
  87. protected override void InitStatus()
  88. {
  89. }
  90. }
  91. }