终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

341 righe
17 KiB

  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.Message;
  4. using BPASmartClient.Modbus;
  5. using FryPot_DosingSystem.Model;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace FryPot_DosingSystem.Control
  15. {
  16. internal class DeviceOperate
  17. {
  18. private static DeviceOperate _instance;
  19. public static DeviceOperate GetInstance => _instance ??= new DeviceOperate();
  20. public bool IsConfig { get; set; }//设备plc数据是否配置
  21. ModbusTcp modbus = new ModbusTcp();//滚筒线Modbus通讯对象
  22. ModbusTcp fryOneModbus = new ModbusTcp();//炒锅1Modbus通讯对象
  23. ModbusTcp fryTwoModbus = new ModbusTcp();//炒锅2Modbus通讯对象
  24. ModbusTcp fryThreeModbus = new ModbusTcp();//炒锅3Modbus通讯对象
  25. ModbusTcp fryFourModbus = new ModbusTcp();//炒锅4Modbus通讯对象
  26. ModbusTcp fryFiveModbus = new ModbusTcp();//炒锅5Modbus通讯对象
  27. // private string Ip { get; set; }
  28. // private string Port { get; set; }
  29. public bool Connected { get; set; }
  30. public bool FryOneConnected { get; set; }
  31. public bool FryTwoConnected { get; set; }
  32. public bool FryThreeConnected { get; set; }
  33. public bool FryFourConnected { get; set; }
  34. public bool FryFiveConnected { get; set; }
  35. // private string DeviceName { get; set; }
  36. public ConcurrentDictionary<string, object> Data { get; set; } = new ConcurrentDictionary<string, object>();
  37. public ConcurrentDictionary<string, object> FryOneData { get; set; } = new ConcurrentDictionary<string, object>();
  38. public ConcurrentDictionary<string, object> FryTwoData { get; set; } = new ConcurrentDictionary<string, object>();
  39. public ConcurrentDictionary<string, object> FryThreeData { get; set; } = new ConcurrentDictionary<string, object>();
  40. public ConcurrentDictionary<string, object> FryFourData { get; set; } = new ConcurrentDictionary<string, object>();
  41. public ConcurrentDictionary<string, object> FryFiveData { get; set; } = new ConcurrentDictionary<string, object>();
  42. public ObservableCollection<PlcVariableModel> Variables { get; set; } = new ObservableCollection<PlcVariableModel>();
  43. public ObservableCollection<PlcVariableModel> FryOneVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  44. public ObservableCollection<PlcVariableModel> FryTwoVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  45. public ObservableCollection<PlcVariableModel> FryThreeVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  46. public ObservableCollection<PlcVariableModel> FryFourVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  47. public ObservableCollection<PlcVariableModel> FryFiveVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  48. public DeviceOperate()
  49. {
  50. Init();
  51. Connect();
  52. ReadData();
  53. }
  54. public void Init()
  55. {
  56. Variables.Clear();
  57. FryOneVariables.Clear();
  58. FryTwoVariables.Clear();
  59. FryThreeVariables.Clear();
  60. FryFourVariables.Clear();
  61. FryFiveVariables.Clear();
  62. Json<PlcVariableInfoManage>.Read();
  63. if (Json<PlcVariableInfoManage>.Data.VariablesInfo.Count > 0)
  64. {
  65. try
  66. {
  67. if (Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒输送线"].Count>0)
  68. {
  69. //foreach (var item in Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒运输线"])
  70. //{
  71. // Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  72. //}
  73. foreach (KeyValuePair<string,ObservableCollection<PlcVariableInfoModel>> dic in Json<PlcVariableInfoManage>.Data.VariablesInfo)
  74. {
  75. if (string.Equals(dic.Key, "滚筒输送线"))
  76. {
  77. foreach (var item in dic.Value)
  78. {
  79. Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  80. }
  81. }
  82. if (string.Equals(dic.Key, "炒锅1"))
  83. {
  84. foreach (var item in dic.Value)
  85. {
  86. FryOneVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  87. }
  88. }
  89. if (string.Equals(dic.Key, "炒锅2"))
  90. {
  91. foreach (var item in dic.Value)
  92. {
  93. FryTwoVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  94. }
  95. }
  96. if (string.Equals(dic.Key, "炒锅3"))
  97. {
  98. foreach (var item in dic.Value)
  99. {
  100. FryThreeVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  101. }
  102. }
  103. if (string.Equals(dic.Key, "炒锅4"))
  104. {
  105. foreach (var item in dic.Value)
  106. {
  107. FryFourVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  108. }
  109. }
  110. if (string.Equals(dic.Key, "炒锅5"))
  111. {
  112. foreach (var item in dic.Value)
  113. {
  114. FryFiveVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  115. }
  116. }
  117. }
  118. }
  119. IsConfig = true;
  120. }
  121. catch (Exception)
  122. {
  123. IsConfig = false;
  124. //throw;
  125. }
  126. }
  127. else
  128. {
  129. IsConfig = false;
  130. }
  131. //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号
  132. //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号
  133. //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号
  134. //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态
  135. //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行
  136. //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号
  137. //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号
  138. //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV
  139. //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV
  140. //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行
  141. //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号
  142. //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号
  143. //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV
  144. //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号
  145. //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号
  146. }
  147. public void Connect()
  148. {
  149. if (IsConfig)
  150. {
  151. Json<DeviceManage>.Read();
  152. DeviceManage devices = Json<DeviceManage>.Data;
  153. if (devices != null)
  154. {
  155. if (devices.Devices.Count > 0)
  156. {
  157. for (int i = 0; i < devices.Devices.Count; i++)
  158. {
  159. string Ip = devices.Devices[i].Ip;
  160. string Port = devices.Devices[i].Port;
  161. string DeviceName = devices.Devices[i].DeviceName;
  162. switch (DeviceName)
  163. {
  164. case "滚筒输送线": Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  165. case "炒锅1": Task.Run(() => { fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  166. case "炒锅2": Task.Run(() => { fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  167. case "炒锅3": Task.Run(() => { fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  168. case "炒锅4": Task.Run(() => { fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  169. case "炒锅5": Task.Run(() => { fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  170. }
  171. }
  172. // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.RunLog("PLC连接成功"); })); });
  173. }
  174. }
  175. }
  176. }
  177. public void ReadData()
  178. {
  179. if (IsConfig)
  180. {
  181. ThreadManage.GetInstance().StartLong(new Action(() =>
  182. {
  183. //滚筒线
  184. Connected = modbus.Connected;
  185. while (Connected)
  186. {
  187. foreach (var item in Variables)
  188. {
  189. var res = modbus.Read(item.Address, item.Length);//读取plc数据
  190. if (Data.ContainsKey(item.Address))
  191. {
  192. Data[item.Address] = res;
  193. }
  194. else
  195. {
  196. Data.TryAdd(item.Address, res);
  197. }
  198. }
  199. Thread.Sleep(50);
  200. }
  201. //炒锅1
  202. FryOneConnected = fryOneModbus.Connected;
  203. while (FryOneConnected)
  204. {
  205. foreach (var item in FryOneVariables)
  206. {
  207. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  208. if (FryOneData.ContainsKey(item.Address))
  209. {
  210. FryOneData[item.Address] = res;
  211. }
  212. else
  213. {
  214. FryOneData.TryAdd(item.Address, res);
  215. }
  216. }
  217. Thread.Sleep(50);
  218. }
  219. //炒锅2
  220. FryTwoConnected = fryTwoModbus.Connected;
  221. while (FryTwoConnected)
  222. {
  223. foreach (var item in FryTwoVariables)
  224. {
  225. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  226. if (FryTwoData.ContainsKey(item.Address))
  227. {
  228. FryTwoData[item.Address] = res;
  229. }
  230. else
  231. {
  232. FryTwoData.TryAdd(item.Address, res);
  233. }
  234. }
  235. Thread.Sleep(50);
  236. }
  237. //炒锅3
  238. FryThreeConnected = fryThreeModbus.Connected;
  239. while (FryThreeConnected)
  240. {
  241. foreach (var item in FryThreeVariables)
  242. {
  243. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  244. if (FryThreeData.ContainsKey(item.Address))
  245. {
  246. FryThreeData[item.Address] = res;
  247. }
  248. else
  249. {
  250. FryThreeData.TryAdd(item.Address, res);
  251. }
  252. }
  253. Thread.Sleep(50);
  254. }
  255. //炒锅4
  256. FryFourConnected = fryFourModbus.Connected;
  257. while (FryFourConnected)
  258. {
  259. foreach (var item in FryFourVariables)
  260. {
  261. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  262. if (FryFourData.ContainsKey(item.Address))
  263. {
  264. FryFourData[item.Address] = res;
  265. }
  266. else
  267. {
  268. FryFourData.TryAdd(item.Address, res);
  269. }
  270. }
  271. Thread.Sleep(50);
  272. }
  273. //炒锅5
  274. FryFiveConnected = fryFiveModbus.Connected;
  275. while (FryFiveConnected)
  276. {
  277. foreach (var item in FryFiveVariables)
  278. {
  279. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  280. if (FryFiveData.ContainsKey(item.Address))
  281. {
  282. FryFiveData[item.Address] = res;
  283. }
  284. else
  285. {
  286. FryFiveData.TryAdd(item.Address, res);
  287. }
  288. }
  289. Thread.Sleep(50);
  290. }
  291. Thread.Sleep(500);
  292. }), $"PLC实时数据读取线程");
  293. }
  294. }
  295. public void WritePlcData(string address, ushort value)
  296. {
  297. lock (this)
  298. {
  299. modbus.Write(address, value);
  300. }
  301. }
  302. public ConcurrentDictionary<string, object> GetAllData()
  303. {
  304. return Data;
  305. }
  306. public ConcurrentDictionary<string, object> GetFryOneData()
  307. {
  308. return FryOneData;
  309. }
  310. public ConcurrentDictionary<string, object> GetFryTwoData()
  311. {
  312. return FryTwoData;
  313. }
  314. public ConcurrentDictionary<string, object> GetFryThreeData()
  315. {
  316. return FryThreeData;
  317. }
  318. public ConcurrentDictionary<string, object> GetFryFourData()
  319. {
  320. return FryFourData;
  321. }
  322. public ConcurrentDictionary<string, object> GetFryFiveData()
  323. {
  324. return FryFiveData;
  325. }
  326. }
  327. }