iotgateway/Plugins/Drivers/DriverModbusMaster/ModbusMaster.cs

563 lines
22 KiB
C#
Raw Normal View History

2022-08-08 07:15:09 +00:00
using Microsoft.Extensions.Logging;
using Modbus.Device;
using Modbus.Serial;
2021-12-12 06:55:48 +00:00
using PluginInterface;
using System.IO.Ports;
2021-12-12 06:55:48 +00:00
using System.Net.Sockets;
2022-07-17 10:03:15 +00:00
using System.Text;
2021-12-12 06:55:48 +00:00
namespace DriverModbusMaster
2021-12-12 06:55:48 +00:00
{
[DriverSupported("ModbusTCP")]
[DriverSupported("ModbusUDP")]
[DriverSupported("ModbusRtu")]
[DriverSupported("ModbusAscii")]
2022-08-10 08:52:41 +00:00
[DriverInfo("ModbusMaster", "V1.1.0", "Copyright IoTGateway© 2022-8-6")]
public class ModbusMaster : IDriver
2021-12-12 06:55:48 +00:00
{
private TcpClient? _tcpClient;
private UdpClient? _udpClient;
private SerialPort? _serialPort;
private Modbus.Device.ModbusMaster? _master;
private SerialPortAdapter? _adapter;
public ILogger _logger { get; set; }
2022-08-08 07:15:09 +00:00
private readonly string _device;
2022-08-10 08:52:41 +00:00
2021-12-12 06:55:48 +00:00
#region
2022-08-10 08:52:41 +00:00
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
2021-12-12 06:55:48 +00:00
2022-08-10 08:52:41 +00:00
[ConfigParameter("PLC类型")] public PlcType PlcType { get; set; } = PlcType.S71200;
2021-12-12 06:55:48 +00:00
2022-08-10 08:52:41 +00:00
[ConfigParameter("主站类型")] public MasterType MasterType { get; set; } = MasterType.Tcp;
2022-08-10 08:52:41 +00:00
[ConfigParameter("IP地址")] public string IpAddress { get; set; } = "127.0.0.1";
2021-12-12 06:55:48 +00:00
2022-08-10 08:52:41 +00:00
[ConfigParameter("端口号")] public int Port { get; set; } = 502;
2021-12-12 06:55:48 +00:00
2022-08-10 08:52:41 +00:00
[ConfigParameter("串口名")] public string PortName { get; set; } = "COM1";
2022-08-10 08:52:41 +00:00
[ConfigParameter("波特率")] public int BaudRate { get; set; } = 9600;
2022-08-10 08:52:41 +00:00
[ConfigParameter("数据位")] public int DataBits { get; set; } = 8;
2022-08-10 08:52:41 +00:00
[ConfigParameter("校验位")] public Parity Parity { get; set; } = Parity.None;
2022-08-10 08:52:41 +00:00
[ConfigParameter("停止位")] public StopBits StopBits { get; set; } = StopBits.One;
2022-08-10 08:52:41 +00:00
[ConfigParameter("从站号")] public byte SlaveAddress { get; set; } = 1;
2021-12-12 06:55:48 +00:00
2022-08-10 08:52:41 +00:00
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
2021-12-12 06:55:48 +00:00
2022-08-10 08:52:41 +00:00
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
2021-12-12 06:55:48 +00:00
#endregion
2022-08-08 07:15:09 +00:00
public ModbusMaster(string device, ILogger logger)
2021-12-12 06:55:48 +00:00
{
_device = device;
_logger = logger;
2021-12-12 06:55:48 +00:00
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],Create()");
}
2021-12-12 06:55:48 +00:00
public bool IsConnected
{
get
{
2022-08-10 08:52:41 +00:00
switch (MasterType)
2021-12-18 11:13:41 +00:00
{
2022-08-10 08:52:41 +00:00
case MasterType.Tcp:
case MasterType.RtuOnTcp:
case MasterType.AsciiOnTcp:
return _tcpClient != null && _master != null && _tcpClient.Connected;
2022-08-10 08:52:41 +00:00
case MasterType.Udp:
case MasterType.RtuOnUdp:
case MasterType.AsciiOnUdp:
return _udpClient != null && _master != null && _udpClient.Client.Connected;
2022-08-10 08:52:41 +00:00
case MasterType.Rtu:
case MasterType.Ascii:
return _serialPort != null && _master != null && _serialPort.IsOpen;
2021-12-18 11:13:41 +00:00
default:
return false;
}
2021-12-12 06:55:48 +00:00
}
}
public bool Connect()
{
try
{
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],Connect()");
2022-08-10 08:52:41 +00:00
switch (MasterType)
{
2022-08-10 08:52:41 +00:00
case MasterType.Tcp:
_tcpClient = new TcpClient(IpAddress, Port);
_tcpClient.ReceiveTimeout = Timeout;
_tcpClient.SendTimeout = Timeout;
_master = ModbusIpMaster.CreateIp(_tcpClient);
break;
2022-08-10 08:52:41 +00:00
case MasterType.Udp:
_udpClient = new UdpClient(IpAddress, Port);
_udpClient.Client.ReceiveTimeout = Timeout;
_udpClient.Client.SendTimeout = Timeout;
_master = ModbusIpMaster.CreateIp(_udpClient);
break;
2022-08-10 08:52:41 +00:00
case MasterType.Rtu:
_serialPort = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
_serialPort.ReadTimeout = Timeout;
_serialPort.WriteTimeout = Timeout;
_serialPort.Open();
_adapter = new SerialPortAdapter(_serialPort);
_master = ModbusSerialMaster.CreateRtu(_adapter);
break;
2022-08-10 08:52:41 +00:00
case MasterType.RtuOnTcp:
_tcpClient = new TcpClient(IpAddress, Port);
_tcpClient.ReceiveTimeout = Timeout;
_tcpClient.SendTimeout = Timeout;
_master = ModbusSerialMaster.CreateRtu(_tcpClient);
break;
2022-08-10 08:52:41 +00:00
case MasterType.RtuOnUdp:
_udpClient = new UdpClient(IpAddress, Port);
_udpClient.Client.ReceiveTimeout = Timeout;
_udpClient.Client.SendTimeout = Timeout;
_master = ModbusSerialMaster.CreateRtu(_udpClient);
break;
2022-08-10 08:52:41 +00:00
case MasterType.Ascii:
_serialPort = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
_serialPort.ReadTimeout = Timeout;
_serialPort.WriteTimeout = Timeout;
_serialPort.Open();
_adapter = new SerialPortAdapter(_serialPort);
_master = ModbusSerialMaster.CreateAscii(_adapter);
break;
2022-08-10 08:52:41 +00:00
case MasterType.AsciiOnTcp:
_tcpClient = new TcpClient(IpAddress, Port);
_tcpClient.ReceiveTimeout = Timeout;
_tcpClient.SendTimeout = Timeout;
_master = ModbusSerialMaster.CreateAscii(_tcpClient);
break;
2022-08-10 08:52:41 +00:00
case MasterType.AsciiOnUdp:
_udpClient = new UdpClient(IpAddress, Port);
_udpClient.Client.ReceiveTimeout = Timeout;
_udpClient.Client.SendTimeout = Timeout;
_master = ModbusSerialMaster.CreateAscii(_udpClient);
break;
}
2022-08-10 08:52:41 +00:00
_master.Transport.ReadTimeout = Timeout;
_master.Transport.WriteTimeout = Timeout;
2021-12-12 06:55:48 +00:00
}
2021-12-18 11:13:41 +00:00
catch (Exception ex)
2021-12-12 06:55:48 +00:00
{
2022-08-08 07:15:09 +00:00
_logger.LogError($"Device:[{_device}],Connect(),Error", ex);
2021-12-12 06:55:48 +00:00
return false;
}
2022-08-10 08:52:41 +00:00
2021-12-12 06:55:48 +00:00
return IsConnected;
}
public bool Close()
{
try
{
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],Close()");
_tcpClient?.Close();
_udpClient?.Close();
_serialPort?.Close();
2021-12-12 06:55:48 +00:00
return !IsConnected;
}
catch (Exception ex)
2021-12-12 06:55:48 +00:00
{
2022-08-08 07:15:09 +00:00
_logger.LogError($"Device:[{_device}],Close(),Error", ex);
2021-12-12 06:55:48 +00:00
return false;
}
}
public void Dispose()
{
try
{
_tcpClient?.Dispose();
_udpClient?.Dispose();
_serialPort?.Dispose();
_master?.Dispose();
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],Dispose()");
2021-12-12 06:55:48 +00:00
}
catch (Exception ex)
2021-12-12 06:55:48 +00:00
{
2022-08-08 07:15:09 +00:00
_logger.LogError($"Device:[{_device}],Dispose(),Error", ex);
2021-12-12 06:55:48 +00:00
}
}
[Method("功能码:03", description: "ReadHoldingRegisters读保持寄存器")]
public DriverReturnValueModel ReadHoldingRegisters(DriverAddressIoArgModel ioarg)
{
DriverReturnValueModel ret = new();
try
2021-12-12 06:55:48 +00:00
{
if (IsConnected)
ret = ReadRegistersBuffers(3, ioarg);
else
2021-12-12 06:55:48 +00:00
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "TCP连接异常";
2021-12-12 06:55:48 +00:00
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],ReadHoldingRegisters(),Error", ex);
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:52:41 +00:00
return ret;
2021-12-12 06:55:48 +00:00
}
[Method("功能码:04", description: "ReadHoldingRegisters读输入寄存器")]
public DriverReturnValueModel ReadInputRegisters(DriverAddressIoArgModel ioarg)
{
DriverReturnValueModel ret = new();
try
2021-12-12 06:55:48 +00:00
{
if (IsConnected)
ret = ReadRegistersBuffers(4, ioarg);
else
2021-12-12 06:55:48 +00:00
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "TCP连接异常";
2021-12-12 06:55:48 +00:00
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],ReadInputRegisters(),Error", ex);
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:52:41 +00:00
return ret;
2021-12-12 06:55:48 +00:00
}
[Method("功能码:01", description: "ReadCoil读线圈")]
public DriverReturnValueModel ReadCoil(DriverAddressIoArgModel ioarg)
{
DriverReturnValueModel ret = new();
try
{
if (IsConnected)
{
var retBool = _master.ReadCoils(SlaveAddress, ushort.Parse(ioarg.Address), 1)[0];
if (ioarg.ValueType == DataTypeEnum.Bit)
{
if (retBool)
ret.Value = 1;
else
ret.Value = 0;
}
else
ret.Value = retBool;
2022-08-10 08:52:41 +00:00
ret.StatusType = VaribaleStatusTypeEnum.Good;
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "TCP连接异常";
2022-04-13 09:01:24 +00:00
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],ReadCoil(),Error", ex);
}
2022-08-10 08:52:41 +00:00
return ret;
}
[Method("功能码:02", description: "ReadInput读输入")]
public DriverReturnValueModel ReadInput(DriverAddressIoArgModel ioarg)
{
DriverReturnValueModel ret = new();
try
{
if (IsConnected)
{
var retBool = _master.ReadInputs(SlaveAddress, ushort.Parse(ioarg.Address), 1)[0];
if (ioarg.ValueType == DataTypeEnum.Bit)
{
if (retBool)
ret.Value = 1;
else
ret.Value = 0;
}
else
ret.Value = retBool;
2022-08-10 08:52:41 +00:00
ret.StatusType = VaribaleStatusTypeEnum.Good;
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "TCP连接异常";
2022-04-13 09:01:24 +00:00
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],ReadInput(),Error", ex);
}
2022-08-10 08:52:41 +00:00
return ret;
}
2021-12-12 06:55:48 +00:00
[Method("Read方法样例", description: "Read方法样例描述")]
public DriverReturnValueModel Read(DriverAddressIoArgModel ioarg)
{
DriverReturnValueModel ret = new DriverReturnValueModel
{
Message = "",
StatusType = VaribaleStatusTypeEnum.Good,
Value = $"{DeviceId} {DateTime.Now.ToString("O")} Read {ioarg.Address}"
};
return ret;
}
//读功能码03、或04
2022-08-10 08:52:41 +00:00
private DriverReturnValueModel ReadRegistersBuffers(byte funCode, DriverAddressIoArgModel ioarg)
2021-12-12 06:55:48 +00:00
{
DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good };
2021-12-18 11:13:41 +00:00
if (!IsConnected)
2021-12-12 06:55:48 +00:00
ret.StatusType = VaribaleStatusTypeEnum.Bad;
else
{
2022-07-17 10:03:15 +00:00
ushort startAddress, count;
ret = AnalyzeAddress(ioarg, out startAddress, out count);
if (ret.StatusType != VaribaleStatusTypeEnum.Good)
2022-07-17 10:03:15 +00:00
return ret;
try
2021-12-12 06:55:48 +00:00
{
2022-07-17 10:03:15 +00:00
var rawBuffers = new ushort[] { };
2022-08-10 08:52:41 +00:00
if (funCode == 3)
rawBuffers = _master.ReadHoldingRegisters(SlaveAddress, startAddress, count);
2022-08-10 08:52:41 +00:00
else if (funCode == 4)
rawBuffers = _master.ReadInputRegisters(SlaveAddress, startAddress, count);
2022-07-17 10:03:15 +00:00
var retBuffers = ChangeBuffersOrder(rawBuffers, ioarg.ValueType);
if (ioarg.ValueType == DataTypeEnum.AsciiString)
retBuffers = rawBuffers;
if (ioarg.ValueType.ToString().Contains("Uint16"))
ret.Value = retBuffers[0];
else if (ioarg.ValueType.ToString().Contains("Int16"))
ret.Value = (short)retBuffers[0];
else if (ioarg.ValueType.ToString().Contains("Uint32"))
ret.Value = (uint)(retBuffers[0] << 16) + retBuffers[1];
2022-07-17 10:03:15 +00:00
else if (ioarg.ValueType.ToString().Contains("Int32"))
ret.Value = (retBuffers[0] << 16) + retBuffers[1];
2022-07-17 10:03:15 +00:00
else if (ioarg.ValueType.ToString().Contains("Float"))
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:52:41 +00:00
var bytes = new[]
{
(byte)(retBuffers[1] & 0xff), (byte)((retBuffers[1] >> 8) & 0xff),
(byte)(retBuffers[0] & 0xff), (byte)((retBuffers[0] >> 8) & 0xff)
};
2022-07-17 10:03:15 +00:00
ret.Value = BitConverter.ToSingle(bytes, 0);
2021-12-12 06:55:48 +00:00
}
2022-07-17 10:03:15 +00:00
else if (ioarg.ValueType.ToString().Contains("AsciiString"))
2021-12-12 06:55:48 +00:00
{
var str = Encoding.ASCII.GetString(GetBytes(retBuffers).ToArray());
2022-07-17 10:03:15 +00:00
if (str.Contains('\0'))
str = str.Split('\0')[0];
ret.Value = str;
2021-12-12 06:55:48 +00:00
}
2022-07-17 10:03:15 +00:00
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = ex.Message;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],ReadRegistersBuffers(),Error", ex);
2021-12-12 06:55:48 +00:00
}
2022-07-17 10:03:15 +00:00
}
2022-08-10 08:52:41 +00:00
2021-12-12 06:55:48 +00:00
return ret;
}
private ushort GetModbusReadCount(uint functionCode, DataTypeEnum dataType)
{
if (dataType.ToString().Contains("32") || dataType.ToString().Contains("Float"))
return 2;
if (dataType.ToString().Contains("64") || dataType.ToString().Contains("Double"))
2021-12-12 06:55:48 +00:00
return 4;
return 1;
2021-12-12 06:55:48 +00:00
}
//预留了大小端转换的
private ushort[] ChangeBuffersOrder(ushort[] buffers, DataTypeEnum dataType)
{
var newBuffers = new ushort[buffers.Length];
if (dataType.ToString().Contains("32") || dataType.ToString().Contains("Float"))
{
2022-08-10 08:52:41 +00:00
var a = buffers[0] & 0xff00; //A
var b = buffers[0] & 0x00ff; //B
var c = buffers[1] & 0xff00; //C
var d = buffers[1] & 0x00ff; //D
2021-12-12 06:55:48 +00:00
if (dataType.ToString().Contains("_1"))
{
2022-08-10 08:52:41 +00:00
newBuffers[0] = (ushort)(a + b); //AB
newBuffers[1] = (ushort)(c + d); //CD
2021-12-12 06:55:48 +00:00
}
else if (dataType.ToString().Contains("_2"))
{
2022-08-10 08:52:41 +00:00
newBuffers[0] = (ushort)((a >> 8) + (b << 8)); //BA
newBuffers[1] = (ushort)((c >> 8) + (d << 8)); //DC
2021-12-12 06:55:48 +00:00
}
else if (dataType.ToString().Contains("_3"))
{
2022-08-10 08:52:41 +00:00
newBuffers[0] = (ushort)((c >> 8) + (d << 8)); //DC
newBuffers[1] = (ushort)((a >> 8) + (b << 8)); //BA
2021-12-12 06:55:48 +00:00
}
else
{
2022-08-10 08:52:41 +00:00
newBuffers[0] = (ushort)(c + d); //CD
newBuffers[1] = (ushort)(a + b); //AB
2021-12-12 06:55:48 +00:00
}
}
else if (dataType.ToString().Contains("64") || dataType.ToString().Contains("Double"))
{
if (dataType.ToString().Contains("_1"))
{
}
else
{
newBuffers[0] = buffers[3];
newBuffers[1] = buffers[2];
newBuffers[2] = buffers[1];
newBuffers[3] = buffers[0];
}
}
else
{
if (dataType.ToString().Contains("_1"))
{
var h8 = buffers[0] & 0xf0;
var l8 = buffers[0] & 0x0f;
newBuffers[0] = (ushort)(h8 >> 8 + l8 << 8);
}
else
newBuffers[0] = buffers[0];
}
2022-08-10 08:52:41 +00:00
2021-12-12 06:55:48 +00:00
return newBuffers;
}
2022-04-13 09:01:24 +00:00
2022-07-17 10:03:15 +00:00
private List<byte> GetBytes(ushort[] retBuffers)
{
List<byte> vs = new();
foreach (var retBuffer in retBuffers)
2022-07-17 10:03:15 +00:00
{
vs.Add((byte)(retBuffer & 0xFF));
vs.Add((byte)((retBuffer & 0xFF00) >> 8));
2022-07-17 10:03:15 +00:00
}
2022-08-10 08:52:41 +00:00
2022-07-17 10:03:15 +00:00
return vs;
}
2022-08-10 08:52:41 +00:00
private DriverReturnValueModel AnalyzeAddress(DriverAddressIoArgModel ioarg, out ushort startAddress,
out ushort readCount)
2022-07-17 10:03:15 +00:00
{
DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good };
try
{
if (ioarg.ValueType == DataTypeEnum.AsciiString)
{
2022-08-10 08:52:41 +00:00
startAddress = ushort.Parse(ioarg.Address.Split(',')[0]);
readCount = ushort.Parse(ioarg.Address.Split(',')[1]);
2022-07-17 10:03:15 +00:00
}
else
{
2022-08-10 08:52:41 +00:00
startAddress = ushort.Parse(ioarg.Address);
readCount = GetModbusReadCount(3, ioarg.ValueType);
2022-07-17 10:03:15 +00:00
}
2022-08-10 08:52:41 +00:00
return ret;
2022-07-17 10:03:15 +00:00
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.AddressError;
ret.Message = ex.Message;
2022-08-10 08:52:41 +00:00
startAddress = 0;
readCount = 0;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],AnalyzeAddress(),Error", ex);
2022-07-17 10:03:15 +00:00
return ret;
}
}
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg)
2022-04-13 09:01:24 +00:00
{
RpcResponse rpcResponse = new() { IsSuccess = false };
try
2022-04-13 09:01:24 +00:00
{
if (!IsConnected)
rpcResponse.Description = "设备连接已断开";
else
2022-04-13 09:01:24 +00:00
{
ushort address, count;
2022-08-10 08:52:41 +00:00
AnalyzeAddress(ioarg, out address, out count);
//功能码01
if (method == nameof(ReadCoil))
2022-04-13 09:01:24 +00:00
{
var value = ioarg.Value.ToString() == "1" || ioarg.Value.ToString().ToLower() == "true";
await _master.WriteSingleCoilAsync(SlaveAddress, address, value);
rpcResponse.IsSuccess = true;
return rpcResponse;
2022-04-13 09:01:24 +00:00
}
2022-08-10 08:52:41 +00:00
//功能码03
if (method == nameof(ReadHoldingRegisters))
{
ushort[] shortArray = new ushort[count];
switch (ioarg.ValueType)
{
case DataTypeEnum.AsciiString:
ModbusDataConvert.SetString(shortArray, 0, ioarg.Value.ToString());
await _master.WriteMultipleRegistersAsync(SlaveAddress, address, shortArray);
break;
case DataTypeEnum.Float:
float f = 0;
float.TryParse(ioarg.Value.ToString(), out f);
ModbusDataConvert.SetReal(shortArray, 0, f);
await _master.WriteMultipleRegistersAsync(SlaveAddress, address, shortArray);
break;
default:
2022-08-10 08:52:41 +00:00
await _master.WriteSingleRegisterAsync(SlaveAddress, address,
ushort.Parse(ioarg.Value.ToString()));
break;
}
2022-08-10 08:52:41 +00:00
rpcResponse.IsSuccess = true;
return rpcResponse;
}
2022-08-10 08:52:41 +00:00
rpcResponse.Description = $"不支持写入:{method}";
2022-04-13 09:01:24 +00:00
}
}
catch (Exception ex)
{
2022-08-10 08:52:41 +00:00
rpcResponse.Description = $"写入失败,[method]:{method},[ioarg]:{ioarg},[ex]:{ex}";
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],WriteAsync(),Error", ex);
}
2022-08-10 08:52:41 +00:00
return rpcResponse;
2022-04-13 09:01:24 +00:00
}
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:52:41 +00:00
}