终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

363 рядки
18 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 ??= 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. }
  53. public void Init()
  54. {
  55. Variables.Clear();
  56. FryOneVariables.Clear();
  57. FryTwoVariables.Clear();
  58. FryThreeVariables.Clear();
  59. FryFourVariables.Clear();
  60. FryFiveVariables.Clear();
  61. Json<PlcVariableInfoManage>.Read();
  62. if (Json<PlcVariableInfoManage>.Data.VariablesInfo.Count > 0)
  63. {
  64. try
  65. {
  66. if (Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒输送线"].Count > 0)
  67. {
  68. //foreach (var item in Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒运输线"])
  69. //{
  70. // Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  71. //}
  72. foreach (KeyValuePair<string, ObservableCollection<PlcVariableInfoModel>> dic in Json<PlcVariableInfoManage>.Data.VariablesInfo)
  73. {
  74. if (string.Equals(dic.Key, "滚筒输送线"))
  75. {
  76. foreach (var item in dic.Value)
  77. {
  78. Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  79. }
  80. }
  81. if (string.Equals(dic.Key, "炒锅1"))
  82. {
  83. foreach (var item in dic.Value)
  84. {
  85. FryOneVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  86. }
  87. }
  88. if (string.Equals(dic.Key, "炒锅2"))
  89. {
  90. foreach (var item in dic.Value)
  91. {
  92. FryTwoVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  93. }
  94. }
  95. if (string.Equals(dic.Key, "炒锅3"))
  96. {
  97. foreach (var item in dic.Value)
  98. {
  99. FryThreeVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  100. }
  101. }
  102. if (string.Equals(dic.Key, "炒锅4"))
  103. {
  104. foreach (var item in dic.Value)
  105. {
  106. FryFourVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  107. }
  108. }
  109. if (string.Equals(dic.Key, "炒锅5"))
  110. {
  111. foreach (var item in dic.Value)
  112. {
  113. FryFiveVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  114. }
  115. }
  116. }
  117. }
  118. IsConfig = true;
  119. MessageLog.GetInstance.ShowRunLog("PLC变量配置成功");
  120. }
  121. catch (Exception)
  122. {
  123. IsConfig = false;
  124. MessageLog.GetInstance.ShowRunLog("PLC变量配置错误,请重新配置并重启软件");
  125. //throw;
  126. }
  127. }
  128. else
  129. {
  130. IsConfig = false;
  131. MessageLog.GetInstance.ShowRunLog("PLC变量配置失败:文件无数据,请重新配置并重启软件");
  132. }
  133. //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号
  134. //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号
  135. //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号
  136. //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态
  137. //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行
  138. //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号
  139. //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号
  140. //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV
  141. //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV
  142. //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行
  143. //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号
  144. //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号
  145. //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV
  146. //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号
  147. //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号
  148. }
  149. public void Connect()
  150. {
  151. if (IsConfig)
  152. {
  153. Json<DeviceManage>.Read();
  154. DeviceManage devices = Json<DeviceManage>.Data;
  155. if (devices != null)
  156. {
  157. if (devices.Devices.Count > 0)
  158. {
  159. for (int i = 0; i < devices.Devices.Count; i++)
  160. {
  161. string Ip = devices.Devices[i].Ip;
  162. string Port = devices.Devices[i].Port;
  163. string DeviceName = devices.Devices[i].DeviceName;
  164. switch (DeviceName)
  165. {
  166. case "滚筒输送线": Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("滚筒线PLC连接成功"); }); break;
  167. case "炒锅1": Task.Run(() => { fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("1号炒锅PLC连接成功"); }); break;
  168. case "炒锅2": Task.Run(() => { fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("2号炒锅PLC连接成功"); }); break;
  169. case "炒锅3": Task.Run(() => { fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("3号炒锅PLC连接成功"); }); break;
  170. case "炒锅4": Task.Run(() => { fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("4号炒锅PLC连接成功"); }); break;
  171. case "炒锅5": Task.Run(() => { fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("5号炒锅PLC连接成功"); }); break;
  172. }
  173. }
  174. // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.ShowRunLog("PLC连接成功"); })); });
  175. }
  176. }
  177. }
  178. }
  179. public void ReadData()
  180. {
  181. if (IsConfig)
  182. {
  183. ThreadManage.GetInstance().StartLong(new Action(() =>
  184. {
  185. //滚筒线
  186. Connected = modbus.Connected;
  187. while (Connected)
  188. {
  189. foreach (var item in Variables)
  190. {
  191. var res = modbus.Read(item.Address, item.Length);//读取plc数据
  192. if (Data.ContainsKey(item.Address))
  193. {
  194. Data[item.Address] = res;
  195. }
  196. else
  197. {
  198. Data.TryAdd(item.Address, res);
  199. }
  200. }
  201. Thread.Sleep(50);
  202. }
  203. Thread.Sleep(10);
  204. }), $"滚筒线实时数据读取线程");
  205. ThreadManage.GetInstance().StartLong(new Action(() =>
  206. {
  207. //炒锅1
  208. FryOneConnected = fryOneModbus.Connected;
  209. while (FryOneConnected)
  210. {
  211. foreach (var item in FryOneVariables)
  212. {
  213. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  214. if (FryOneData.ContainsKey(item.Address))
  215. {
  216. FryOneData[item.Address] = res;
  217. }
  218. else
  219. {
  220. FryOneData.TryAdd(item.Address, res);
  221. }
  222. }
  223. Thread.Sleep(50);
  224. }
  225. Thread.Sleep(10);
  226. }), $"炒锅1实时数据读取线程");
  227. ThreadManage.GetInstance().StartLong(new Action(() =>
  228. {
  229. //炒锅2
  230. FryTwoConnected = fryTwoModbus.Connected;
  231. while (FryTwoConnected)
  232. {
  233. foreach (var item in FryTwoVariables)
  234. {
  235. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  236. if (FryTwoData.ContainsKey(item.Address))
  237. {
  238. FryTwoData[item.Address] = res;
  239. }
  240. else
  241. {
  242. FryTwoData.TryAdd(item.Address, res);
  243. }
  244. }
  245. Thread.Sleep(50);
  246. }
  247. Thread.Sleep(10);
  248. }), $"炒锅2实时数据读取线程");
  249. ThreadManage.GetInstance().StartLong(new Action(() =>
  250. {
  251. //炒锅3
  252. FryThreeConnected = fryThreeModbus.Connected;
  253. while (FryThreeConnected)
  254. {
  255. foreach (var item in FryThreeVariables)
  256. {
  257. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  258. if (FryThreeData.ContainsKey(item.Address))
  259. {
  260. FryThreeData[item.Address] = res;
  261. }
  262. else
  263. {
  264. FryThreeData.TryAdd(item.Address, res);
  265. }
  266. }
  267. Thread.Sleep(50);
  268. }
  269. Thread.Sleep(10);
  270. }), $"炒锅3实时数据读取线程");
  271. ThreadManage.GetInstance().StartLong(new Action(() =>
  272. {
  273. //炒锅4
  274. FryFourConnected = fryFourModbus.Connected;
  275. while (FryFourConnected)
  276. {
  277. foreach (var item in FryFourVariables)
  278. {
  279. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  280. if (FryFourData.ContainsKey(item.Address))
  281. {
  282. FryFourData[item.Address] = res;
  283. }
  284. else
  285. {
  286. FryFourData.TryAdd(item.Address, res);
  287. }
  288. }
  289. Thread.Sleep(50);
  290. }
  291. Thread.Sleep(10);
  292. }), $"炒锅4实时数据读取线程");
  293. ThreadManage.GetInstance().StartLong(new Action(() =>
  294. {
  295. //炒锅5
  296. FryFiveConnected = fryFiveModbus.Connected;
  297. while (FryFiveConnected)
  298. {
  299. foreach (var item in FryFiveVariables)
  300. {
  301. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  302. if (FryFiveData.ContainsKey(item.Address))
  303. {
  304. FryFiveData[item.Address] = res;
  305. }
  306. else
  307. {
  308. FryFiveData.TryAdd(item.Address, res);
  309. }
  310. }
  311. Thread.Sleep(50);
  312. }
  313. Thread.Sleep(10);
  314. }), $"炒锅5实时数据读取线程");
  315. }
  316. }
  317. public void WritePlcData(string address, ushort value)
  318. {
  319. lock (this)
  320. {
  321. modbus.Write(address, value);
  322. }
  323. }
  324. public ConcurrentDictionary<string, object> GetAllData()
  325. {
  326. return Data;
  327. }
  328. public ConcurrentDictionary<string, object> GetFryOneData()
  329. {
  330. return FryOneData;
  331. }
  332. public ConcurrentDictionary<string, object> GetFryTwoData()
  333. {
  334. return FryTwoData;
  335. }
  336. public ConcurrentDictionary<string, object> GetFryThreeData()
  337. {
  338. return FryThreeData;
  339. }
  340. public ConcurrentDictionary<string, object> GetFryFourData()
  341. {
  342. return FryFourData;
  343. }
  344. public ConcurrentDictionary<string, object> GetFryFiveData()
  345. {
  346. return FryFiveData;
  347. }
  348. }
  349. }