终端一体化运控平台
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.

DeviceOperate.cs 17 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 ??= 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. MessageLog.GetInstance.ShowRunLog("PLC变量配置成功");
  121. }
  122. catch (Exception)
  123. {
  124. IsConfig = false;
  125. MessageLog.GetInstance.ShowRunLog("PLC变量配置错误,请重新配置并重启软件");
  126. //throw;
  127. }
  128. }
  129. else
  130. {
  131. IsConfig = false;
  132. MessageLog.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 "滚筒输送线": Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("滚筒线PLC连接成功"); }); break;
  168. case "炒锅1": Task.Run(() => { fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("1号炒锅PLC连接成功"); }); break;
  169. case "炒锅2": Task.Run(() => { fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("2号炒锅PLC连接成功"); }); break;
  170. case "炒锅3": Task.Run(() => { fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("3号炒锅PLC连接成功"); }); break;
  171. case "炒锅4": Task.Run(() => { fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("4号炒锅PLC连接成功"); }); break;
  172. case "炒锅5": Task.Run(() => { fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); MessageLog.GetInstance.ShowRunLog("5号炒锅PLC连接成功"); }); break;
  173. }
  174. }
  175. // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.ShowRunLog("PLC连接成功"); })); });
  176. }
  177. }
  178. }
  179. }
  180. public void ReadData()
  181. {
  182. if (IsConfig)
  183. {
  184. ThreadManage.GetInstance().StartLong(new Action(() =>
  185. {
  186. //滚筒线
  187. Connected = modbus.Connected;
  188. while (Connected)
  189. {
  190. foreach (var item in Variables)
  191. {
  192. var res = modbus.Read(item.Address, item.Length);//读取plc数据
  193. if (Data.ContainsKey(item.Address))
  194. {
  195. Data[item.Address] = res;
  196. }
  197. else
  198. {
  199. Data.TryAdd(item.Address, res);
  200. }
  201. }
  202. Thread.Sleep(50);
  203. }
  204. //炒锅1
  205. FryOneConnected = fryOneModbus.Connected;
  206. while (FryOneConnected)
  207. {
  208. foreach (var item in FryOneVariables)
  209. {
  210. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  211. if (FryOneData.ContainsKey(item.Address))
  212. {
  213. FryOneData[item.Address] = res;
  214. }
  215. else
  216. {
  217. FryOneData.TryAdd(item.Address, res);
  218. }
  219. }
  220. Thread.Sleep(50);
  221. }
  222. //炒锅2
  223. FryTwoConnected = fryTwoModbus.Connected;
  224. while (FryTwoConnected)
  225. {
  226. foreach (var item in FryTwoVariables)
  227. {
  228. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  229. if (FryTwoData.ContainsKey(item.Address))
  230. {
  231. FryTwoData[item.Address] = res;
  232. }
  233. else
  234. {
  235. FryTwoData.TryAdd(item.Address, res);
  236. }
  237. }
  238. Thread.Sleep(50);
  239. }
  240. //炒锅3
  241. FryThreeConnected = fryThreeModbus.Connected;
  242. while (FryThreeConnected)
  243. {
  244. foreach (var item in FryThreeVariables)
  245. {
  246. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  247. if (FryThreeData.ContainsKey(item.Address))
  248. {
  249. FryThreeData[item.Address] = res;
  250. }
  251. else
  252. {
  253. FryThreeData.TryAdd(item.Address, res);
  254. }
  255. }
  256. Thread.Sleep(50);
  257. }
  258. //炒锅4
  259. FryFourConnected = fryFourModbus.Connected;
  260. while (FryFourConnected)
  261. {
  262. foreach (var item in FryFourVariables)
  263. {
  264. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  265. if (FryFourData.ContainsKey(item.Address))
  266. {
  267. FryFourData[item.Address] = res;
  268. }
  269. else
  270. {
  271. FryFourData.TryAdd(item.Address, res);
  272. }
  273. }
  274. Thread.Sleep(50);
  275. }
  276. //炒锅5
  277. FryFiveConnected = fryFiveModbus.Connected;
  278. while (FryFiveConnected)
  279. {
  280. foreach (var item in FryFiveVariables)
  281. {
  282. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  283. if (FryFiveData.ContainsKey(item.Address))
  284. {
  285. FryFiveData[item.Address] = res;
  286. }
  287. else
  288. {
  289. FryFiveData.TryAdd(item.Address, res);
  290. }
  291. }
  292. Thread.Sleep(50);
  293. }
  294. Thread.Sleep(500);
  295. }), $"PLC实时数据读取线程");
  296. }
  297. }
  298. public void WritePlcData(string address, ushort value)
  299. {
  300. lock (this)
  301. {
  302. modbus.Write(address, value);
  303. }
  304. }
  305. public ConcurrentDictionary<string, object> GetAllData()
  306. {
  307. return Data;
  308. }
  309. public ConcurrentDictionary<string, object> GetFryOneData()
  310. {
  311. return FryOneData;
  312. }
  313. public ConcurrentDictionary<string, object> GetFryTwoData()
  314. {
  315. return FryTwoData;
  316. }
  317. public ConcurrentDictionary<string, object> GetFryThreeData()
  318. {
  319. return FryThreeData;
  320. }
  321. public ConcurrentDictionary<string, object> GetFryFourData()
  322. {
  323. return FryFourData;
  324. }
  325. public ConcurrentDictionary<string, object> GetFryFiveData()
  326. {
  327. return FryFiveData;
  328. }
  329. }
  330. }