//PLC参数管理类 using MvvmScaffoldFrame48.DLL.AlarmTools; using MvvmScaffoldFrame48.DLL.LogTools; using MvvmScaffoldFrame48.Model.StorageModel.PlcParameter; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MvvmScaffoldFrame48.DLL.CommunicationTools { public class PlcCommunManager { #region 变量 private bool isConnect = false; public bool IsConnect { get { return isConnect ? modbusTcpClient.IsTcpClientConnected() : isConnect; } } private string IpAddress = "127.0.0.1"; #endregion #region 实例 public static PlcCommunManager Instance => _instance; private static PlcCommunManager _instance = new PlcCommunManager(); // 寄存器位掩码 private static readonly ConcurrentDictionary BitMasks = new ConcurrentDictionary { [0] = (ushort)(1 << 0), [1] = (ushort)(1 << 1), [2] = (ushort)(1 << 2), [3] = (ushort)(1 << 3), [4] = (ushort)(1 << 4), [5] = (ushort)(1 << 5), [6] = (ushort)(1 << 6), [7] = (ushort)(1 << 7), [8] = (ushort)(1 << 8), [9] = (ushort)(1 << 9), [10] = (ushort)(1 << 10), [11] = (ushort)(1 << 11), [12] = (ushort)(1 << 12), [13] = (ushort)(1 << 13), [14] = (ushort)(1 << 14), [15] = (ushort)(1 << 15), }; // 寄存器信息 public static readonly ConcurrentDictionary RegisterMap = new ConcurrentDictionary { ["test1"] = new PlcValueBitParameterModel(0, 0), ["test2"] = new PlcInt32ParameterModel(1), ["test3"] = new PlcDouble64ParameterModel(3) }; private ModbusTcpClient modbusTcpClient = new ModbusTcpClient(); #endregion #region 构造方法 private PlcCommunManager() { ConnectModbus(IpAddress); } #endregion #region 私有方法 private void ConnectModbus(string ipAddress) { int i = 10; while (!modbusTcpClient.Connect(ipAddress) && i > 0) { isConnect = false; //SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败, $"Modbus通讯连接失败,目标IP:{ipAddress}", "DLL:MainThreadClass-ConnectModbus"); SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败, $"PLC communication connection failed, target IP:{ipAddress}", $"PLC通讯连接失败, IP地址:{ipAddress}", "DLL:MainThreadClass-ConnectModbus"); i--; Task.Delay(1000); } if (modbusTcpClient.Connect(ipAddress)) { SystemAlarm.AlarmCancel(AlarmMessageList.PLC通讯连接失败); TxtLog.log("PLC通讯成功", 6); //FaultLog.RecordLogMessage($"Modbus通讯连接成功,目标IP:{ipAddress}", 6); isConnect = true; } } #endregion #region 共有方法 /// /// 根据参数模型读取数据 /// public object ReadParameter(PlcParameterModel param) { // 1. 根据 Address 和 Length 从 PLC/Modbus 读取原始字节数组或寄存器数组 // byte[] rawData = ModbusClient.ReadRegisters(param.Address, param.Length); switch (param.Type) { case PlcDataType.ValueBit: // ValueBit if (param is PlcValueBitParameterModel bitParam) { // 读取一个字节的位,并根据 bitAddress 提取特定位 // return GetBitFromByte(rawData, bitParam.bitAddress); return true; // 示例返回 } break; case PlcDataType.Int32: // Int32 if (param is PlcInt32ParameterModel intParam) { // 读取2个寄存器 (4字节),转换为 Int32 // int value = BitConverter.ToInt32(rawData, 0); // 可选:进行范围检查 // if (value > intParam.MaxValue || value < intParam.MinValue) { ... } return 123; // 示例返回 } break; case PlcDataType.Double: // Double if (param is PlcDouble64ParameterModel doubleParam) { // 读取4个寄存器 (8字节),转换为 Double // double value = BitConverter.ToDouble(rawData, 0); return 36.5; // 示例返回 } break; } return null; } #endregion } }