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

78 linhas
2.4 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. Task.Run(new Action(() => { modbusTcp.ModbusTcpConnect(communicationPar.IPAddress, communicationPar.IPPort); })); //PLC 设备连接
  17. Thread.Sleep(1000);
  18. ThreadManage.GetInstance().StartLong(new Action(() =>
  19. {
  20. IsConnected = modbusTcp.Connected;
  21. if (!IsConnected) IsWork = false;
  22. while (IsConnected)
  23. {
  24. IsWork = true;
  25. foreach (var par in variables)
  26. {
  27. if (par?.Address.Length > 0)
  28. {
  29. var res = modbusTcp.Read(par.Address, (ushort)par.ReadLeng);
  30. if (status.ContainsKey(par.Address))
  31. {
  32. status[par.Address] = res;
  33. }
  34. else
  35. {
  36. status.TryAdd(par.Address, res);
  37. }
  38. }
  39. }
  40. Thread.Sleep(500);
  41. }
  42. Thread.Sleep(1000);
  43. }), $"设备[{DeviceId}][{modbusTcp.IPAdress}]PLC读取线程", true);
  44. //写入数据
  45. EventBus.EventBus.GetInstance().Subscribe<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  46. {
  47. if (@event == null) return;
  48. var par = @event as WriteModel;
  49. modbusTcp.Write(par?.Address, par?.Value);
  50. });
  51. }
  52. public override void Start()
  53. {
  54. }
  55. public override void Stop()
  56. {
  57. }
  58. public override void WriteData(string address, object value)
  59. {
  60. if (address != null && value != null)
  61. modbusTcp.Write(address, value);
  62. }
  63. protected override void InitStatus()
  64. {
  65. }
  66. }
  67. }