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

DeviceOperate.cs 6.0 KiB

2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
2 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 
  2. using BPASmartClient.CustomResource.Pages.Model;
  3. using BPASmartClient.Helper;
  4. using BPASmartClient.Message;
  5. using BPASmartClient.Modbus;
  6. using FryPot_DosingSystem.Model;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace FryPot_DosingSystem.Control
  16. {
  17. internal class DeviceOperate
  18. {
  19. private static DeviceOperate _instance;
  20. public static DeviceOperate GetInstance => _instance ??= new DeviceOperate();
  21. public bool IsConfig { get; set; }//设备plc数据是否配置
  22. ModbusTcp modbus = new ModbusTcp();
  23. private string Ip { get; set; }
  24. private string Port { get; set; }
  25. public bool Connected { get; set; }
  26. private string DeviceName { get; set; }
  27. public ConcurrentDictionary<string, object> Data { get; set; } = new ConcurrentDictionary<string, object>();
  28. public ObservableCollection<PlcVariableModel> Variables { get; set; } = new ObservableCollection<PlcVariableModel>();
  29. public DeviceOperate()
  30. {
  31. Init();
  32. Connect();
  33. ReadData();
  34. }
  35. public void Init()
  36. {
  37. if (Variables.Count > 0)
  38. {
  39. Variables.Clear();
  40. }
  41. Json<PlcVariableInfoManage>.Read();
  42. if (Json<PlcVariableInfoManage>.Data.VariablesInfo.Count > 0)
  43. {
  44. try
  45. {
  46. foreach (var item in Json<PlcVariableInfoManage>.Data.VariablesInfo)
  47. {
  48. Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  49. }
  50. IsConfig = true;
  51. }
  52. catch (Exception)
  53. {
  54. IsConfig = false;
  55. //throw;
  56. }
  57. }
  58. else
  59. {
  60. IsConfig= false;
  61. }
  62. //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号
  63. //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号
  64. //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号
  65. //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态
  66. //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行
  67. //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号
  68. //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号
  69. //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV
  70. //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV
  71. //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行
  72. //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号
  73. //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号
  74. //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV
  75. //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号
  76. //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号
  77. }
  78. public void Connect()
  79. {
  80. if (IsConfig)
  81. {
  82. Json<DeviceManage>.Read();
  83. DeviceManage devices = Json<DeviceManage>.Data;
  84. if (devices != null)
  85. {
  86. if (devices.Devices.Count > 0)
  87. {
  88. Ip = devices.Devices[0].Ip;
  89. Port = devices.Devices[0].Port;
  90. DeviceName = devices.Devices[0].DeviceName;
  91. Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); });
  92. // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.RunLog("PLC连接成功"); })); });
  93. }
  94. }
  95. }
  96. }
  97. public void ReadData()
  98. {
  99. if (IsConfig)
  100. {
  101. ThreadManage.GetInstance().StartLong(new Action(() =>
  102. {
  103. Connected = modbus.Connected;
  104. while (Connected)
  105. {
  106. foreach (var item in Variables)
  107. {
  108. var res = modbus.Read(item.Address, item.Length);//读取plc数据
  109. if (Data.ContainsKey(item.Address))
  110. {
  111. Data[item.Address] = res;
  112. }
  113. else
  114. {
  115. Data.TryAdd(item.Address, res);
  116. }
  117. }
  118. Thread.Sleep(500);
  119. }
  120. Thread.Sleep(1000);
  121. }), $"设备【{DeviceName}】PLC实时数据读取线程");
  122. }
  123. }
  124. public void WritePlcData(string address, ushort value)
  125. {
  126. lock (this)
  127. {
  128. modbus.Write(address, value);
  129. }
  130. }
  131. public ConcurrentDictionary<string, object> GetAllData()
  132. {
  133. return Data;
  134. }
  135. }
  136. }