I'm new to modbus but I have a small project to work on. I need to read some values from an energy meter. I wrote this from some examples found in the internet:
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()
It seems to be connecting to the meter 'cause this is my output:
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)
I want to read from register 40012
to 40014
and this is Modbusdbus map I have:
Modbus map
I appreciate your help. Regards,
I think you should set the unit
and port
argument, and for getting the value use the rr.registers
, so you need to know the unit_ID value, and the device port.
In most cases, the unit
is 1
and the port
is 502
as the modbus default.
If you want to read from address 40012
to 40014
, you could read from 40012
as a bulky reading with count=3
.
I improved your code, try it:
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()