要获取原始帧,您只需在调试模式下运行请求即可。
那会是这样的:
from pymodbus.client.sync import ModbusTcpClient
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
ipAddress = '10.130.14.174'
registerToRead = 3000
client = ModbusTcpClient(ipAddress, port=502)
connection = client.connect()
response = client.read_holding_registers(registerToRead, 1, unit=1)
如果您现在从 Python 控制台运行此代码,您应该会看到类似以下内容:
2019-10-08 13:10:42,872 MainThread DEBUG transaction :111 Current transaction state - TRANSACTION_COMPLETE
2019-10-08 13:10:42,872 MainThread DEBUG transaction :116 Running transaction 3
2019-10-08 13:10:42,872 MainThread DEBUG transaction :215 SEND: 0x0 0x3 0x0 0x0 0x0 0x6 0x1 0x3 0x0 0x1 0x0 0x1
2019-10-08 13:10:42,872 MainThread DEBUG sync :73 New Transaction state 'SENDING'
2019-10-08 13:10:42,872 MainThread DEBUG transaction :224 Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2019-10-08 13:10:42,873 MainThread DEBUG transaction :300 Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
2019-10-08 13:10:42,873 MainThread DEBUG transaction :229 RECV: 0x0 0x3 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x0 0x14
2019-10-08 13:10:42,873 MainThread DEBUG socket_framer :147 Processing: 0x0 0x3 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x0 0x14
2019-10-08 13:10:42,873 MainThread DEBUG factory :266 Factory Response[ReadHoldingRegistersResponse: 3]
2019-10-08 13:10:42,873 MainThread DEBUG transaction :379 Adding transaction 3
2019-10-08 13:10:42,873 MainThread DEBUG transaction :390 Getting transaction 3
2019-10-08 13:10:42,873 MainThread DEBUG transaction :189 Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
>>>
如果您想要更多详细信息或者需要处理多个帧,我建议安装Wireshark https://www.wireshark.org/这是非常强大的。如果您需要通过串行方式对 Modbus 执行相同的操作,您可以尝试串行PCAP https://github.com/j123b567/SerialPCAP.
EDIT:这可能是您目前不需要的东西,但如果您无法访问 Modbus 串行链路的任何一侧,您可以点击总线或使用软件嗅探器,正如我所解释的那样here https://stackoverflow.com/a/56377015/11476836, here https://stackoverflow.com/questions/57056042/i-am-sending-commands-through-serial-port-in-python-but-they-are-sent-multiple-t/57062146#57062146 and here https://stackoverflow.com/questions/57153141/cant-use-interceptty-or-slsnif-to-sniff-data-sent-via-serial-port.
对于 Modbus TCP,如果您无法访问链路的任一侧或无法访问网络交换机 https://serverfault.com/questions/187597/can-wireshark-read-data-being-sent-to-from-other-computers.