终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

483 lines
23 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. }
  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. MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载成功");
  120. }
  121. catch (Exception)
  122. {
  123. IsConfig = false;
  124. MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载错误,请重新配置并重启软件");
  125. //throw;
  126. }
  127. }
  128. else
  129. {
  130. IsConfig = false;
  131. MessageNotify.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 "滚筒输送线": modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("滚筒线PLC连接成功"); break;
  167. case "炒锅1": fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("1号炒锅PLC连接成功"); break;
  168. case "炒锅2": fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("2号炒锅PLC连接成功"); break;
  169. case "炒锅3": fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("3号炒锅PLC连接成功"); break;
  170. case "炒锅4": fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("4号炒锅PLC连接成功"); break;
  171. case "炒锅5": fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("5号炒锅PLC连接成功"); break;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. public void ReadData()
  179. {
  180. if (IsConfig)
  181. {
  182. ThreadManage.GetInstance().StartLong(new Action(() =>
  183. {
  184. //滚筒线
  185. Connected = modbus.Connected;
  186. if (Connected)
  187. {
  188. foreach (var item in Variables)
  189. {
  190. var res = modbus.Read(item.Address, item.Length);//读取plc数据
  191. if (Data.ContainsKey(item.Address))
  192. {
  193. Data[item.Address] = res;
  194. }
  195. else
  196. {
  197. Data.TryAdd(item.Address, res);
  198. }
  199. }
  200. }
  201. else
  202. {
  203. DeviceManage devices = Json<DeviceManage>.Data;
  204. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "滚筒输送线");
  205. if (res != null)
  206. modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("滚筒线PLC重连成功");
  207. }
  208. Thread.Sleep(10);
  209. }), $"滚筒线实时数据读取线程");
  210. ThreadManage.GetInstance().StartLong(new Action(() =>
  211. {
  212. //炒锅1
  213. FryOneConnected = fryOneModbus.Connected;
  214. if (FryOneConnected)
  215. {
  216. foreach (var item in FryOneVariables)
  217. {
  218. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  219. if (FryOneData.ContainsKey(item.Address))
  220. {
  221. FryOneData[item.Address] = res;
  222. }
  223. else
  224. {
  225. FryOneData.TryAdd(item.Address, res);
  226. }
  227. }
  228. //Thread.Sleep(50);
  229. }
  230. else
  231. {
  232. DeviceManage devices = Json<DeviceManage>.Data;
  233. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅1");
  234. if (res != null)
  235. fryOneModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("1号炒锅PLC重连成功");
  236. }
  237. Thread.Sleep(10);
  238. }), $"炒锅1实时数据读取线程");
  239. ThreadManage.GetInstance().StartLong(new Action(() =>
  240. {
  241. //炒锅2
  242. FryTwoConnected = fryTwoModbus.Connected;
  243. if (FryTwoConnected)
  244. {
  245. foreach (var item in FryTwoVariables)
  246. {
  247. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  248. if (FryTwoData.ContainsKey(item.Address))
  249. {
  250. FryTwoData[item.Address] = res;
  251. }
  252. else
  253. {
  254. FryTwoData.TryAdd(item.Address, res);
  255. }
  256. }
  257. }
  258. else
  259. {
  260. DeviceManage devices = Json<DeviceManage>.Data;
  261. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅2");
  262. if (res != null)
  263. fryTwoModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("2号炒锅PLC重连成功");
  264. }
  265. Thread.Sleep(10);
  266. }), $"炒锅2实时数据读取线程");
  267. ThreadManage.GetInstance().StartLong(new Action(() =>
  268. {
  269. //炒锅3
  270. FryThreeConnected = fryThreeModbus.Connected;
  271. if (FryThreeConnected)
  272. {
  273. foreach (var item in FryThreeVariables)
  274. {
  275. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  276. if (FryThreeData.ContainsKey(item.Address))
  277. {
  278. FryThreeData[item.Address] = res;
  279. }
  280. else
  281. {
  282. FryThreeData.TryAdd(item.Address, res);
  283. }
  284. }
  285. //Thread.Sleep(50);
  286. }
  287. else
  288. {
  289. DeviceManage devices = Json<DeviceManage>.Data;
  290. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅3");
  291. if (res != null)
  292. fryThreeModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("3号炒锅PLC重连成功");
  293. }
  294. Thread.Sleep(10);
  295. }), $"炒锅3实时数据读取线程");
  296. ThreadManage.GetInstance().StartLong(new Action(() =>
  297. {
  298. //炒锅4
  299. FryFourConnected = fryFourModbus.Connected;
  300. if (FryFourConnected)
  301. {
  302. foreach (var item in FryFourVariables)
  303. {
  304. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  305. if (FryFourData.ContainsKey(item.Address))
  306. {
  307. FryFourData[item.Address] = res;
  308. }
  309. else
  310. {
  311. FryFourData.TryAdd(item.Address, res);
  312. }
  313. }
  314. //Thread.Sleep(50);
  315. }
  316. else
  317. {
  318. DeviceManage devices = Json<DeviceManage>.Data;
  319. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅4");
  320. if (res != null)
  321. fryFourModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("4号炒锅PLC重连成功");
  322. }
  323. Thread.Sleep(10);
  324. }), $"炒锅4实时数据读取线程");
  325. ThreadManage.GetInstance().StartLong(new Action(() =>
  326. {
  327. //炒锅5
  328. FryFiveConnected = fryFiveModbus.Connected;
  329. if (FryFiveConnected)
  330. {
  331. foreach (var item in FryFiveVariables)
  332. {
  333. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  334. if (FryFiveData.ContainsKey(item.Address))
  335. {
  336. FryFiveData[item.Address] = res;
  337. }
  338. else
  339. {
  340. FryFiveData.TryAdd(item.Address, res);
  341. }
  342. }
  343. // Thread.Sleep(50);
  344. }
  345. else
  346. {
  347. DeviceManage devices = Json<DeviceManage>.Data;
  348. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅5");
  349. if (res != null)
  350. fryFiveModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("5号炒锅PLC重连成功");
  351. }
  352. Thread.Sleep(10);
  353. }), $"炒锅5实时数据读取线程");
  354. }
  355. }
  356. /// <summary>
  357. /// 滚筒线写ushort数据
  358. /// </summary>
  359. /// <param name="address"></param>
  360. /// <param name="value"></param>
  361. public void WritePlcData(string address, ushort value)
  362. {
  363. lock (this)
  364. {
  365. modbus.Write(address, value);
  366. }
  367. }
  368. /// <summary>
  369. /// 滚筒线写float数据
  370. /// </summary>
  371. /// <param name="address"></param>
  372. /// <param name="value"></param>
  373. public void WriteRealPlcData(string address, float value)
  374. {
  375. lock (this)
  376. {
  377. modbus.SetReal(address,value);
  378. }
  379. }
  380. /// <summary>
  381. /// 炒锅1工艺数据
  382. /// </summary>
  383. /// <param name="address"></param>
  384. /// <param name="value"></param>
  385. public void WritePotOnePlcData(string address,ushort value )
  386. {
  387. fryOneModbus.Write(address,value);
  388. }
  389. /// <summary>
  390. /// 炒锅2工艺数据
  391. /// </summary>
  392. /// <param name="address"></param>
  393. /// <param name="value"></param>
  394. public void WritePotTwoPlcData(string address, ushort value)
  395. {
  396. fryTwoModbus.Write(address, value);
  397. }
  398. /// <summary>
  399. /// 炒锅3工艺数据
  400. /// </summary>
  401. /// <param name="address"></param>
  402. /// <param name="value"></param>
  403. public void WritePotThreePlcData(string address, ushort value)
  404. {
  405. fryThreeModbus.Write(address, value);
  406. }
  407. /// <summary>
  408. /// 炒锅4工艺数据
  409. /// </summary>
  410. /// <param name="address"></param>
  411. /// <param name="value"></param>
  412. public void WritePotFourPlcData(string address, ushort value)
  413. {
  414. fryFourModbus.Write(address,value);
  415. }
  416. /// <summary>
  417. /// 炒锅5工艺数据
  418. /// </summary>
  419. /// <param name="address"></param>
  420. /// <param name="value"></param>
  421. public void WritePotFivePlcData(string address, object value)
  422. {
  423. fryFiveModbus.Write(address, value);
  424. }
  425. public ConcurrentDictionary<string, object> GetAllData()
  426. {
  427. return Data;
  428. }
  429. public ConcurrentDictionary<string, object> GetFryOneData()
  430. {
  431. return FryOneData;
  432. }
  433. public ConcurrentDictionary<string, object> GetFryTwoData()
  434. {
  435. return FryTwoData;
  436. }
  437. public ConcurrentDictionary<string, object> GetFryThreeData()
  438. {
  439. return FryThreeData;
  440. }
  441. public ConcurrentDictionary<string, object> GetFryFourData()
  442. {
  443. return FryFourData;
  444. }
  445. public ConcurrentDictionary<string, object> GetFryFiveData()
  446. {
  447. return FryFiveData;
  448. }
  449. }
  450. }