1.创建Modbus类,用于转换数据
internal 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>
/// 获取string
/// </summary>
/// <param name="src"></param>
/// <param name="start"></param>
/// <param name="len"></param>
/// <returns></returns>
public static string GetString1(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);
//8C-A6-DE-C5-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
string str = BitConverter.ToString(bytesTemp, 0);
string[] strs = str.Split('-');
string hexValue = "";
for (int i = strs.Length - 1; i >= 0; i--)
{
hexValue += strs[i];
}
if (hexValue != "")
return Convert.ToInt64(hexValue, 16).ToString();
else
return null;
}
/// <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);
return res;
}
private static bool[] Bytes2Bools(byte[] b)
{
bool[] array = new bool[8 * b.Length];
for (int i = 0; i < b.Length; i++)
{
for (int j = 0; j < 8; j++)
{
array[i * 8 + j] = (b[i] & 1) == 1;//判定byte的最后一位是否为1,若为1,则是true;否则是false
b[i] = (byte)(b[i] >> 1);//将byte右移一位
}
}
return array;
}
private static byte Bools2Byte(bool[] array)
{
if (array != null && array.Length > 0)
{
byte b = 0;
for (int i = 0; i < 8; i++)
{
if (array[i])
{
byte nn = (byte)(1 << i);//左移一位,相当于×2
b += nn;
}
}
return b;
}
return 0;
}
private static ushort[] Bytes2Ushorts(byte[] src, bool reverse = false)
{
int len = src.Length;
byte[] srcPlus = new byte[len + 1];
src.CopyTo(srcPlus, 0);
int count = len >> 1;
if (len % 2 != 0)
{
count += 1;
}
ushort[] dest = new ushort[count];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
}
}
return dest;
}
private static byte[] Ushorts2Bytes(ushort[] src, bool reverse = false)
{
int count = src.Length;
byte[] dest = new byte[count << 1];
if (reverse)
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 8);
dest[i * 2 + 1] = (byte)(src[i] >> 0);
}
}
else
{
for (int i = 0; i < count; i++)
{
dest[i * 2] = (byte)(src[i] >> 0);
dest[i * 2 + 1] = (byte)(src[i] >> 8);
}
}
return dest;
}
}
2.创建ModbusTCP类,用于通讯
internal class ModbusTCP
{
private ModbusFactory modbusFactory;
private IModbusMaster master;
private TcpClient tcpClient;
public byte SlaveID { get; set; } = 1;
public string IPAdress { get; set; }
public int Port { get; set; }
public bool Connected
{
get => tcpClient != null && tcpClient.Connected;
}
public ModbusTCP(string ip, int port, byte slaveId)
{
IPAdress = ip;
Port = port;
SlaveID = slaveId;
Connect();
}
public string Connect()
{
tcpClient?.Dispose();
master?.Dispose();
try
{
modbusFactory = new ModbusFactory();
tcpClient = new TcpClient(IPAdress, Port);
master = modbusFactory.CreateMaster(tcpClient);
master.Transport.ReadTimeout = 2000;
master.Transport.Retries = 10;
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
public bool[] ReadCoils(ushort startAddress, ushort num)
{
return master.ReadCoils(SlaveID, startAddress, num);
}
public bool[] ReadInputs(ushort startAddress, ushort num)
{
return master.ReadInputs(SlaveID, startAddress, num);
}
public ushort[] ReadHoldingRegisters(ushort startAddress, ushort num)
{
return master.ReadHoldingRegisters(SlaveID, startAddress, num);
}
public ushort[] ReadInputRegisters(ushort startAddress, ushort num)
{
return master.ReadInputRegisters(SlaveID, startAddress, num);
}
public void WriteSingleCoil(ushort startAddress, bool value)
{
master.WriteSingleCoil(SlaveID, startAddress, value);
}
public void WriteSingleRegister(ushort startAddress, ushort value)
{
master.WriteSingleRegister(SlaveID, startAddress, value);
}
public void WriteMultipleCoils(ushort startAddress, bool[] value)
{
master.WriteMultipleCoils(SlaveID, startAddress, value);
}
public void WriteMultipleRegisters(ushort startAddress, ushort[] value)
{
master.WriteMultipleRegisters(SlaveID, startAddress, value);
}
}
需添加引用为
using NModbus;
using System;
using System.Net.Sockets;
3.调用方法实现通讯(SlaveID默认为1)
ModbusTCP modbus = new ModbusTCP(ip, port, slaveID);
if (modbus != null)
{
bool coil = modbus.ReadCoils(0, 1)[0]; //获取地址为00001长度为1bit的值
modbus.WriteSingleCoil(0, true); //写入地址00001赋值为1
bool coil = modbus.ReadInputs(0, 1)[0]; //获取地址为10001长度为1bit的值
ushort[] register = modbus.ReadHoldingRegisters(0, 10); //获取地址为40001长度为20byte的值
modbus.WriteSingleRegister(14, 2); //写入地址为40015赋值为2
modbus.WriteMultipleRegisters(10, new ushort[2]); //写入地址为40011-40012赋值为空
string str = Modbus.GetString1(register, 0, 10); //转换为string
ushort[] register = modbus.ReadInputRegisters(0, 2); //获取地址为30001长度为4byte的值
float f = Modbus.GetReal(register, 0); //转换为float
}