PlcCommunManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //PLC参数管理类
  2. using MvvmScaffoldFrame48.DLL.AlarmTools;
  3. using MvvmScaffoldFrame48.DLL.LogTools;
  4. using MvvmScaffoldFrame48.Model.StorageModel.PlcParameter;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace MvvmScaffoldFrame48.DLL.CommunicationTools
  12. {
  13. public class PlcCommunManager
  14. {
  15. #region 变量
  16. private bool isConnect = false;
  17. public bool IsConnect
  18. {
  19. get
  20. {
  21. return isConnect ? modbusTcpClient.IsTcpClientConnected() : isConnect;
  22. }
  23. }
  24. private string IpAddress = "127.0.0.1";
  25. #endregion
  26. #region 实例
  27. public static PlcCommunManager Instance => _instance;
  28. private static PlcCommunManager _instance = new PlcCommunManager();
  29. // 寄存器位掩码
  30. private static readonly ConcurrentDictionary<int, ushort> BitMasks = new ConcurrentDictionary<int, ushort>
  31. {
  32. [0] = (ushort)(1 << 0),
  33. [1] = (ushort)(1 << 1),
  34. [2] = (ushort)(1 << 2),
  35. [3] = (ushort)(1 << 3),
  36. [4] = (ushort)(1 << 4),
  37. [5] = (ushort)(1 << 5),
  38. [6] = (ushort)(1 << 6),
  39. [7] = (ushort)(1 << 7),
  40. [8] = (ushort)(1 << 8),
  41. [9] = (ushort)(1 << 9),
  42. [10] = (ushort)(1 << 10),
  43. [11] = (ushort)(1 << 11),
  44. [12] = (ushort)(1 << 12),
  45. [13] = (ushort)(1 << 13),
  46. [14] = (ushort)(1 << 14),
  47. [15] = (ushort)(1 << 15),
  48. };
  49. // 寄存器信息
  50. public static readonly ConcurrentDictionary<string, PlcParameterModel> RegisterMap = new ConcurrentDictionary<string, PlcParameterModel>
  51. {
  52. ["test1"] = new PlcValueBitParameterModel(0, 0),
  53. ["test2"] = new PlcInt32ParameterModel(1),
  54. ["test3"] = new PlcDouble64ParameterModel(3)
  55. };
  56. private ModbusTcpClient modbusTcpClient = new ModbusTcpClient();
  57. #endregion
  58. #region 构造方法
  59. private PlcCommunManager()
  60. {
  61. ConnectModbus(IpAddress);
  62. }
  63. #endregion
  64. #region 私有方法
  65. private void ConnectModbus(string ipAddress)
  66. {
  67. int i = 10;
  68. while (!modbusTcpClient.Connect(ipAddress) && i > 0)
  69. {
  70. isConnect = false;
  71. //SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败, $"Modbus通讯连接失败,目标IP:{ipAddress}", "DLL:MainThreadClass-ConnectModbus");
  72. SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败,
  73. $"PLC communication connection failed, target IP:{ipAddress}",
  74. $"PLC通讯连接失败, IP地址:{ipAddress}",
  75. "DLL:MainThreadClass-ConnectModbus");
  76. i--;
  77. Task.Delay(1000);
  78. }
  79. if (modbusTcpClient.Connect(ipAddress))
  80. {
  81. SystemAlarm.AlarmCancel(AlarmMessageList.PLC通讯连接失败);
  82. TxtLog.log("PLC通讯成功", 6);
  83. //FaultLog.RecordLogMessage($"Modbus通讯连接成功,目标IP:{ipAddress}", 6);
  84. isConnect = true;
  85. }
  86. }
  87. #endregion
  88. #region 共有方法
  89. /// <summary>
  90. /// 根据参数模型读取数据
  91. /// </summary>
  92. public object ReadParameter(PlcParameterModel param)
  93. {
  94. // 1. 根据 Address 和 Length 从 PLC/Modbus 读取原始字节数组或寄存器数组
  95. // byte[] rawData = ModbusClient.ReadRegisters(param.Address, param.Length);
  96. switch (param.Type)
  97. {
  98. case PlcDataType.ValueBit: // ValueBit
  99. if (param is PlcValueBitParameterModel bitParam)
  100. {
  101. // 读取一个字节的位,并根据 bitAddress 提取特定位
  102. // return GetBitFromByte(rawData, bitParam.bitAddress);
  103. return true; // 示例返回
  104. }
  105. break;
  106. case PlcDataType.Int32: // Int32
  107. if (param is PlcInt32ParameterModel intParam)
  108. {
  109. // 读取2个寄存器 (4字节),转换为 Int32
  110. // int value = BitConverter.ToInt32(rawData, 0);
  111. // 可选:进行范围检查
  112. // if (value > intParam.MaxValue || value < intParam.MinValue) { ... }
  113. return 123; // 示例返回
  114. }
  115. break;
  116. case PlcDataType.Double: // Double
  117. if (param is PlcDouble64ParameterModel doubleParam)
  118. {
  119. // 读取4个寄存器 (8字节),转换为 Double
  120. // double value = BitConverter.ToDouble(rawData, 0);
  121. return 36.5; // 示例返回
  122. }
  123. break;
  124. }
  125. return null;
  126. }
  127. #endregion
  128. }
  129. }