Modbus RTU

一是什么

工业串口 问答 协议 走RS485/RS232,主站问,从站答

RTU(Remote Terminal Unit)远程终端单元

二RTU帧格式

从站地址 功能码 数据区 CRC16 校验
1 字节 1 字节 N 字节 2 字节(低字节在前)

三串口基础

3.1引用

cs 复制代码
using System.IO.Ports;

3.2串口参数(必须和从站完全一致!)

cs 复制代码
SerialPort sp = new SerialPort("COM1")
{
    BaudRate=9600,
    DataBits =8, 
    Parity = StopBits.One,
    ReadTimeout = 1000,
    WriteTiemout = 1000
};
sp.Open();
sp.Close();

3.3CRC校验(Modbus)

发之前算,收之后验。 CRC 不包含自己的两个字节。

cs 复制代码
// Modbus CRC16
public static ushort Crc16(byte[] data, int len)
{
    ushort crc = 0xFFFF;
    for (int i = 0; i < len; i++)
    {
        crc ^= data[i];
        for (int j = 0; j < 8; j++)
        {
            if ((crc & 0x0001) != 0)
                crc = (ushort)((crc >> 1) ^ 0xA001);
            else
                crc >>= 1;
        }
    }
    return crc;
}

使用

cs 复制代码
byte[] frame = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02 };
ushort crc = Crc16(frame, frame.Length);
// 拼到后面:低字节在前
byte[] sendBuf = frame.Concat(new byte[] { (byte)crc, (byte)(crc >> 8) }).ToArray();

四发送RTU请求

目标:从站地址 1,读寄存器 0,数量 2

cs 复制代码
// 1. 构造帧(不含 CRC)
byte[] req = new byte[]
{
    0x01,       // 从站地址
    0x03,       // 功能码:读保持寄存器
    0x00, 0x00, // 起始地址 0
    0x00, 0x02  // 寄存器数量 2
};

// 2. 加 CRC
ushort crc = Crc16(req, req.Length);
byte[] send = new byte[req.Length + 2];
Buffer.BlockCopy(req, 0, send, 0, req.Length);
send[req.Length] = (byte)crc;
send[req.Length + 1] = (byte)(crc >> 8);

// 3. 串口发送
sp.Write(send, 0, send.Length);

五C# 接收 RTU 应答

应答帧格式(03 应答)

从站地址 03 字节数 N 数据1 数据2 ... CRC低 CRC高

接收 + 校验 + 解析

cs 复制代码
// 收 1 帧(先按最大长度收,再处理)
byte[] recvBuf = new byte[256];
int len = sp.Read(recvBuf, 0, recvBuf.Length);
byte[] frame = recvBuf.Take(len).ToArray();

// 1. 校验 CRC
if (frame.Length < 2) return;
ushort recvCrc = (ushort)(frame[frame.Length - 2] | (frame[frame.Length - 1] << 8));
ushort calcCrc = Crc16(frame, frame.Length - 2);
if (recvCrc != calcCrc)
{
    Console.WriteLine("CRC 错误");
    return;
}

// 2. 检查地址、功能码
byte slaveId = frame[0];
byte func = frame[1];
if (slaveId != 1 || func != 0x03)
{
    Console.WriteLine("应答不匹配");
    return;
}

// 3. 解析数据
byte numBytes = frame[2];
for (int i = 0; i < numBytes; i += 2)
{
    ushort val = (ushort)(frame[3 + i] << 8 | frame[3 + i + 1]);
    Console.WriteLine("寄存器值:" + val);
}

完整示例

cs 复制代码
using System;
using System.IO.Ports;
using System.Linq;

class ModbusRtuDemo
{
    static SerialPort sp;

    static void Main()
    {
        sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
        {
            ReadTimeout = 1000,
            WriteTimeout = 1000
        };
        sp.Open();
        Console.WriteLine("串口打开");

        // 发:从站1,读寄存器0,2个
        byte[] req = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02 };
        ushort crc = Crc16(req, req.Length);
        byte[] send = req.Concat(new byte[] { (byte)crc, (byte)(crc >> 8) }).ToArray();
        sp.Write(send, 0, send.Length);
        Console.WriteLine("已发送:" + BitConverter.ToString(send));

        // 收
        byte[] buf = new byte[256];
        int len = sp.Read(buf, 0, buf.Length);
        byte[] frame = buf.Take(len).ToArray();
        Console.WriteLine("收到:" + BitConverter.ToString(frame));

        // CRC 校验
        ushort rCrc = (ushort)(frame[frame.Length - 2] | (frame[frame.Length - 1] << 8));
        ushort cCrc = Crc16(frame, frame.Length - 2);
        Console.WriteLine(rCrc == cCrc ? "CRC 正确" : "CRC 错误");

        sp.Close();
    }

    public static ushort Crc16(byte[] data, int len)
    {
        ushort crc = 0xFFFF;
        for (int i = 0; i < len; i++)
        {
            crc ^= data[i];
            for (int j = 0; j < 8; j++)
            {
                if ((crc & 0x0001) != 0)
                    crc = (ushort)((crc >> 1) ^ 0xA001);
                else
                    crc >>= 1;
            }
        }
        return crc;
    }
}

六第三方库

NModbus4/新Modbus

NModbus4 示例

cs 复制代码
using Modbus.Device;
using System.IO.Ports;

var sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
sp.Open();
var master = ModbusFactory.CreateRtuMaster(sp);
ushort[] regs = master.ReadHoldingRegisters(1, 0, 2); // 从站1,地址0,2个
相关推荐
kyle~10 小时前
嵌入式 SoC 系统
linux·c++·嵌入式·计算系统
tianyuanwo11 小时前
PostgreSQL 部署运维实战总结:从安装踩坑到生产可用
运维·数据库·postgresql
Decay__11 小时前
SQLServer 优化案例集 —— 慢 SQL 优化、索引重构与参数调整收益对比
运维·数据库·sql·mysql·架构
人生!?12 小时前
Linux 从入门到进阶 | 基础语法
linux·笔记·教程
优化Henry13 小时前
LTE 站点 S1 链路反复闪断导致小区频闪退服故障排查
运维·网络·学习·信息与通信·tdd
心之王者71613 小时前
Linux 进程信号:从产生、阻塞到捕捉与子进程回收
linux·服务器·网络
AndreaYYmm13 小时前
日志里 grep 到 Fail 就判失败?这六个假阳性我全踩过
linux·shell
manageengine13 小时前
服务器 CPU 100% 怎么排查:8 条命令从现象定位到根因
运维·服务器·cpu
分布式存储与RustFS13 小时前
RustFS 分片上传怎么配:大文件断点续传的最小可验证流程
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
赤生云电竞14 小时前
云游戏边缘节点部署:机房选址与网络拓扑优化实践 —— 从80ms时延预算看节点落位与Spine-Leaf组网
服务器·游戏·spine