终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DeviceOperate.cs 23 KiB

2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. 
  2. using BPASmartClient.CustomResource.Pages.Model;
  3. using BPASmartClient.Helper;
  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 ??(_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. MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载成功");
  121. }
  122. catch (Exception)
  123. {
  124. IsConfig = false;
  125. MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载错误,请重新配置并重启软件");
  126. //throw;
  127. }
  128. }
  129. else
  130. {
  131. IsConfig = false;
  132. MessageNotify.GetInstance.ShowRunLog("PLC变量配置加载失败:文件无数据,请重新配置并重启软件");
  133. }
  134. //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号
  135. //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号
  136. //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号
  137. //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态
  138. //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行
  139. //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号
  140. //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号
  141. //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV
  142. //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV
  143. //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行
  144. //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号
  145. //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号
  146. //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV
  147. //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号
  148. //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号
  149. }
  150. public void Connect()
  151. {
  152. if (IsConfig)
  153. {
  154. Json<DeviceManage>.Read();
  155. DeviceManage devices = Json<DeviceManage>.Data;
  156. if (devices != null)
  157. {
  158. if (devices.Devices.Count > 0)
  159. {
  160. for (int i = 0; i < devices.Devices.Count; i++)
  161. {
  162. string Ip = devices.Devices[i].Ip;
  163. string Port = devices.Devices[i].Port;
  164. string DeviceName = devices.Devices[i].DeviceName;
  165. switch (DeviceName)
  166. {
  167. case "滚筒输送线": modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("滚筒线PLC连接成功"); break;
  168. case "炒锅1": fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("1号炒锅PLC连接成功"); break;
  169. case "炒锅2": fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("2号炒锅PLC连接成功"); break;
  170. case "炒锅3": fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("3号炒锅PLC连接成功"); break;
  171. case "炒锅4": fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("4号炒锅PLC连接成功"); break;
  172. case "炒锅5": fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageNotify.GetInstance.ShowRunLog("5号炒锅PLC连接成功"); break;
  173. }
  174. }
  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. if (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. }
  202. else
  203. {
  204. DeviceManage devices = Json<DeviceManage>.Data;
  205. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "滚筒输送线");
  206. if (res != null)
  207. modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("滚筒线PLC重连成功");
  208. }
  209. Thread.Sleep(10);
  210. }), $"滚筒线实时数据读取线程");
  211. ThreadManage.GetInstance().StartLong(new Action(() =>
  212. {
  213. //炒锅1
  214. FryOneConnected = fryOneModbus.Connected;
  215. if (FryOneConnected)
  216. {
  217. foreach (var item in FryOneVariables)
  218. {
  219. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  220. if (FryOneData.ContainsKey(item.Address))
  221. {
  222. FryOneData[item.Address] = res;
  223. }
  224. else
  225. {
  226. FryOneData.TryAdd(item.Address, res);
  227. }
  228. }
  229. //Thread.Sleep(50);
  230. }
  231. else
  232. {
  233. DeviceManage devices = Json<DeviceManage>.Data;
  234. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅1");
  235. if (res != null)
  236. fryOneModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("1号炒锅PLC重连成功");
  237. }
  238. Thread.Sleep(10);
  239. }), $"炒锅1实时数据读取线程");
  240. ThreadManage.GetInstance().StartLong(new Action(() =>
  241. {
  242. //炒锅2
  243. FryTwoConnected = fryTwoModbus.Connected;
  244. if (FryTwoConnected)
  245. {
  246. foreach (var item in FryTwoVariables)
  247. {
  248. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  249. if (FryTwoData.ContainsKey(item.Address))
  250. {
  251. FryTwoData[item.Address] = res;
  252. }
  253. else
  254. {
  255. FryTwoData.TryAdd(item.Address, res);
  256. }
  257. }
  258. }
  259. else
  260. {
  261. DeviceManage devices = Json<DeviceManage>.Data;
  262. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅2");
  263. if (res != null)
  264. fryTwoModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("2号炒锅PLC重连成功");
  265. }
  266. Thread.Sleep(10);
  267. }), $"炒锅2实时数据读取线程");
  268. ThreadManage.GetInstance().StartLong(new Action(() =>
  269. {
  270. //炒锅3
  271. FryThreeConnected = fryThreeModbus.Connected;
  272. if (FryThreeConnected)
  273. {
  274. foreach (var item in FryThreeVariables)
  275. {
  276. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  277. if (FryThreeData.ContainsKey(item.Address))
  278. {
  279. FryThreeData[item.Address] = res;
  280. }
  281. else
  282. {
  283. FryThreeData.TryAdd(item.Address, res);
  284. }
  285. }
  286. //Thread.Sleep(50);
  287. }
  288. else
  289. {
  290. DeviceManage devices = Json<DeviceManage>.Data;
  291. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅3");
  292. if (res != null)
  293. fryThreeModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("3号炒锅PLC重连成功");
  294. }
  295. Thread.Sleep(10);
  296. }), $"炒锅3实时数据读取线程");
  297. ThreadManage.GetInstance().StartLong(new Action(() =>
  298. {
  299. //炒锅4
  300. FryFourConnected = fryFourModbus.Connected;
  301. if (FryFourConnected)
  302. {
  303. foreach (var item in FryFourVariables)
  304. {
  305. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  306. if (FryFourData.ContainsKey(item.Address))
  307. {
  308. FryFourData[item.Address] = res;
  309. }
  310. else
  311. {
  312. FryFourData.TryAdd(item.Address, res);
  313. }
  314. }
  315. //Thread.Sleep(50);
  316. }
  317. else
  318. {
  319. DeviceManage devices = Json<DeviceManage>.Data;
  320. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅4");
  321. if (res != null)
  322. fryFourModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("4号炒锅PLC重连成功");
  323. }
  324. Thread.Sleep(10);
  325. }), $"炒锅4实时数据读取线程");
  326. ThreadManage.GetInstance().StartLong(new Action(() =>
  327. {
  328. //炒锅5
  329. FryFiveConnected = fryFiveModbus.Connected;
  330. if (FryFiveConnected)
  331. {
  332. foreach (var item in FryFiveVariables)
  333. {
  334. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  335. if (FryFiveData.ContainsKey(item.Address))
  336. {
  337. FryFiveData[item.Address] = res;
  338. }
  339. else
  340. {
  341. FryFiveData.TryAdd(item.Address, res);
  342. }
  343. }
  344. // Thread.Sleep(50);
  345. }
  346. else
  347. {
  348. DeviceManage devices = Json<DeviceManage>.Data;
  349. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅5");
  350. if (res != null)
  351. fryFiveModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port)); MessageNotify.GetInstance.ShowRunLog("5号炒锅PLC重连成功");
  352. }
  353. Thread.Sleep(10);
  354. }), $"炒锅5实时数据读取线程");
  355. }
  356. }
  357. /// <summary>
  358. /// 滚筒线写ushort数据
  359. /// </summary>
  360. /// <param name="address"></param>
  361. /// <param name="value"></param>
  362. public void WritePlcData(string address, ushort value)
  363. {
  364. lock (this)
  365. {
  366. modbus.Write(address, value);
  367. }
  368. }
  369. /// <summary>
  370. /// 滚筒线写float数据
  371. /// </summary>
  372. /// <param name="address"></param>
  373. /// <param name="value"></param>
  374. public void WriteRealPlcData(string address, float value)
  375. {
  376. lock (this)
  377. {
  378. modbus.SetReal(address,value);
  379. }
  380. }
  381. /// <summary>
  382. /// 炒锅1工艺数据
  383. /// </summary>
  384. /// <param name="address"></param>
  385. /// <param name="value"></param>
  386. public void WritePotOnePlcData(string address,ushort value )
  387. {
  388. fryOneModbus.Write(address,value);
  389. }
  390. /// <summary>
  391. /// 炒锅2工艺数据
  392. /// </summary>
  393. /// <param name="address"></param>
  394. /// <param name="value"></param>
  395. public void WritePotTwoPlcData(string address, ushort value)
  396. {
  397. fryTwoModbus.Write(address, value);
  398. }
  399. /// <summary>
  400. /// 炒锅3工艺数据
  401. /// </summary>
  402. /// <param name="address"></param>
  403. /// <param name="value"></param>
  404. public void WritePotThreePlcData(string address, ushort value)
  405. {
  406. fryThreeModbus.Write(address, value);
  407. }
  408. /// <summary>
  409. /// 炒锅4工艺数据
  410. /// </summary>
  411. /// <param name="address"></param>
  412. /// <param name="value"></param>
  413. public void WritePotFourPlcData(string address, ushort value)
  414. {
  415. fryFourModbus.Write(address,value);
  416. }
  417. /// <summary>
  418. /// 炒锅5工艺数据
  419. /// </summary>
  420. /// <param name="address"></param>
  421. /// <param name="value"></param>
  422. public void WritePotFivePlcData(string address, object value)
  423. {
  424. fryFiveModbus.Write(address, value);
  425. }
  426. public ConcurrentDictionary<string, object> GetAllData()
  427. {
  428. return Data;
  429. }
  430. public ConcurrentDictionary<string, object> GetFryOneData()
  431. {
  432. return FryOneData;
  433. }
  434. public ConcurrentDictionary<string, object> GetFryTwoData()
  435. {
  436. return FryTwoData;
  437. }
  438. public ConcurrentDictionary<string, object> GetFryThreeData()
  439. {
  440. return FryThreeData;
  441. }
  442. public ConcurrentDictionary<string, object> GetFryFourData()
  443. {
  444. return FryFourData;
  445. }
  446. public ConcurrentDictionary<string, object> GetFryFiveData()
  447. {
  448. return FryFiveData;
  449. }
  450. }
  451. }