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

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