Modbus RTU 与 Modbus TCP 深入指南-Wireshark抓包分析实战

九、Wireshark抓包分析实战

9.1 Modbus TCP 过滤器

过滤器表达式 说明
tcp.port == 502 捕获所有Modbus TCP流量
modbus 简单过滤器
modbus.func_code == 3 只显示读保持寄存器
modbus.func_code == 6 只显示写单寄存器
modbus.trans_id == 0x0001 特定事务ID
modbus.proto_id != 0 非标准协议标识(异常)
modbus.unit_id == 1 特定单元ID
modbus.error == 1 只显示异常响应

9.2 RTU over serial 抓包(Linux)

bash 复制代码
# 方法1:创建虚拟串口用于监听
socat -d -d pty,link=/tmp/ttyV0,rawer tcp:127.0.0.1:1234

# 将设备连接到虚拟串口
# 在Wireshark中捕获 /tmp/ttyV0,使用"Serial"接口

# 方法2:使用serial2pcap工具
sudo apt-get install wireshark
sudo modprobe can
# 使用外部串口嗅探硬件(如ComSpy)

9.3 分析步骤

9.3.1 抓包命令
bash 复制代码
# 捕获到文件
sudo tcpdump -i eth0 -s 1500 -w modbus_capture.pcap 'tcp port 502'

# 同时捕获并解码
sudo tcpdump -i eth0 -A -s 1500 'tcp port 502 and host 192.168.1.100'
9.3.2 Wireshark分析流程
  1. 打开pcap文件:File → Open → modbus_capture.pcap

  2. 应用过滤器 :输入 modbus 回车

  3. 查看请求:选择一个包,展开 Modbus/TCP 部分

  4. 跟踪流:右键 → Follow → TCP Stream

  5. 查看时序:Statistics → Flow Graph

9.3.3 解读示例(读保持寄存器)
bash 复制代码
Frame 1: 192.168.1.10 → 192.168.1.100 (Modbus TCP)
Transcation ID: 0x0001
Protocol ID: 0x0000
Length: 0x0006
Unit ID: 0x01
Modbus PDU:
    Function Code: Read Holding Registers (3)
    Starting Address: 0x0000
    Quantity: 0x0002

Frame 2: 192.168.1.100 → 192.168.1.10 (Modbus TCP)
Transcation ID: 0x0001
Protocol ID: 0x0000
Length: 0x0005
Unit ID: 0x01
Modbus PDU:
    Function Code: Read Holding Registers (3)
    Byte Count: 0x04
    Register 0x0000: 0x1234
    Register 0x0001: 0x5678

9.4 常见问题诊断

现象 Wireshark显示 可能原因 解决方法
无响应 只有请求,无响应 设备IP错误、端口错误、防火墙 检查连接,ping设备
重置连接 TCP RST包 端口未监听 确认设备支持Modbus TCP
超时重传 TCP Dup ACK, Retransmission 网络丢包 检查网线、交换机
乱序响应 事务ID不匹配 设备实现有bug 忽略乱序响应,等待匹配
异常响应 Modbus Exception 请求非法 查异常码表
相关推荐
Lzg_na2 小时前
ARP 地址解析协议详解
网络·网络协议
北京盛世宏博3 小时前
通信异常深度排查:以太网温湿度设备 TCP 粘包、UDP 丢包根治方案
单片机·网络协议·tcp/ip·udp·以太网·传感器·温湿度
paopaokaka_luck5 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
CCblandy6 小时前
HTTP 长连接 3 类故障实测:[WinError 10053]、99 个 TIME_WAIT、69 次多余握手,4 种对策怎么选
后端·python·网络协议·http·云原生·性能优化·架构
雪雪爱冲浪6 小时前
2026 年网络爬虫领域的Puppeteer vs. Playwright
爬虫·网络协议·测试工具·接口隔离原则
zxanz16 小时前
SSL 证书申请下来快吗?
网络·网络协议·ssl
false_or_true6 小时前
一次由 tcp_tw_recycle 引发的“灵异”故障排查:基于双网闸架构的踩坑记
网络·tcp/ip·架构
samble8 小时前
从零搭建灌装监控系统(十八):自定义控件,三色灯与状态指示
c#·wpf·mvvm·modbus·工业控制
Escalating_xu9 小时前
【Linux 网络编程】网络基础概念(下):IP、端口、Socket 与网络字节序,写代码前先打好基础
linux·网络·tcp/ip
网络小江9 小时前
上网第五十二课:加密隧道与拨号专网——安全接入的通俗解释
网络协议