- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
ModbusTcp协议是基于tcp的,但不是说一定要通过tcp协议才能传输,只要能传输二进制的地方都可以。比如WebSocket协议。 但由于目前我只有tcp上面的modbus服务器实现,所以我必须先用tcp连接借助已有工具来验证我的服务器是否写正确.
ModBusTCP协议报文比较复杂,主要是区分了3组类型 。
理论上将报文格式的种类从1种变成了8种。 但是在响应读数据时,没有区分数据量,而是到请求中去查找,听说是为了减少传输量。因此实际格式种类是3组6种.
请求读数据报文 。
事务标识符 | 协议标识符 | 报文长度 | 单元标识符 | 功能码 | 起始地址 | 读取数量 | |
---|---|---|---|---|---|---|---|
字段长度 | 2 byte | 2 byte | 2 byte | 1 byte | 1 byte | 2 byte | 2 byte |
数据类型 | ushort | ushort | ushort | byte | byte | ushort | ushort |
自增 | 一般为0 | 6,从单元标识符开始算 | 一般为0 | 链接 |
响应读数据报文 。
事务标识符 | 协议标识符 | 报文长度 | 单元标识符 | 功能码 | 数据字节数 | 数据 |
---|---|---|---|---|---|---|
2 byte | 2 byte | 2 byte | 1 byte | 1 byte | 1 byte | n byte |
值得注意的是,响应报文中没有包含请求的数据数量,只能从请求中获得。比如是7个线圈还是8个线圈是看不出来的。 这其实就限制了程序结构,应该在同一个上下文中声明请求和响应变量.
请求写单个数据报文 。
事务标识符 | 协议标识符 | 报文长度 | 单元标识符 | 功能码 | 起始地址 | 数据 |
---|---|---|---|---|---|---|
2 byte | 2 byte | 2 byte | 1 byte | 1 byte | 2 byte | 2 byte |
响应写单个数据报文 。
事务标识符 | 协议标识符 | 报文长度 | 单元标识符 | 功能码 | 起始地址 | 数据 |
---|---|---|---|---|---|---|
2 byte | 2 byte | 2 byte | 1 byte | 1 byte | 2 byte | 2 byte |
请求写单个数据的响应实际上是原样返回请求。 写1位线圈和写16位寄存器都传输了2个字节,这很奇怪。 原来是ModBusTCP定义写单个线圈用0xff00表示1,0x0000表示0.
请求写多个数据报文 。
事务标识符 | 协议标识符 | 报文长度 | 单元标识符 | 功能码 | 起始地址 | 数量 | 字节数 | 数据 |
---|---|---|---|---|---|---|---|---|
2 byte | 2 byte | 2 byte | 1 byte | 1 byte | 2 byte | 2 byte | 1 byte | n byte |
请求写多个数据时,如果是线圈,现在又变成了一位代表一个数据,而不是原来的2字节代表单个数据 还需要注意的是modbus采用大端传输,意味着一个字节中如果写了四个线圈,要从右向左数.
响应写多个数据报文 。
事务标识符 | 协议标识符 | 报文长度 | 单元标识符 | 功能码 | 起始地址 | 数量 |
---|---|---|---|---|---|---|
2 byte | 2 byte | 2 byte | 1 byte | 1 byte | 2 byte | 2 byte |
从功能码字段往后,字段不确定,所以统一用Data表示。 在各个响应方法里根据不同功能码对数据进行处理.
//WebModbus.cs
// Modbus报文
public class ADUMessage
{
// 事务标识符
public ushort Transaction { get; set; }
// 协议标识符
public ushort Protocol { get; set; }
// 报文长度
public ushort Length { get; set; }
// 单元标识符
public byte Unit { get; set; }
// 功能码
public byte FunctionCode { get; set; }
// 数据
public byte[] Data { get; set; }
public static ADUMessage Deserialize(byte[] buffer){}
public static byte[] Serialze(ADUMessage message){}
}
modbusTCP协议规定了服务器实现线圈、寄存器2种数据模型,每种又分为只读、可读可写。于是服务器一共要提供4个存储栈。 因为报文起始地址字段有2个字节,所以寻址范围是0-65535 。
栈地址 | 线圈栈 | 离散量输入栈 | 只读寄存器栈 | 读写寄存器栈 |
---|---|---|---|---|
65535 | 0 | 0 | 0xFF C1 | 0x00 00 |
65534 | 0 | 1 | 0x00 00 | 0x00 00 |
... | 1 | 0 | 0x00 00 | 0x00 00 |
1 | 0 | 0 | 0x00 00 | 0x08 00 |
0 | 0 | 0 | 0x00 00 | 0x00 1A |
其中线圈栈和离散量输入栈是bool类型的,实际实现的时候可以是数字0、1,也可以是布尔true,false,还可以是字符串"on","off" 如何存储,存储的数据类型是什么?这不重要。重要的是传输的时候采用规定格式就行.
采用第3种报文写单线圈时,如果要传输true,要使用0xFF 00。传输false,要使用0x00 00 。
其它时候都用一个bit位表示true和false 比如线圈栈,从地址0开始读9个数据。就是小端字节序0x0f 00.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ... |
---|---|---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
当然,要从请求报文中知道截取取响应数据的哪几位.
//WebModbus.cs
// 数据仓库,144KB
public class DataStore
{
// 读写16位寄存器,64KB
public ushort[] HoldingRegisters;
// 只读16位寄存器,64KB
public ushort[] InputRegisters;
// 读写1位线圈,8KB
public bool[] CoilDiscretes;
// 只读1位线圈,8KB
public bool[] CoilInputs;
public DataStore()
{
HoldingRegisters = new ushort[65536];
InputRegisters = new ushort[65536];
CoilDiscretes = new bool[65536];
CoilInputs = new bool[65536];
}
// 读 读写16位寄存器
public ushort[] ReadHoldingRegisters(ushort startIndex, ushort length){}
// 读 只读16位寄存器
public ushort[] ReadInputRegisters(ushort startIndex, ushort length){}
// 读 读写1位线圈
public bool[] ReadCoilDiscretes(ushort startIndex, ushort length){}
// 读 只读1位线圈
public bool[] ReadCoilInputs(ushort startIndex, ushort length){}
// 写 读写16位寄存器
public void WriteHoldingRegisters(ushort startIndex, ushort[] data){}
//写 读写1位线圈
public void WriteCoilDiscretes(ushort startIndex, bool[] data){}
}
功能码主要用来操作这4个栈,因为栈分读写和只读,写入还分为多个数据和单个数据。所以主要有8种功能码 。
相应的 。
字节序这个东西还是当初上操作系统课时老师讲过,以后再没接触。不过要自己处理字节流时又面临这个问题了。 modbusTcp主要采用大端字节序方式传输数据。 为什么说主要呢,因为唯独线圈数据是采用小端的方式传输的,其它数据都是大端传输。这要特别注意.
BinaryReader
和BinaryWriter
来处理接收到的和要传输的字节流。reader.ReadUInt16()
,如果我们传的字节流是0x00 01,它会以为是ushort 256,而不是ushort 1。override
这个方法,翻转字节数组,按照大端方式读取。//BigEndianBinaryReader.cs
public override short ReadInt16()
{
var data = base.ReadBytes(2);
Array.Reverse(data);
return BitConverter.ToInt16(data, 0);
}
modbus服务器要做的就是 。
接收消息,反序列化为消息对象 。
然后判断怎么响应 。
读取数据栈,构造消息 。
序列化后响应 。
核心方法 。
//WebModbus.cs
public ADUMessage HandleRequest(byte[] buffer)
{
ADUMessage request = ADUMessage.Deserialize(buffer);
switch (request.FunctionCode)
{
//读 读写线圈
case 0x01:
return Response_01(request);
//读 只读线圈
case 0x02:
return Response_02(request);
//读 读写寄存器
case 0x03:
return Response_03(request);
//读 只读寄存器
case 0x04:
return Response_04(request);
//写 读写一个线圈
case 0x05:
return Response_05(request);
//写 读写一个寄存器
case 0x06:
return Response_06(request);
//写 读写多个线圈
case 0x0f:
return Response_0f(request);
//写 读写多个寄存器
case 0x10:
return Response_10(request);
default:
return Response_01(request);
}
}
程序所需命令行参数的一种方式是在项目文件种指定,这在调试时比较方便 。
<PropertyGroup>
<StartArguments>5234</StartArguments>
</PropertyGroup>
/// <summary>
/// 数据仓库,144KB
/// </summary>
public class DataStore
{
/// <summary>
/// 读写16位寄存器,64KB
/// </summary>
public ushort[] HoldingRegisters;
/// <summary>
/// 只读16位寄存器,64KB
/// </summary>
public ushort[] InputRegisters;
/// <summary>
/// 读写1位线圈,8KB
/// </summary>
public bool[] CoilDiscretes;
/// <summary>
/// 只读1位线圈,8KB
/// </summary>
public bool[] CoilInputs;
public DataStore()
{
HoldingRegisters = new ushort[65536];
InputRegisters = new ushort[65536];
CoilDiscretes = new bool[65536];
CoilInputs = new bool[65536];
}
/// <summary>
/// 读 读写16位寄存器
/// </summary>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public ushort[] ReadHoldingRegisters(ushort startIndex, ushort length)
{
return HoldingRegisters.Take(new Range(new Index(startIndex), new Index(startIndex + length))).ToArray();
}
/// <summary>
/// 读 只读16位寄存器
/// </summary>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public ushort[] ReadInputRegisters(ushort startIndex, ushort length)
{
return InputRegisters.Take(new Range(new Index(startIndex), new Index(startIndex + length))).ToArray();
}
/// <summary>
/// 读 读写1位线圈
/// </summary>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public bool[] ReadCoilDiscretes(ushort startIndex, ushort length)
{
return CoilDiscretes.Take(new Range(new Index(startIndex), new Index(startIndex + length))).ToArray();
}
/// <summary>
/// 读 只读1位线圈
/// </summary>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
public bool[] ReadCoilInputs(ushort startIndex, ushort length)
{
return CoilInputs.Take(new Range(new Index(startIndex), new Index(startIndex + length))).ToArray();
}
/// <summary>
/// 写 读写16位寄存器
/// </summary>
/// <param name="startIndex"></param>
/// <param name="data"></param>
public void WriteHoldingRegisters(ushort startIndex, ushort[] data)
{
for (int i = 0; i < data.Length; i++)
{
if (startIndex+i < 65536)
{
HoldingRegisters[startIndex + i] = data[i];
}
}
}
/// <summary>
/// 写 读写1位线圈
/// </summary>
/// <param name="startIndex"></param>
/// <param name="data"></param>
public void WriteCoilDiscretes(ushort startIndex, bool[] data)
{
for (int i = 0; i < data.Length; i++)
{
if (startIndex + i < 65536)
{
CoilDiscretes[startIndex + i] = data[i];
}
}
}
}
/// <summary>
/// Modbus报文
/// </summary>
public class ADUMessage
{
/// <summary>
/// 事务标识符
/// </summary>
public ushort Transaction { get; set; }
/// <summary>
/// 协议标识符
/// </summary>
public ushort Protocol { get; set; }
/// <summary>
/// 报文长度
/// </summary>
public ushort Length { get; set; }
/// <summary>
/// 单元标识符
/// </summary>
public byte Unit { get; set; }
/// <summary>
/// 功能码
/// </summary>
public byte FunctionCode { get; set; }
/// <summary>
/// 数据
/// </summary>
public byte[] Data { get; set; }
public static ADUMessage Deserialize(byte[] buffer)
{
//BinaryReader读取方式是小端(右边是高字节),而modbus是大端传输(左边是高字节)
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(buffer));
ADUMessage adu = new ADUMessage()
{
Transaction = reader.ReadUInt16(),
Protocol = reader.ReadUInt16(),
Length = reader.ReadUInt16(),
Unit = reader.ReadByte(),
FunctionCode = reader.ReadByte(),
Data = reader.ReadBytes(buffer.Length - 8)
};
return adu;
}
public static byte[] Serialze(ADUMessage message)
{
using (MemoryStream ms=new MemoryStream())
{
BinaryWriter writer = new BigEndianBinaryWriter(ms);
writer.Write(message.Transaction);
writer.Write(message.Protocol);
writer.Write(message.Length);
writer.Write(message.Unit);
writer.Write(message.FunctionCode);
writer.Write(message.Data);
return ms.ToArray();
}
}
}
public class WebModbusServer
{
public DataStore store = new DataStore();
public ADUMessage HandleRequest(byte[] buffer)
{
ADUMessage request = ADUMessage.Deserialize(buffer);
switch (request.FunctionCode)
{
//读 读写线圈
case 0x01:
return Response_01(request);
//读 只读线圈
case 0x02:
return Response_02(request);
//读 读写寄存器
case 0x03:
return Response_03(request);
//读 只读寄存器
case 0x04:
return Response_04(request);
//写 读写一个线圈
case 0x05:
return Response_05(request);
//写 读写一个寄存器
case 0x06:
return Response_06(request);
//写 读写多个线圈
case 0x0f:
return Response_0f(request);
//写 读写多个寄存器
case 0x10:
return Response_10(request);
default:
return Response_01(request);
}
}
public byte[] CoilToBytes(bool[] bools)
{
int byteCount = (bools.Length + 7) / 8; // 计算所需的字节数
byte[] bytes = new byte[byteCount];
for (int i = 0; i < bools.Length; i++)
{
int byteIndex = i / 8; // 计算当前布尔值应该存储在哪个字节中
int bitIndex = i % 8; // 计算当前布尔值应该存储在字节的哪个位上
if (bools[i])
{
// 设置对应位为 1
bytes[byteIndex] |= (byte)(1 << bitIndex);
}
else
{
// 对应位保持为 0,无需额外操作
}
}
return bytes;
}
/// <summary>
/// 读 读写线圈
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_01(ADUMessage request)
{
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
BinaryWriter writer;
ushort StartAddress, DataNumber;
StartAddress = reader.ReadUInt16();
DataNumber = reader.ReadUInt16();
bool[] data = store.ReadCoilDiscretes(StartAddress, DataNumber);
byte[] coilBytes = CoilToBytes(data);
byte[] dataBytes = new byte[coilBytes.Length + 1];
writer = new BinaryWriter(new MemoryStream(dataBytes));
writer.Write((byte)coilBytes.Length);
writer.Write(coilBytes);
ADUMessage response = new ADUMessage()
{
Transaction = request.Transaction,
Protocol = request.Protocol,
Length = (ushort)(dataBytes.Length + 2),
Unit = request.Unit,
FunctionCode = request.FunctionCode,
Data = dataBytes,
};
return response;
}
/// <summary>
/// 读 只读线圈
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_02(ADUMessage request)
{
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
BinaryWriter writer;
ushort StartAddress, DataNumber;
StartAddress = reader.ReadUInt16();
DataNumber = reader.ReadUInt16();
bool[] data = store.ReadCoilInputs(StartAddress, DataNumber);
byte[] coilBytes = CoilToBytes(data);
byte[] dataBytes = new byte[coilBytes.Length + 1];
writer = new BinaryWriter(new MemoryStream(dataBytes));
writer.Write((byte)coilBytes.Length);
writer.Write(coilBytes);
ADUMessage response = new ADUMessage()
{
Transaction = request.Transaction,
Protocol = request.Protocol,
Length = (ushort)(dataBytes.Length + 2),
Unit = request.Unit,
FunctionCode = request.FunctionCode,
Data = dataBytes,
};
return response;
}
/// <summary>
/// 读 读写寄存器
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_03(ADUMessage request)
{
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
BinaryWriter writer;
ushort StartAddress, DataNumber;
StartAddress = reader.ReadUInt16();
DataNumber = reader.ReadUInt16();
ushort[] data = store.ReadHoldingRegisters(StartAddress, DataNumber);
byte[] dataBytes = new byte[data.Length * 2 + 1];
writer = new BigEndianBinaryWriter(new MemoryStream(dataBytes));
writer.Write((byte)(data.Length * 2));
foreach (ushort value in data)
{
writer.Write(value);
}
Array.Resize(ref dataBytes, dataBytes.Length + 1);
ADUMessage response = new ADUMessage()
{
Transaction = request.Transaction,
Protocol = request.Protocol,
Length = (ushort)(dataBytes.Length + 2),
Unit = request.Unit,
FunctionCode = request.FunctionCode,
Data = dataBytes,
};
return response;
}
/// <summary>
/// 读 只读寄存器
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_04(ADUMessage request)
{
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
BinaryWriter writer;
ushort StartAddress, DataNumber;
StartAddress = reader.ReadUInt16();
DataNumber = reader.ReadUInt16();
ushort[] data = store.ReadInputRegisters(StartAddress, DataNumber);
byte[] dataBytes = new byte[data.Length * 2 + 1];
writer = new BigEndianBinaryWriter(new MemoryStream(dataBytes));
writer.Write((byte)(data.Length * 2));
foreach (ushort value in data)
{
writer.Write(value);
}
Array.Resize(ref dataBytes, dataBytes.Length + 1);
ADUMessage response = new ADUMessage()
{
Transaction = request.Transaction,
Protocol = request.Protocol,
Length = (ushort)(dataBytes.Length + 2),
Unit = request.Unit,
FunctionCode = request.FunctionCode,
Data = dataBytes,
};
return response;
}
/// <summary>
/// 写 读写一个线圈
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_05(ADUMessage request)
{
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
ushort StartAddress, coli;
StartAddress = reader.ReadUInt16();
coli = reader.ReadUInt16();
store.WriteCoilDiscretes(StartAddress, new bool[] { coli ==0xff00?true:false});
return request;
}
/// <summary>
/// 写 读写一个寄存器
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_06(ADUMessage request)
{
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
ushort StartAddress, register;
StartAddress = reader.ReadUInt16();
register = reader.ReadUInt16();
store.WriteHoldingRegisters(StartAddress, new ushort[] { register });
return request;
}
/// <summary>
/// 写 读写多个线圈
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_0f(ADUMessage request)
{
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
ushort StartAddress, DataNumber;
StartAddress = reader.ReadUInt16();
DataNumber = reader.ReadUInt16();
byte byteNumber = reader.ReadByte();
//线圈是小端传输
byte[] bytes = reader.ReadBytes(byteNumber);
bool[] data=new bool[DataNumber];
byte index = 0;
foreach (var item in bytes)
{
//1000 0000
byte rr = (byte)0x01;
for (int i = 0; i < 8; i++)
{
if (index< DataNumber)
{
var result = rr & item;
if (result > 0)
{
data[index] = true;
}
else
{
data[index] = false;
}
//0100 0000
rr <<= 1;
index++;
}
else
{
break;
}
}
}
store.WriteCoilDiscretes(StartAddress, data);
return request;
}
/// <summary>
/// 写 读写多个寄存器
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private ADUMessage Response_10(ADUMessage request)
{
//寄存器是大端传输
BinaryReader reader = new BigEndianBinaryReader(new MemoryStream(request.Data));
ushort StartAddress, DataNumber;
StartAddress = reader.ReadUInt16();
DataNumber = reader.ReadUInt16();
byte byteNumber = reader.ReadByte();
ushort[] data = new ushort[byteNumber / 2];
for (int i = 0; i < data.Length; i++)
{
data[i] = reader.ReadUInt16();
}
store.WriteHoldingRegisters(StartAddress, data);
return request;
}
}
public class BigEndianBinaryReader : BinaryReader
{
public BigEndianBinaryReader(Stream input) : base(input)
{
}
public override short ReadInt16()
{
var data = base.ReadBytes(2);
Array.Reverse(data);
return BitConverter.ToInt16(data, 0);
}
public override ushort ReadUInt16()
{
var data = base.ReadBytes(2);
Array.Reverse(data);
return BitConverter.ToUInt16(data, 0);
}
public override int ReadInt32()
{
var data = base.ReadBytes(4);
Array.Reverse(data);
return BitConverter.ToInt32(data, 0);
}
public override uint ReadUInt32()
{
var data = base.ReadBytes(4);
Array.Reverse(data);
return BitConverter.ToUInt32(data, 0);
}
public override long ReadInt64()
{
var data = base.ReadBytes(8);
Array.Reverse(data);
return BitConverter.ToInt64(data, 0);
}
public override float ReadSingle()
{
var data = base.ReadBytes(4);
Array.Reverse(data);
return BitConverter.ToSingle(data, 0);
}
public override double ReadDouble()
{
var data = base.ReadBytes(8);
Array.Reverse(data);
return BitConverter.ToDouble(data, 0);
}
// 可以继续添加其他方法来支持更多数据类型的大端读取
}
public class BigEndianBinaryWriter : BinaryWriter
{
public BigEndianBinaryWriter(Stream input) : base(input)
{
}
public override void Write(ushort value)
{
var bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
base.Write(bytes);
}
public override void Write(short value)
{
var bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
base.Write(bytes);
}
public override void Write(uint value)
{
var bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
base.Write(bytes);
}
public override void Write(int value)
{
var bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
base.Write(bytes);
}
public override void Write(ulong value)
{
var bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
base.Write(bytes);
}
public override void Write(long value)
{
var bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
base.Write(bytes);
}
// 可以继续添加其他方法来支持更多数据类型的大端写入
}
internal class Program
{
static WebModbusServer webModbusServer;
static void Main(string[] args)
{
webModbusServer = new WebModbusServer();
//服务器
if (args.Length == 1)
{
//webModbusServer.store.WriteCoilDiscretes(0, new bool[] { true, true });
//webModbusServer.store.CoilInputs[0] = true;
//webModbusServer.store.CoilInputs[1] = true;
StartServer(args[0]);
}
}
private static void StartServer(string args)
{
int serverPort = Convert.ToInt32(args);
var server = new TcpListener(IPAddress.Parse("127.0.0.1"), serverPort);
Console.WriteLine($"TCP服务器 127.0.0.1:{serverPort}");
server.Start();
int cnt = 0;
Task.Run(async () =>
{
List<TcpClient> clients = new List<TcpClient>();
while (true)
{
TcpClient client = await server.AcceptTcpClientAsync();
clients.Add(client);
cnt++;
var ep = client.Client.RemoteEndPoint as IPEndPoint;
Console.WriteLine($"TCP客户端_{cnt} {ep.Address}:{ep.Port}");
//给这个客户端开一个聊天线程
//操作系统将会根据游客端口对应表将控制权交给对应游客线程
StartModbus(client);
}
}).Wait();
}
public static async Task StartModbus(TcpClient client)
{
var buffer = new byte[1024 * 4];
while (client.Connected)
{
int msgLength = await client.Client.ReceiveAsync(new ArraySegment<byte>(buffer));
//关闭连接时会接收到一次空消息,不知道为什么
if (msgLength>0)
{
PrintBytes(buffer.Take(msgLength).ToArray(), "请求 ");
ADUMessage response = webModbusServer.HandleRequest(buffer.Take(msgLength).ToArray());
await client.Client.SendAsync(ADUMessage.Serialze(response));
PrintBytes(ADUMessage.Serialze(response), "响应 ");
}
}
}
public static void PrintBytes(byte[] bytes,string prefix="")
{
Console.Write(prefix);
for (int i = 0; i < bytes.Length; i++)
{
if (i < 2)
{
Console.ForegroundColor = ConsoleColor.Red;
}
else if(i<4)
{
Console.ForegroundColor = ConsoleColor.Green;
}
else if(i<6)
{
Console.ForegroundColor= ConsoleColor.Blue;
}
else if (i < 7)
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
else if (i<8)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
}
else
{
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write(bytes[i].ToString("X2") + " ");
}
Console.WriteLine();
}
}
最后此篇关于基于WebSocket的modbus通信(一)-服务器的文章就讲到这里了,如果你想了解更多关于基于WebSocket的modbus通信(一)-服务器的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
将 MODBUS 安装与 Home Assistant 集成 Gavazzi 中的 MODBUS Controller SH2WEB24 Home Assistant Configuration.ya
我正在使用 ModBus RTU,并且我正在尝试弄清楚如何计算 CRC16。 我不需要代码示例。我只是对机制很好奇。 我了解到基本的 CRC 是数据字的多项式除法,根据多项式的长度,用零填充。 下面的
如何通过广播机制获取有效ip范围后判断接入网络的设备是否为modbus设备或如何自动发现接入网关的modbus设备的slave id? 最佳答案 在 Modbus TCP 的上下文中,slaveId
我正在尝试使用 Modbus 协议(protocol)对 Socomec 仪表执行 ping 操作,经过研究,我发现了 NModbus,这是一个 C# 库。我之前从未使用过库或 C#(通常是 Java
我以前从未使用过 modbus。 我有一个 eBox-Wifi Epever 连接到用于太阳能的 MPPT 充电 Controller TRIRON。 我能够让它连接到我的 wifi,现在有了一个可访
我正在开发的设备必须支持 CANopen 和 Modbus(RTU 和 TCP)通信。是否有任何格式的电子数据表(例如 EDS 或 XDD)可用于定义两者? 我尝试使用几种工具(Vector CANe
https://code.google.com/p/stm32modbus/source/browse/#svn%2Ftrunk%2F%20stm32modbus%20--username%20ick
我对 Modbus 通信有点陌生,我已经开始阅读“免费 Modbus”库。现在,我了解了如何使用它来实现 Modbus 通信的从站端,但我似乎无法找到如何在主站端使用该库。例如,我在主机端调用什么函数
我对 Modbus 通信有点陌生,我已经开始阅读“免费 Modbus”库。现在,我了解了如何使用它来实现 Modbus 通信的从站端,但我似乎无法找到如何在主站端使用该库。例如,我在主机端调用什么函数
我们正在构建一个产品,它需要 modbus 通信(rs-485 和 TCP/IP)。代码必须在运行 Linux 的嵌入式设备上运行。我们有以下标准来选择我们将使用的库。 它必须是开源的,因为我们是开源
I am trying to read and write data to modbus via TCP/IP. I can connect to modbus simulator succes
我正在尝试创建一个 modbus 主从模型。我已经使用 pymodbus 编写了 modbus slave,而 modbus master 使用的是 c libmodbus。 Slave 有一堆寄存器
我正在使用 modbus-tk 通过 RS-485 网络通过 Modbus RTU 与设备进行串行通信。 我正在尝试弄清楚如何使用函数 23,READ_WRITE_MULTIPLE_REGISTERS
我有一个支持 ModBus 协议(protocol)的设备,使用“Jamod”尝试连接到该设备并读取寄存器值,收到错误代码 2, 单位配置: 该装置支持通过 RS-485 和以太网接口(interfa
我有一个设置,2 个 SDM120 kWh 电能表以菊花链方式连接在同一个串行端口上(将来我想添加一个 SDM630)。我在 MinimalModbus 通信中找到了“Using multiple i
我正在为一个嵌入式系统玩弄 Modbus(我以前从未处理过这个问题)并且我知道 Modbus 从设备接收到一个范围为 1-247 的地址,但是 Modbus 主设备是否也接收到一个 ID? 如果是这样
我正在寻找一个好的专业 modbus 嗅探器工具来扫描主从通信。请问有人知道吗? 问候 达尼 最佳答案 您可以使用 Wireshark .优秀的工具。支持 Modbus TCP。 关于需要 Modbu
我必须控制带有串行接口(interface)的 modbus 设备。我没有使用 modbus 的经验。但我的简短研究揭示了几个 modbus 库 pymodbus MinimalModbus Modb
要求:通过 Modbus 从 Serial port-1 收集数据,在 Serial port-2 上进行镜像。 我可以在主从之间进行通信,但无法将数据镜像到串行端口 2。请建议我如何实现这一点。 代
我在 MODBUS 项目中工作了 2 个月,现在我发现了一个问题。 我的客户要求我写入一个输入寄存器(地址 30001 到 40000)。 我认为这对我来说不是问题,因为每个 modbus 文档都说
我是一名优秀的程序员,十分优秀!