gpt4 book ai didi

python - 访问 pyModbus 事务中的原始字节

转载 作者:行者123 更新时间:2023-12-01 00:29:39 28 4
gpt4 key购买 nike

我有一个 python 脚本,可以使用 pymodbus 库处理 Modbus 事务。为了进行故障排除,我想打印发送和接收到设备的原始字节,最好以十六进制格式。

这是简化的代码,请参阅底部的注释以获取我想要得到的示例。我使用了 TCP 客户端,但希望它也能在 ModbusSerialClient 上工作。

from pymodbus.client.sync import ModbusTcpClient

ipAddress = '10.130.14.174'
registerToRead = 3000

client = ModbusTcpClient(ipAddress, port=502)
connection = client.connect()

response = client.read_holding_registers(registerToRead, 1, unit=1)

print(response.registers)

# Would like to get something like:
# OUT: [00h] [00h] [00h] [00h] [00h] [06h] [01h] [03h] [0Bh] [B8h] [00h] [01h]
# IN : [00h] [00h] [00h] [00h] [00h] [05h] [01h] [03h] [02h] [00h] [FFh]

我尝试了 response.encode() 但只返回了 b'\x02\x00\xff'

最佳答案

要获取原始帧,您只需在 Debug模式下运行请求即可。

那就是这样的:

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这是非常强大的。如果您需要通过串行方式对 Modbus 执行相同操作,您可以尝试 SerialPCAP .

编辑:这可能是您目前不需要的东西,但如果您无法访问 Modbus 串行链路的任何一侧,您可以点击总线或使用正如我所解释的软件嗅探器here , herehere .

对于 Modbus TCP,如果您无法访问链路的任何一侧或 network switch,我不知道有任何简单的技术可以使用 Wireshark 监控流量。 .

关于python - 访问 pyModbus 事务中的原始字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58276741/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com