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

DeviceOperate.cs 17 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年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. 
  2. using BPASmartClient.CustomResource.Pages.Model;
  3. using BPASmartClient.Helper;
  4. using BPASmartClient.Message;
  5. using BPASmartClient.Modbus;
  6. using FryPot_DosingSystem.Model;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace FryPot_DosingSystem.Control
  16. {
  17. internal class DeviceOperate
  18. {
  19. private static DeviceOperate _instance;
  20. public static DeviceOperate GetInstance => _instance ??= new DeviceOperate();
  21. public bool IsConfig { get; set; }//设备plc数据是否配置
  22. ModbusTcp modbus = new ModbusTcp();//滚筒线Modbus通讯对象
  23. ModbusTcp fryOneModbus = new ModbusTcp();//炒锅1Modbus通讯对象
  24. ModbusTcp fryTwoModbus = new ModbusTcp();//炒锅2Modbus通讯对象
  25. ModbusTcp fryThreeModbus = new ModbusTcp();//炒锅3Modbus通讯对象
  26. ModbusTcp fryFourModbus = new ModbusTcp();//炒锅4Modbus通讯对象
  27. ModbusTcp fryFiveModbus = new ModbusTcp();//炒锅5Modbus通讯对象
  28. // private string Ip { get; set; }
  29. // private string Port { get; set; }
  30. public bool Connected { get; set; }
  31. public bool FryOneConnected { get; set; }
  32. public bool FryTwoConnected { get; set; }
  33. public bool FryThreeConnected { get; set; }
  34. public bool FryFourConnected { get; set; }
  35. public bool FryFiveConnected { get; set; }
  36. // private string DeviceName { get; set; }
  37. public ConcurrentDictionary<string, object> Data { get; set; } = new ConcurrentDictionary<string, object>();
  38. public ConcurrentDictionary<string, object> FryOneData { get; set; } = new ConcurrentDictionary<string, object>();
  39. public ConcurrentDictionary<string, object> FryTwoData { get; set; } = new ConcurrentDictionary<string, object>();
  40. public ConcurrentDictionary<string, object> FryThreeData { get; set; } = new ConcurrentDictionary<string, object>();
  41. public ConcurrentDictionary<string, object> FryFourData { get; set; } = new ConcurrentDictionary<string, object>();
  42. public ConcurrentDictionary<string, object> FryFiveData { get; set; } = new ConcurrentDictionary<string, object>();
  43. public ObservableCollection<PlcVariableModel> Variables { get; set; } = new ObservableCollection<PlcVariableModel>();
  44. public ObservableCollection<PlcVariableModel> FryOneVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  45. public ObservableCollection<PlcVariableModel> FryTwoVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  46. public ObservableCollection<PlcVariableModel> FryThreeVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  47. public ObservableCollection<PlcVariableModel> FryFourVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  48. public ObservableCollection<PlcVariableModel> FryFiveVariables { get; set; } = new ObservableCollection<PlcVariableModel>();
  49. public DeviceOperate()
  50. {
  51. Init();
  52. Connect();
  53. ReadData();
  54. }
  55. public void Init()
  56. {
  57. Variables.Clear();
  58. FryOneVariables.Clear();
  59. FryTwoVariables.Clear();
  60. FryThreeVariables.Clear();
  61. FryFourVariables.Clear();
  62. FryFiveVariables.Clear();
  63. Json<PlcVariableInfoManage>.Read();
  64. if (Json<PlcVariableInfoManage>.Data.VariablesInfo.Count > 0)
  65. {
  66. try
  67. {
  68. if (Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒输送线"].Count>0)
  69. {
  70. //foreach (var item in Json<PlcVariableInfoManage>.Data.VariablesInfo["滚筒运输线"])
  71. //{
  72. // Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  73. //}
  74. foreach (KeyValuePair<string,ObservableCollection<PlcVariableInfoModel>> dic in Json<PlcVariableInfoManage>.Data.VariablesInfo)
  75. {
  76. if (string.Equals(dic.Key, "滚筒输送线"))
  77. {
  78. foreach (var item in dic.Value)
  79. {
  80. Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  81. }
  82. }
  83. if (string.Equals(dic.Key, "炒锅1"))
  84. {
  85. foreach (var item in dic.Value)
  86. {
  87. FryOneVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  88. }
  89. }
  90. if (string.Equals(dic.Key, "炒锅2"))
  91. {
  92. foreach (var item in dic.Value)
  93. {
  94. FryTwoVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  95. }
  96. }
  97. if (string.Equals(dic.Key, "炒锅3"))
  98. {
  99. foreach (var item in dic.Value)
  100. {
  101. FryThreeVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  102. }
  103. }
  104. if (string.Equals(dic.Key, "炒锅4"))
  105. {
  106. foreach (var item in dic.Value)
  107. {
  108. FryFourVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  109. }
  110. }
  111. if (string.Equals(dic.Key, "炒锅5"))
  112. {
  113. foreach (var item in dic.Value)
  114. {
  115. FryFiveVariables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  116. }
  117. }
  118. }
  119. }
  120. IsConfig = true;
  121. }
  122. catch (Exception)
  123. {
  124. IsConfig = false;
  125. //throw;
  126. }
  127. }
  128. else
  129. {
  130. IsConfig = false;
  131. }
  132. //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号
  133. //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号
  134. //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号
  135. //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态
  136. //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行
  137. //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号
  138. //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号
  139. //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV
  140. //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV
  141. //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行
  142. //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号
  143. //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号
  144. //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV
  145. //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号
  146. //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号
  147. }
  148. public void Connect()
  149. {
  150. if (IsConfig)
  151. {
  152. Json<DeviceManage>.Read();
  153. DeviceManage devices = Json<DeviceManage>.Data;
  154. if (devices != null)
  155. {
  156. if (devices.Devices.Count > 0)
  157. {
  158. for (int i = 0; i < devices.Devices.Count; i++)
  159. {
  160. string Ip = devices.Devices[i].Ip;
  161. string Port = devices.Devices[i].Port;
  162. string DeviceName = devices.Devices[i].DeviceName;
  163. switch (DeviceName)
  164. {
  165. case "滚筒输送线": Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  166. case "炒锅1": Task.Run(() => { fryOneModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  167. case "炒锅2": Task.Run(() => { fryTwoModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  168. case "炒锅3": Task.Run(() => { fryThreeModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  169. case "炒锅4": Task.Run(() => { fryFourModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  170. case "炒锅5": Task.Run(() => { fryFiveModbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); }); break;
  171. }
  172. }
  173. // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.RunLog("PLC连接成功"); })); });
  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. while (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. Thread.Sleep(50);
  201. }
  202. //炒锅1
  203. FryOneConnected = fryOneModbus.Connected;
  204. while (FryOneConnected)
  205. {
  206. foreach (var item in FryOneVariables)
  207. {
  208. var res = fryOneModbus.Read(item.Address, item.Length);//读取plc数据
  209. if (FryOneData.ContainsKey(item.Address))
  210. {
  211. FryOneData[item.Address] = res;
  212. }
  213. else
  214. {
  215. FryOneData.TryAdd(item.Address, res);
  216. }
  217. }
  218. Thread.Sleep(50);
  219. }
  220. //炒锅2
  221. FryTwoConnected = fryTwoModbus.Connected;
  222. while (FryTwoConnected)
  223. {
  224. foreach (var item in FryTwoVariables)
  225. {
  226. var res = fryTwoModbus.Read(item.Address, item.Length);//读取plc数据
  227. if (FryTwoData.ContainsKey(item.Address))
  228. {
  229. FryTwoData[item.Address] = res;
  230. }
  231. else
  232. {
  233. FryTwoData.TryAdd(item.Address, res);
  234. }
  235. }
  236. Thread.Sleep(50);
  237. }
  238. //炒锅3
  239. FryThreeConnected = fryThreeModbus.Connected;
  240. while (FryThreeConnected)
  241. {
  242. foreach (var item in FryThreeVariables)
  243. {
  244. var res = fryThreeModbus.Read(item.Address, item.Length);//读取plc数据
  245. if (FryThreeData.ContainsKey(item.Address))
  246. {
  247. FryThreeData[item.Address] = res;
  248. }
  249. else
  250. {
  251. FryThreeData.TryAdd(item.Address, res);
  252. }
  253. }
  254. Thread.Sleep(50);
  255. }
  256. //炒锅4
  257. FryFourConnected = fryFourModbus.Connected;
  258. while (FryFourConnected)
  259. {
  260. foreach (var item in FryFourVariables)
  261. {
  262. var res = fryFourModbus.Read(item.Address, item.Length);//读取plc数据
  263. if (FryFourData.ContainsKey(item.Address))
  264. {
  265. FryFourData[item.Address] = res;
  266. }
  267. else
  268. {
  269. FryFourData.TryAdd(item.Address, res);
  270. }
  271. }
  272. Thread.Sleep(50);
  273. }
  274. //炒锅5
  275. FryFiveConnected = fryFiveModbus.Connected;
  276. while (FryFiveConnected)
  277. {
  278. foreach (var item in FryFiveVariables)
  279. {
  280. var res = fryFiveModbus.Read(item.Address, item.Length);//读取plc数据
  281. if (FryFiveData.ContainsKey(item.Address))
  282. {
  283. FryFiveData[item.Address] = res;
  284. }
  285. else
  286. {
  287. FryFiveData.TryAdd(item.Address, res);
  288. }
  289. }
  290. Thread.Sleep(50);
  291. }
  292. Thread.Sleep(500);
  293. }), $"PLC实时数据读取线程");
  294. }
  295. }
  296. public void WritePlcData(string address, ushort value)
  297. {
  298. lock (this)
  299. {
  300. modbus.Write(address, value);
  301. }
  302. }
  303. public ConcurrentDictionary<string, object> GetAllData()
  304. {
  305. return Data;
  306. }
  307. public ConcurrentDictionary<string, object> GetFryOneData()
  308. {
  309. return FryOneData;
  310. }
  311. public ConcurrentDictionary<string, object> GetFryTwoData()
  312. {
  313. return FryTwoData;
  314. }
  315. public ConcurrentDictionary<string, object> GetFryThreeData()
  316. {
  317. return FryThreeData;
  318. }
  319. public ConcurrentDictionary<string, object> GetFryFourData()
  320. {
  321. return FryFourData;
  322. }
  323. public ConcurrentDictionary<string, object> GetFryFiveData()
  324. {
  325. return FryFiveData;
  326. }
  327. }
  328. }