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

343 lines
17 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. //炒锅1
  204. FryOneConnected = fryOneModbus.Connected;
  205. while (FryOneConnected)
  206. {
  207. foreach (var item in FryOneVariables)
  208. {
  209. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  210. if (FryOneData.ContainsKey(item.Address))
  211. {
  212. FryOneData[item.Address] = res;
  213. }
  214. else
  215. {
  216. FryOneData.TryAdd(item.Address, res);
  217. }
  218. }
  219. Thread.Sleep(50);
  220. }
  221. //炒锅2
  222. FryTwoConnected = fryTwoModbus.Connected;
  223. while (FryTwoConnected)
  224. {
  225. foreach (var item in FryTwoVariables)
  226. {
  227. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  228. if (FryTwoData.ContainsKey(item.Address))
  229. {
  230. FryTwoData[item.Address] = res;
  231. }
  232. else
  233. {
  234. FryTwoData.TryAdd(item.Address, res);
  235. }
  236. }
  237. Thread.Sleep(50);
  238. }
  239. //炒锅3
  240. FryThreeConnected = fryThreeModbus.Connected;
  241. while (FryThreeConnected)
  242. {
  243. foreach (var item in FryThreeVariables)
  244. {
  245. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  246. if (FryThreeData.ContainsKey(item.Address))
  247. {
  248. FryThreeData[item.Address] = res;
  249. }
  250. else
  251. {
  252. FryThreeData.TryAdd(item.Address, res);
  253. }
  254. }
  255. Thread.Sleep(50);
  256. }
  257. //炒锅4
  258. FryFourConnected = fryFourModbus.Connected;
  259. while (FryFourConnected)
  260. {
  261. foreach (var item in FryFourVariables)
  262. {
  263. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  264. if (FryFourData.ContainsKey(item.Address))
  265. {
  266. FryFourData[item.Address] = res;
  267. }
  268. else
  269. {
  270. FryFourData.TryAdd(item.Address, res);
  271. }
  272. }
  273. Thread.Sleep(50);
  274. }
  275. //炒锅5
  276. FryFiveConnected = fryFiveModbus.Connected;
  277. while (FryFiveConnected)
  278. {
  279. foreach (var item in FryFiveVariables)
  280. {
  281. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  282. if (FryFiveData.ContainsKey(item.Address))
  283. {
  284. FryFiveData[item.Address] = res;
  285. }
  286. else
  287. {
  288. FryFiveData.TryAdd(item.Address, res);
  289. }
  290. }
  291. Thread.Sleep(50);
  292. }
  293. Thread.Sleep(500);
  294. }), $"PLC实时数据读取线程");
  295. }
  296. }
  297. public void WritePlcData(string address, ushort value)
  298. {
  299. lock (this)
  300. {
  301. modbus.Write(address, value);
  302. }
  303. }
  304. public ConcurrentDictionary<string, object> GetAllData()
  305. {
  306. return Data;
  307. }
  308. public ConcurrentDictionary<string, object> GetFryOneData()
  309. {
  310. return FryOneData;
  311. }
  312. public ConcurrentDictionary<string, object> GetFryTwoData()
  313. {
  314. return FryTwoData;
  315. }
  316. public ConcurrentDictionary<string, object> GetFryThreeData()
  317. {
  318. return FryThreeData;
  319. }
  320. public ConcurrentDictionary<string, object> GetFryFourData()
  321. {
  322. return FryFourData;
  323. }
  324. public ConcurrentDictionary<string, object> GetFryFiveData()
  325. {
  326. return FryFiveData;
  327. }
  328. }
  329. }