终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

101 lignes
3.6 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. while (modbusTcp.Connected)
  25. {
  26. foreach (var par in plcReadParameters)
  27. {
  28. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  29. object readData = new object();
  30. switch (par.CmdType)
  31. {
  32. case CommandType.Coils:
  33. modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
  34. break;
  35. case CommandType.HoldingRegisters:
  36. readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
  37. break;
  38. }
  39. status[par.Address] = readData;
  40. }
  41. Thread.Sleep(500);
  42. }
  43. Thread.Sleep(1000);
  44. }), $"设备[{DeviceId}]PLC读取线程", true);
  45. //读取数据
  46. EventBus.EventBus.GetInstance().Subscribe<ReadModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  47. {
  48. if (@event == null) return;
  49. var par = @event as ReadModel;
  50. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  51. object readData = new object();
  52. if (par.Address.ToUpper().Contains("M"))
  53. {
  54. modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
  55. }
  56. else if (par.Address.ToUpper().Contains("VW"))
  57. {
  58. readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
  59. }
  60. callBack?.Invoke(readData);
  61. });
  62. //写入数据
  63. EventBus.EventBus.GetInstance().Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  64. {
  65. if (@event == null) return;
  66. var par = @event as WriteModel;
  67. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  68. if (par.Address.ToUpper().Contains("M"))
  69. {
  70. modbusTcp.Write(address, CommandType.Coils, par.Value);
  71. }
  72. else if (par.Address.ToUpper().Contains("VW"))
  73. {
  74. modbusTcp.Write(address, CommandType.HoldingRegisters, par.Value);
  75. }
  76. });
  77. }
  78. public override void Start()
  79. {
  80. }
  81. public override void Stop()
  82. {
  83. }
  84. protected override void InitStatus()
  85. {
  86. }
  87. }
  88. }