终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

628 linhas
27 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. //消息注册
  134. modbus.ShowEx += new Action<string>((s) =>
  135. {
  136. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  137. });
  138. fryOneModbus.ShowEx += new Action<string>((s) =>
  139. {
  140. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  141. });
  142. fryTwoModbus.ShowEx += new Action<string>((s) =>
  143. {
  144. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  145. });
  146. fryThreeModbus.ShowEx += new Action<string>((s) =>
  147. {
  148. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  149. });
  150. fryFourModbus.ShowEx += new Action<string>((s) =>
  151. {
  152. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  153. });
  154. fryFiveModbus.ShowEx += new Action<string>((s) =>
  155. {
  156. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  157. });
  158. modbus.Show += new Action<string>((s) =>
  159. {
  160. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  161. });
  162. fryOneModbus.Show += new Action<string>((s) =>
  163. {
  164. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  165. });
  166. fryTwoModbus.Show += new Action<string>((s) =>
  167. {
  168. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  169. });
  170. fryThreeModbus.Show += new Action<string>((s) =>
  171. {
  172. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  173. });
  174. fryFourModbus.Show += new Action<string>((s) =>
  175. {
  176. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  177. });
  178. fryFiveModbus.Show += new Action<string>((s) =>
  179. {
  180. if (s != null) MessageNotify.GetInstance.ShowRunLog(s);
  181. });
  182. }
  183. public void Connect()
  184. {
  185. if (IsConfig)
  186. {
  187. Json<DeviceManage>.Read();
  188. DeviceManage devices = Json<DeviceManage>.Data;
  189. if (devices != null)
  190. {
  191. if (devices.Devices.Count > 0)
  192. {
  193. for (int i = 0; i < devices.Devices.Count; i++)
  194. {
  195. string Ip = devices.Devices[i].Ip;
  196. string Port = devices.Devices[i].Port;
  197. string DeviceName = devices.Devices[i].DeviceName;
  198. switch (DeviceName)
  199. {
  200. case "滚筒输送线": modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
  201. case "炒锅1": fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
  202. case "炒锅2": fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
  203. case "炒锅3": fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
  204. case "炒锅4": fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
  205. case "炒锅5": fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); break;
  206. }
  207. }
  208. if (modbus.Connected)
  209. MessageNotify.GetInstance.ShowRunLog("滚筒线PLC连接成功");
  210. if (fryOneModbus.Connected)
  211. MessageNotify.GetInstance.ShowRunLog("1号炒锅PLC连接成功");
  212. if (fryTwoModbus.Connected)
  213. MessageNotify.GetInstance.ShowRunLog("2号炒锅PLC连接成功");
  214. if (fryThreeModbus.Connected)
  215. MessageNotify.GetInstance.ShowRunLog("3号炒锅PLC连接成功");
  216. if (fryFourModbus.Connected)
  217. MessageNotify.GetInstance.ShowRunLog("4号炒锅PLC连接成功");
  218. if (fryFiveModbus.Connected)
  219. MessageNotify.GetInstance.ShowRunLog("5号炒锅PLC连接成功");
  220. }
  221. }
  222. }
  223. }
  224. public void ReadData()
  225. {
  226. if (IsConfig)
  227. {
  228. ThreadManage.GetInstance().StartLong(new Action(() =>
  229. {
  230. //滚筒线
  231. try
  232. {
  233. Connected = modbus.Connected;
  234. if (Connected)
  235. {
  236. foreach (var item in Variables)
  237. {
  238. var res = modbus.Read(item.Address, item.Length);//读取plc数据
  239. if (Data.ContainsKey(item.Address))
  240. {
  241. Data[item.Address] = res;
  242. }
  243. else
  244. {
  245. Data.TryAdd(item.Address, res);
  246. }
  247. }
  248. }
  249. else
  250. {
  251. DeviceManage devices = Json<DeviceManage>.Data;
  252. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "滚筒输送线");
  253. if (res != null)
  254. modbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
  255. Thread.Sleep(10000);
  256. }
  257. }
  258. catch (Exception)
  259. {
  260. // throw;
  261. }
  262. Thread.Sleep(10);
  263. }), $"滚筒线实时数据读取线程");
  264. ThreadManage.GetInstance().StartLong(new Action(() =>
  265. {
  266. try
  267. {
  268. //炒锅1
  269. FryOneConnected = fryOneModbus.Connected;
  270. if (FryOneConnected)
  271. {
  272. foreach (var item in FryOneVariables)
  273. {
  274. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  275. if (FryOneData.ContainsKey(item.Address))
  276. {
  277. FryOneData[item.Address] = res;
  278. }
  279. else
  280. {
  281. FryOneData.TryAdd(item.Address, res);
  282. }
  283. }
  284. //Thread.Sleep(50);
  285. }
  286. else
  287. {
  288. DeviceManage devices = Json<DeviceManage>.Data;
  289. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅1");
  290. if (res != null)
  291. fryOneModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
  292. Thread.Sleep(10000);
  293. }
  294. }
  295. catch (Exception)
  296. {
  297. //throw;
  298. }
  299. Thread.Sleep(10);
  300. }), $"炒锅1实时数据读取线程");
  301. ThreadManage.GetInstance().StartLong(new Action(() =>
  302. {
  303. try
  304. {
  305. //炒锅2
  306. FryTwoConnected = fryTwoModbus.Connected;
  307. if (FryTwoConnected)
  308. {
  309. foreach (var item in FryTwoVariables)
  310. {
  311. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  312. if (FryTwoData.ContainsKey(item.Address))
  313. {
  314. FryTwoData[item.Address] = res;
  315. }
  316. else
  317. {
  318. FryTwoData.TryAdd(item.Address, res);
  319. }
  320. }
  321. }
  322. else
  323. {
  324. DeviceManage devices = Json<DeviceManage>.Data;
  325. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅2");
  326. if (res != null)
  327. fryTwoModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
  328. Thread.Sleep(10000);
  329. }
  330. }
  331. catch (Exception)
  332. {
  333. //throw;
  334. }
  335. Thread.Sleep(10);
  336. }), $"炒锅2实时数据读取线程");
  337. ThreadManage.GetInstance().StartLong(new Action(() =>
  338. {
  339. //炒锅3
  340. try
  341. {
  342. FryThreeConnected = fryThreeModbus.Connected;
  343. if (FryThreeConnected)
  344. {
  345. foreach (var item in FryThreeVariables)
  346. {
  347. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  348. if (FryThreeData.ContainsKey(item.Address))
  349. {
  350. FryThreeData[item.Address] = res;
  351. }
  352. else
  353. {
  354. FryThreeData.TryAdd(item.Address, res);
  355. }
  356. }
  357. //Thread.Sleep(50);
  358. }
  359. else
  360. {
  361. DeviceManage devices = Json<DeviceManage>.Data;
  362. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅3");
  363. if (res != null)
  364. fryThreeModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
  365. Thread.Sleep(10000);
  366. }
  367. }
  368. catch (Exception)
  369. {
  370. //throw;
  371. }
  372. Thread.Sleep(10);
  373. }), $"炒锅3实时数据读取线程");
  374. ThreadManage.GetInstance().StartLong(new Action(() =>
  375. {
  376. try
  377. {
  378. //炒锅4
  379. FryFourConnected = fryFourModbus.Connected;
  380. if (FryFourConnected)
  381. {
  382. foreach (var item in FryFourVariables)
  383. {
  384. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  385. if (FryFourData.ContainsKey(item.Address))
  386. {
  387. FryFourData[item.Address] = res;
  388. }
  389. else
  390. {
  391. FryFourData.TryAdd(item.Address, res);
  392. }
  393. }
  394. //Thread.Sleep(50);
  395. }
  396. else
  397. {
  398. DeviceManage devices = Json<DeviceManage>.Data;
  399. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅4");
  400. if (res != null)
  401. fryFourModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
  402. Thread.Sleep(10000);
  403. }
  404. }
  405. catch (Exception)
  406. {
  407. //throw;
  408. }
  409. Thread.Sleep(10);
  410. }), $"炒锅4实时数据读取线程");
  411. ThreadManage.GetInstance().StartLong(new Action(() =>
  412. {
  413. try
  414. {
  415. //炒锅5
  416. FryFiveConnected = fryFiveModbus.Connected;
  417. if (FryFiveConnected)
  418. {
  419. foreach (var item in FryFiveVariables)
  420. {
  421. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  422. if (FryFiveData.ContainsKey(item.Address))
  423. {
  424. FryFiveData[item.Address] = res;
  425. }
  426. else
  427. {
  428. FryFiveData.TryAdd(item.Address, res);
  429. }
  430. }
  431. // Thread.Sleep(50);
  432. }
  433. else
  434. {
  435. DeviceManage devices = Json<DeviceManage>.Data;
  436. var res = devices.Devices.FirstOrDefault(p => p.DeviceName == "炒锅5");
  437. if (res != null)
  438. fryFiveModbus.ModbusTcpConnect(res.Ip, Convert.ToInt32(res.Port));
  439. Thread.Sleep(10000);
  440. }
  441. }
  442. catch (Exception)
  443. {
  444. // throw;
  445. }
  446. Thread.Sleep(10);
  447. }), $"炒锅5实时数据读取线程");
  448. }
  449. }
  450. /// <summary>
  451. /// 滚筒线写ushort数据
  452. /// </summary>
  453. /// <param name="address"></param>
  454. /// <param name="value"></param>
  455. public void WritePlcData(string address, ushort value)
  456. {
  457. lock (this)
  458. {
  459. modbus.Write(address, value);
  460. }
  461. }
  462. /// <summary>
  463. /// 滚筒线写float数据
  464. /// </summary>
  465. /// <param name="address"></param>
  466. /// <param name="value"></param>
  467. public void WriteRealPlcData(string address, float value)
  468. {
  469. lock (this)
  470. {
  471. modbus.SetReal(address,value);
  472. }
  473. }
  474. /// <summary>
  475. /// 炒锅1工艺数据
  476. /// </summary>
  477. /// <param name="address"></param>
  478. /// <param name="value"></param>
  479. public void WritePotOnePlcData(string address,ushort value )
  480. {
  481. fryOneModbus.Write(address,value);
  482. }
  483. /// <summary>
  484. /// 炒锅1工艺目标重量偏差
  485. /// </summary>
  486. /// <param name="address"></param>
  487. /// <param name="value"></param>
  488. public void WritePotOneRealPlcData(string address, float value)
  489. {
  490. fryOneModbus.SetReal(address, value);
  491. }
  492. /// <summary>
  493. /// 炒锅2工艺数据
  494. /// </summary>
  495. /// <param name="address"></param>
  496. /// <param name="value"></param>
  497. public void WritePotTwoPlcData(string address, ushort value)
  498. {
  499. fryTwoModbus.Write(address, value);
  500. }
  501. /// <summary>
  502. /// 炒锅2工艺目标重量偏差
  503. /// </summary>
  504. /// <param name="address"></param>
  505. /// <param name="value"></param>
  506. public void WritePotTwoRealPlcData(string address, float value)
  507. {
  508. fryTwoModbus.SetReal(address, value);
  509. }
  510. /// <summary>
  511. /// 炒锅3工艺数据
  512. /// </summary>
  513. /// <param name="address"></param>
  514. /// <param name="value"></param>
  515. public void WritePotThreePlcData(string address, ushort value)
  516. {
  517. fryThreeModbus.Write(address, value);
  518. }
  519. /// <summary>
  520. /// 炒锅3工艺目标重量偏差
  521. /// </summary>
  522. /// <param name="address"></param>
  523. /// <param name="value"></param>
  524. public void WritePotThreeRealPlcData(string address, float value)
  525. {
  526. fryThreeModbus.SetReal(address, value);
  527. }
  528. /// <summary>
  529. /// 炒锅4工艺数据
  530. /// </summary>
  531. /// <param name="address"></param>
  532. /// <param name="value"></param>
  533. public void WritePotFourPlcData(string address, ushort value)
  534. {
  535. fryFourModbus.Write(address,value);
  536. }
  537. /// <summary>
  538. /// 炒锅4工艺目标重量偏差
  539. /// </summary>
  540. /// <param name="address"></param>
  541. /// <param name="value"></param>
  542. public void WritePotFourRealPlcData(string address, float value)
  543. {
  544. fryFourModbus.SetReal(address, value);
  545. }
  546. /// <summary>
  547. /// 炒锅5工艺数据
  548. /// </summary>
  549. /// <param name="address"></param>
  550. /// <param name="value"></param>
  551. public void WritePotFivePlcData(string address, object value)
  552. {
  553. fryFiveModbus.Write(address, value);
  554. }
  555. /// <summary>
  556. /// 炒锅5工艺目标重量偏差
  557. /// </summary>
  558. /// <param name="address"></param>
  559. /// <param name="value"></param>
  560. public void WritePotFiveRealPlcData(string address, float value)
  561. {
  562. fryFiveModbus.SetReal(address, value);
  563. }
  564. public ConcurrentDictionary<string, object> GetAllData()
  565. {
  566. return Data;
  567. }
  568. public ConcurrentDictionary<string, object> GetFryOneData()
  569. {
  570. return FryOneData;
  571. }
  572. public ConcurrentDictionary<string, object> GetFryTwoData()
  573. {
  574. return FryTwoData;
  575. }
  576. public ConcurrentDictionary<string, object> GetFryThreeData()
  577. {
  578. return FryThreeData;
  579. }
  580. public ConcurrentDictionary<string, object> GetFryFourData()
  581. {
  582. return FryFourData;
  583. }
  584. public ConcurrentDictionary<string, object> GetFryFiveData()
  585. {
  586. return FryFiveData;
  587. }
  588. }
  589. }