本文主要介绍基于开源modbus库NModbus4的数据读写
添加引用
using Modbus.Device;
属性
private ModbusIpMaster _master;
private string _ipAddress;
private TcpClient _tcpClient;
///
/// 设备IP地址
///
public string IpAddress { get => _ipAddress; set => _ipAddress = value; }
方法
public override OperateResult Open()
{
try
{
_tcpClient = new TcpClient(IPAddress.Parse(_ipAddress).ToString(), 502);
_master = ModbusIpMaster.CreateIp(_tcpClient);
return OperateResult.CreateSuccessResult();
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
public override OperateResult Close()
{
try
{
_tcpClient.Close();
return OperateResult.CreateSuccessResult();
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
bool
数据public OperateResult ReadBool(ushort address)
{
try
{
Validate();
var buffer = _master.ReadInputs(address, 1);
return OperateResult.CreateSuccessResult(buffer[0]);
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
short
数据public OperateResult ReadInt16(ushort address)
{
try
{
Validate();
var buffer = _master.ReadHoldingRegisters(address, 1);
var value = buffer[0] <= short.MaxValue ? (short)buffer[0] : (short)(buffer[0] - ushort.MaxValue - 1);
return OperateResult.CreateSuccessResult(value);
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
int
数据public OperateResult ReadInt32(ushort address)
{
try
{
Validate();
var buffer = _master.ReadHoldingRegisters((ushort)(address * 2), 2);
var value = (buffer[1] << 16) + buffer[0];
return OperateResult.CreateSuccessResult(value);
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
float
数据public OperateResult ReadFloat(ushort address)
{
try
{
Validate();
var buffer = _master.ReadHoldingRegisters(address, 2);
var low = ((IEnumerable)BitConverter.GetBytes(buffer[0]));
var high = (IEnumerable)BitConverter.GetBytes(buffer[1]);
var value = BitConverter.ToSingle(low.Concat(high).ToArray(), 0);
//var value = ModbusUtility.GetSingle(buffer[1], buffer[0]);
return OperateResult.CreateSuccessResult(value);
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
double
数据public OperateResult ReadDouble(ushort address)
{
try
{
Validate();
var buffer = _master.ReadHoldingRegisters(address, 4);
var b0 = (IEnumerable)BitConverter.GetBytes(buffer[0]);
var b1 = (IEnumerable)BitConverter.GetBytes(buffer[1]);
var b2 = (IEnumerable)BitConverter.GetBytes(buffer[2]);
var b3 = (IEnumerable)BitConverter.GetBytes(buffer[3]);
var value = BitConverter.ToDouble(b0.Concat(b1).Concat(b2).Concat(b3).ToArray(), 0);
//var value = ModbusUtility.GetDouble(buffer[3], buffer[2], buffer[1], buffer[0]);
return OperateResult.CreateSuccessResult(value);
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
bool
数组数据 public OperateResult ReadBool(ushort address, ushort length)
{
try
{
Validate();
var buffer = _master.ReadInputs(address, length);
return OperateResult.CreateSuccessResult(buffer);
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
short
数组数据 public OperateResult ReadInt16(ushort address, ushort length)
{
try
{
Validate();
var buffer = _master.ReadHoldingRegisters(address, 1);
List values = new List();
for (int i = 0; i < length; i++)
{
var value = buffer[i] <= short.MaxValue ? (short)buffer[i] : (short)(buffer[i] - ushort.MaxValue - 1);
values.Add(value);
}
return OperateResult.CreateSuccessResult(values.ToArray());
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
int
数组数据 public OperateResult ReadInt32(ushort startAddress, ushort length)
{
try
{
Validate();
ushort address = (ushort)(startAddress * 2);
var buffer = _master.ReadHoldingRegisters(address, (ushort)(length * 2));
List values = new List();
for (int i = 0; i < length; i++)
{
var value = (buffer[i * 2 + 1] << 16) + buffer[i * 2];
values.Add(value);
}
return OperateResult.CreateSuccessResult(values.ToArray());
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
float
数组数据 public OperateResult ReadFloat(ushort address, ushort length)
{
try
{
Validate();
var buffer = _master.ReadHoldingRegisters(address, (ushort)(2 * length));
List values = new List();
for (int i = 0; i < length; i++)
{
var low = ((IEnumerable)BitConverter.GetBytes(buffer[i * 2]));
var high = (IEnumerable)BitConverter.GetBytes(buffer[i * 2 + 1]);
var value = BitConverter.ToSingle(low.Concat(high).ToArray(), 0);
//var value = ModbusUtility.GetSingle(buffer[1], buffer[0]);
values.Add(value);
}
return OperateResult.CreateSuccessResult(values.ToArray());
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}
double
数组数据 public OperateResult ReadDouble(ushort address, ushort length)
{
try
{
Validate();
var buffer = _master.ReadHoldingRegisters(address, (ushort)(4 * length));
List values = new List();
for (int i = 0; i < length; i++)
{
var b0 = (IEnumerable)BitConverter.GetBytes(buffer[4 * i]);
var b1 = (IEnumerable)BitConverter.GetBytes(buffer[4 * i + 1]);
var b2 = (IEnumerable)BitConverter.GetBytes(buffer[4 * i + 2]);
var b3 = (IEnumerable)BitConverter.GetBytes(buffer[4 * i + 3]);
var value = BitConverter.ToDouble(b0.Concat(b1).Concat(b2).Concat(b3).ToArray(), 0);
values.Add(value);
}
return OperateResult.CreateSuccessResult(values.ToArray());
}
catch (Exception ex)
{
return OperateResult.CreateFailedResult(ex.Message);
}
}