我正在尝试与具有 RS485 modbus 接口的电表 (Orno WE-517) 进行通信。该串行接口连接到 Elfin EW11 modbus 至 Wifi 转换器。
使用 pymodbus,我可以连接到适配器,但我无法从仪表中读取任何内容。 这是我的代码:
from pymodbus.client.sync import ModbusTcpClient
from pprint import pprint
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)
client = ModbusTcpClient('192.168.0.18', 502)
if not client.connect():
print('Error connecting')
exit()
print('holding register')
result = client.read_holding_registers(28,1)
response = client.execute(result)
pprint(vars(response))
client.close()
这是脚本的输出:
python3 modBus.py
2021-12-30 10:46:44,112 MainThread DEBUG sync :216 Connection to Modbus server established. Socket ('192.168.0.10', 47763)
holding register
2021-12-30 10:46:44,113 MainThread DEBUG transaction :140 Current transaction state - IDLE
2021-12-30 10:46:44,114 MainThread DEBUG transaction :145 Running transaction 1
2021-12-30 10:46:44,114 MainThread DEBUG transaction :273 SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x1c 0x0 0x1
2021-12-30 10:46:44,115 MainThread DEBUG sync :76 New Transaction state 'SENDING'
2021-12-30 10:46:44,115 MainThread DEBUG transaction :287 Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:44,216 MainThread DEBUG transaction :375 Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
2021-12-30 10:46:44,217 MainThread DEBUG transaction :297 RECV: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread DEBUG socket_framer :147 Processing: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread DEBUG factory :266 Factory Response[ReadHoldingRegistersResponse: 3]
2021-12-30 10:46:44,219 MainThread DEBUG transaction :454 Adding transaction 1
2021-12-30 10:46:44,220 MainThread DEBUG transaction :465 Getting transaction 1
2021-12-30 10:46:44,220 MainThread DEBUG transaction :224 Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
2021-12-30 10:46:44,221 MainThread DEBUG transaction :140 Current transaction state - TRANSACTION_COMPLETE
2021-12-30 10:46:44,221 MainThread DEBUG transaction :145 Running transaction 2
2021-12-30 10:46:44,222 MainThread DEBUG transaction :273 SEND: 0x0 0x2 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,222 MainThread DEBUG sync :76 New Transaction state 'SENDING'
2021-12-30 10:46:44,223 MainThread DEBUG transaction :287 Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:47,227 MainThread DEBUG transaction :303 Transaction failed. (Modbus Error: [Invalid Message] No response received, expected at least 8 bytes (0 received))
2021-12-30 10:46:47,228 MainThread DEBUG socket_framer :147 Processing:
2021-12-30 10:46:47,228 MainThread DEBUG transaction :465 Getting transaction 2
2021-12-30 10:46:47,229 MainThread DEBUG transaction :224 Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
{'fcode': 3,
'message': '[Input/Output] Modbus Error: [Invalid Message] No response '
'received, expected at least 8 bytes (0 received)',
'string': '[Input/Output] Modbus Error: [Invalid Message] No response '
'received, expected at least 8 bytes (0 received)'}
如您所见,pymodbus 设法连接、发送请求并接收响应。 但我找不到从该响应中执行某些操作的方法。
有人有想法可以帮助我吗?
提前,非常感谢您的帮助:)
您想读哪个电表?
尝试添加
改变
result = client.read_holding_registers(address=28, count=2, unit=1)
添加
if not result.isError():
print(BinaryPayloadDecoder.fromRegisters(result.registers,
byteorder=Endian.Big,
wordorder=Endian.Big).decode_32bit_float())
坚持寻找解决方案,我发现这些值实际上是浮动的。 这导致我得出以下解决方案:
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.payload import BinaryPayloadDecoder
client = ModbusTcpClient('192.168.0.18', 502)
if not client.connect():
print('Error connecting')
exit()
result = client.read_holding_registers(0x1C, 2)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, '>', '>')
print(decoder.decode_32bit_float())
client.close()
解决方案确实是找到BinaryPayloadDecoder的各种方法。
有时 http 服务器关闭和 TCP 服务器关闭,但 ping 到 IP 可以正常工作。我怎样才能检查出什么问题。可能是 SSH 连接吗?