NModbus网口使用MODBUSTCP字符串、浮点数读写
引用NModbus
在NuGet搜索NModbus,添加引用。
封装ModbusTcp类
public class ModbusTCP
{
private ModbusFactory modbusFactory;
private IModbusMaster master;
private TcpClient tcpClient;
public string IPAdress { get; set; }
public int Port { get; set; }
public bool Connected
{
get => tcpClient.Connected;
}
public ModbusTCP(string ip, int port)
{
IPAdress = ip;
Port = port;
modbusFactory = new ModbusFactory();
tcpClient = new TcpClient(IPAdress, Port);
master = modbusFactory.CreateMaster(tcpClient);
master.Transport.ReadTimeout = 2000;
master.Transport.Retries = 10;
}
public bool[] ReadCoils(byte slaveAddress, ushort startAddress, ushort num)
{
return master.ReadCoils(slaveAddress, startAddress, num);
}
public bool[] ReadInputs(byte slaveAddress, ushort startAddress, ushort num)
{
return master.ReadInputs(slaveAddress, startAddress, num);
}
public ushort[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress, ushort num)
{
return master.ReadHoldingRegisters(slaveAddress, startAddress, num);
}
public ushort[] ReadInputRegisters(byte slaveAddress, ushort startAddress, ushort num)
{
return master.ReadInputRegisters(slaveAddress, startAddress, num);
}
public void WriteSingleCoil(byte slaveAddress, ushort startAddress, bool value)
{
master.WriteSingleCoil(slaveAddress, startAddress, value);
}
public void WriteSingleRegister(byte slaveAddress, ushort startAddress, ushort value)
{
master.WriteSingleRegister(slaveAddress, startAddress, value);
}
public void WriteMultipleCoils(byte slaveAddress, ushort startAddress, bool[] value)
{
master.WriteMultipleCoils(slaveAddress, startAddress, value);
}
public void WriteMultipleRegisters(byte slaveAddress, ushort startAddress, ushort[] value)
{
master.WriteMultipleRegisters(slaveAddress, startAddress, value);
}
}
连接测试
下载 Modbus Slave
链接:https://pan.baidu.com/s/1ydX_9KElkOTA7h-E3SIadw
提取码:7fh5
设置
连接
private ModbusTCP modbus;
private void ConnectExecute()
{
try
{
modbus = new ModbusTCP(IP, Port);
pollingTimer.Start();
}
catch (Exception ex)
{
Msg.Info(ex.Message);
}
}
写入测试
NModbus提供的对寄存器读写方法,只包括ushort类型,需要对ushort进行进行转换。
封装转换类型
public class MODBUS
{
/// <summary>
/// 赋值string
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
/// <returns></returns>
public static void SetString(ushort[] src, int start, string value)
{
byte[] bytesTemp = Encoding.UTF8.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytesTemp);
dest.CopyTo(src, start);
}
/// <summary>
/// 获取string
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string GetString(ushort[] src, int start, int len)
{
ushort[] temp = new ushort[len];
for (int i = 0; i < len; i++)
{
temp[i] = src[i + start];
}
byte[] bytesTemp = Ushorts2Bytes(temp);
string res = Encoding.UTF8.GetString(bytesTemp).Trim(new char[] { '\0' });
return res;
}
/// <summary>
/// 赋值Real类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
public static void SetReal(ushort[] src, int start, float value)
{
byte[] bytes = BitConverter.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytes);
dest.CopyTo(src, start);
}
/// <summary>
/// 获取float类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns></returns>
public static float GetReal(ushort[] src, int start)
{
ushort[] temp = new ushort[2];
for (int i = 0; i < 2; i++)
{
temp[i] = src[i + start];
}
byte[] bytesTemp = Ushorts2Bytes(temp);
float res = BitConverter.ToSingle(bytesTemp, 0);
return res;
}
/// <summary>
/// 赋值Short类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="value"></param>
public static void SetShort(ushort[] src, int start, short value)
{
byte[] bytes = BitConverter.GetBytes(value);
ushort[] dest = Bytes2Ushorts(bytes);
dest.CopyTo(src, start);
}
/// <summary>
/// 获取short类型数据
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <returns></returns>
public static short GetShort(ushort[] src, int start)
{
ushort[] temp = new ushort[1];
temp[0] = src[start];
byte[] bytesTemp = Ushorts2Bytes(temp);
short res = BitConverter.ToInt16(bytesTemp, 0);
return res;
}
public static bool[] GetBools(ushort[] src, int start, int num)
{
ushort[] temp = new ushort[num];
for (int i = start; i < start + num; i++)
{
temp[i] = src[i + start];
}
byte[] bytes = Ushorts2Bytes(temp);
bool[] res = Bytes2Bools(bytes);