3
votes

Im new to Modbus python and now i have some questions about my first steps

The Script:

from pymodbus.client.sync import ModbusTcpClient

host = '10.8.3.10'
port = 502   

client = ModbusTcpClient(host, port)
client.connect()

#Register address 0x102A (4138dec) with a word count of 1
#Value - MODBUS/TCP Connections
#Access - Read
#Description - Number of TCP connections

request = client.read_holding_registers(0x3E8,10,unit=0) 
response = client.execute(request)

print response
#print response.registers
print response.getRegister(12)
print response.registers[8]
client.close()

The result:

============= RESTART: D:\Users\mxbruckn\Desktop\read_modbus.py =============
ReadRegisterResponse (38)
0
0
>>> 

Now the questions:

  1. I read from Register 1000, 10 Words, with slave number 0. is this correct, but what means the value 38

  2. How can i read 2 Words from register 1007? my code does not work: (0x3EF,2, unit=0) Exception Response(131, 3, IllegalValue)

Ciao, Doc

1
generally you get IllegalValue error when you are trying to access invalid register address or the offsets , see if you really have the registers at 0x3EF, 0x3F0 and 0x3F1, also 38 in ReadRegisterResponse is the length of registers returned from your request.Sanju

1 Answers

6
votes

first I think you have some mistake in your code. With pymodbus 1.2.0 the code should look like this:

from pymodbus.client.sync import ModbusTcpClient

host = 'localhost'
port = 502 

client = ModbusTcpClient(host, port)
client.connect()

rr = client.read_holding_registers(0x3E8,10,unit=0)
assert(rr.function_code < 0x80)     # test that we are not an error
print rr
print rr.registers


# read 2 registers starting with address 1007
rr = client.read_holding_registers(0x3EF,2,unit=0)
assert(rr.function_code < 0x80)     # test that we are not an error
print rr
print rr.registers

And here is the output (please mention, that I instantiated the datastore on the modbusserver with 17):

ReadRegisterResponse (10)
[17, 17, 17, 17, 17, 17, 17, 17, 17, 17]
ReadRegisterResponse (2)
[17, 17]

Now to your questions:

  1. The value shows you the amount of registers you read from the server.
  2. See code above.

Hope that helps, wewa