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

110 lines
4.6 KiB

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