终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

421 satır
21 KiB

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