我是Modbus的新手,但是我有一个小项目需要处理。我需要从电表读取一些值。我是从互联网上的一些示例中写出来的:
import logging
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
client = ModbusClient('192.168.80.210')
client.connect()
rr = client.read_holding_registers(40012, 1)
print rr
client.close()
似乎正在连接到电表,因为这是我的输出:
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x9c 0x4c 0x0 0x1
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 8 bytes (0 received))
DEBUG:pymodbus.framer.socket_framer:Processing:
DEBUG:pymodbus.transaction:Getting transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 8 bytes (0 received)
我想从寄存器40012
读取到40014
,这是我拥有的Modbusdbus映射:
Modbus map
感谢您的帮助。问候
答案 0 :(得分:0)
我认为您应该设置unit
和port
参数,并使用rr.registers
来获取该值,因此您需要知道unit_ID值和设备端口。 / p>
在大多数情况下,默认的unit
是1
,port
是502
。
如果您想从地址40012
到40014
进行读取,则可以使用40012
从count=3
进行读取。
我改进了您的代码,尝试一下:
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('192.168.80.210', port=502)
if client.connect():
res = client.read_holding_registers(40012, count=3, unit=1)
if not res.isError():
'''.isError() was implemented in pymodbus version 1.4.0 and above.'''
print(res.registers)
else:
# handling error
print(res)
client.close()