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

141 lines
6.0 KiB

  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.Message;
  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();
  22. private string Ip { get; set; }
  23. private string Port { get; set; }
  24. public bool Connected { get; set; }
  25. private string DeviceName { get; set; }
  26. public ConcurrentDictionary<string, object> Data { get; set; } = new ConcurrentDictionary<string, object>();
  27. public ObservableCollection<PlcVariableModel> Variables { get; set; } = new ObservableCollection<PlcVariableModel>();
  28. public DeviceOperate()
  29. {
  30. Init();
  31. Connect();
  32. ReadData();
  33. }
  34. public void Init()
  35. {
  36. if (Variables.Count > 0)
  37. {
  38. Variables.Clear();
  39. }
  40. Json<PlcVariableInfoManage>.Read();
  41. if (Json<PlcVariableInfoManage>.Data.VariablesInfo.Count > 0)
  42. {
  43. try
  44. {
  45. foreach (var item in Json<PlcVariableInfoManage>.Data.VariablesInfo)
  46. {
  47. Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
  48. }
  49. IsConfig = true;
  50. }
  51. catch (Exception)
  52. {
  53. IsConfig = false;
  54. //throw;
  55. }
  56. }
  57. else
  58. {
  59. IsConfig= false;
  60. }
  61. //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号
  62. //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号
  63. //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号
  64. //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态
  65. //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行
  66. //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号
  67. //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号
  68. //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV
  69. //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV
  70. //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行
  71. //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号
  72. //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号
  73. //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV
  74. //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号
  75. //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号
  76. }
  77. public void Connect()
  78. {
  79. if (IsConfig)
  80. {
  81. Json<DeviceManage>.Read();
  82. DeviceManage devices = Json<DeviceManage>.Data;
  83. if (devices != null)
  84. {
  85. if (devices.Devices.Count > 0)
  86. {
  87. Ip = devices.Devices[0].Ip;
  88. Port = devices.Devices[0].Port;
  89. DeviceName = devices.Devices[0].DeviceName;
  90. Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); });
  91. // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.RunLog("PLC连接成功"); })); });
  92. }
  93. }
  94. }
  95. }
  96. public void ReadData()
  97. {
  98. if (IsConfig)
  99. {
  100. ThreadManage.GetInstance().StartLong(new Action(() =>
  101. {
  102. Connected = modbus.Connected;
  103. while (Connected)
  104. {
  105. foreach (var item in Variables)
  106. {
  107. var res = modbus.Read(item.Address, item.Length);//读取plc数据
  108. if (Data.ContainsKey(item.Address))
  109. {
  110. Data[item.Address] = res;
  111. }
  112. else
  113. {
  114. Data.TryAdd(item.Address, res);
  115. }
  116. }
  117. Thread.Sleep(500);
  118. }
  119. Thread.Sleep(1000);
  120. }), $"设备【{DeviceName}】PLC实时数据读取线程");
  121. }
  122. }
  123. public void WritePlcData(string address, ushort value)
  124. {
  125. lock (this)
  126. {
  127. modbus.Write(address, value);
  128. }
  129. }
  130. public ConcurrentDictionary<string, object> GetAllData()
  131. {
  132. return Data;
  133. }
  134. }
  135. }