|
@@ -3,6 +3,7 @@ using MvvmScaffoldFrame48.DLL.AlarmTools;
|
|
|
using MvvmScaffoldFrame48.DLL.LogTools;
|
|
using MvvmScaffoldFrame48.DLL.LogTools;
|
|
|
using MvvmScaffoldFrame48.Model.StorageModel.PlcParameter;
|
|
using MvvmScaffoldFrame48.Model.StorageModel.PlcParameter;
|
|
|
using System;
|
|
using System;
|
|
|
|
|
+using System.Collections.Concurrent;
|
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
using System.Text;
|
|
@@ -12,31 +13,7 @@ namespace MvvmScaffoldFrame48.DLL.CommunicationTools
|
|
|
{
|
|
{
|
|
|
public class PlcCommunManager
|
|
public class PlcCommunManager
|
|
|
{
|
|
{
|
|
|
- private static readonly Dictionary<int, ushort> BitMasks = new Dictionary<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)},
|
|
|
|
|
- };
|
|
|
|
|
- private static readonly Dictionary<string, PlcParameterModel> RegisterMap = new Dictionary<string, PlcParameterModel>
|
|
|
|
|
- {
|
|
|
|
|
- {"test1",new PlcValueBitParameterModel(0,0) }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
|
|
+ #region 变量
|
|
|
private bool isConnect = false;
|
|
private bool isConnect = false;
|
|
|
public bool IsConnect
|
|
public bool IsConnect
|
|
|
{
|
|
{
|
|
@@ -46,13 +23,51 @@ namespace MvvmScaffoldFrame48.DLL.CommunicationTools
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public ModbusTcpClient modbusTcpClient = new ModbusTcpClient();
|
|
|
|
|
|
|
+ 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
|
|
|
|
|
|
|
|
- public PlcCommunManager(string IpAddress)
|
|
|
|
|
|
|
+ #region 构造方法
|
|
|
|
|
+ private PlcCommunManager()
|
|
|
{
|
|
{
|
|
|
ConnectModbus(IpAddress);
|
|
ConnectModbus(IpAddress);
|
|
|
}
|
|
}
|
|
|
- public void ConnectModbus(string ipAddress)
|
|
|
|
|
|
|
+ #endregion
|
|
|
|
|
+
|
|
|
|
|
+ #region 私有方法
|
|
|
|
|
+ private void ConnectModbus(string ipAddress)
|
|
|
{
|
|
{
|
|
|
int i = 10;
|
|
int i = 10;
|
|
|
while (!modbusTcpClient.Connect(ipAddress) && i > 0)
|
|
while (!modbusTcpClient.Connect(ipAddress) && i > 0)
|
|
@@ -74,5 +89,54 @@ namespace MvvmScaffoldFrame48.DLL.CommunicationTools
|
|
|
isConnect = true;
|
|
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
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|