| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //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<int, ushort> BitMasks = new ConcurrentDictionary<int, ushort>
- {
- [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<string, PlcParameterModel> RegisterMap = new ConcurrentDictionary<string, PlcParameterModel>
- {
- ["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 共有方法
- /// <summary>
- /// 根据参数模型读取数据
- /// </summary>
- 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
- }
- }
|