终端一体化运控平台
Não pode escolher mais do que 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.
 
 
 

79 linhas
2.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.MORKSM.BK.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 override void Init()
  17. {
  18. //读取数据
  19. EventBus.EventBus.GetInstance().Subscribe<ReadModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  20. {
  21. if (@event == null) return;
  22. var par = @event as ReadModel;
  23. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  24. object readData = new object();
  25. if (par.Address.ToUpper().Contains("M"))
  26. {
  27. //modbusTcp.Read(address, CommandType.Coils, par.Length);
  28. modbusTcp.Readbool(address, par.Length, new Action<bool[]>((s) => { readData = s; }));
  29. }
  30. else if (par.Address.ToUpper().Contains("VW"))
  31. {
  32. readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
  33. }
  34. callBack?.Invoke(readData);
  35. //callBack = new EventCallBackHandle(readData);
  36. });
  37. //写入数据
  38. EventBus.EventBus.GetInstance().Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  39. {
  40. if (@event == null) return;
  41. var par = @event as WriteModel;
  42. ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
  43. if (par.Address.ToUpper().Contains("M"))
  44. {
  45. modbusTcp.Write(address, CommandType.Coils, par.Value);
  46. }
  47. else if (par.Address.ToUpper().Contains("VW"))
  48. {
  49. modbusTcp.Write(address, CommandType.HoldingRegisters, par.Value);
  50. }
  51. });
  52. ////批量读取数据
  53. //EventBus.EventBus.GetInstance().Subscribe<>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  54. //{
  55. //});
  56. }
  57. public override void Start()
  58. {
  59. modbusTcp.ModbusTcpConnect(IpAddress, Port);
  60. }
  61. public override void Stop()
  62. {
  63. }
  64. protected override void InitStatus()
  65. {
  66. }
  67. }
  68. }