Search code examples
pythontcpmodbus

Pymodbus - Simple program


I have been working with a simple program using pymodbus library in python. This is the sample program I found with the library documentation. The code is as follows

from pymodbus.client.sync import ModbusTcpClient

client = ModbusTcpClient('192.168.10.3')
client.write_coil(410001, False)
result = client.read_coils(410001,1,unit=1)
print result.bits[0]
client.close()

I am getting the error

Traceback (most recent call last):
  File "start_2407.py", line 4, in <module>
    client.write_coil(410001, False)
  File "build\bdist.win-amd64\egg\pymodbus\client\common.py", line 61, in write_coil
  File "build\bdist.win-amd64\egg\pymodbus\client\sync.py", line 131, in execute
  File "build\bdist.win-amd64\egg\pymodbus\client\sync.py", line 46, in execute
  File "build\bdist.win-amd64\egg\pymodbus\transaction.py", line 243, in buildPacket
  File "build\bdist.win-amd64\egg\pymodbus\bit_write_message.py", line 58, in encode
struct.error: 'H' format requires 0 <= number <= 65535

Do I need to provide the address locations in hexadecimal? I have tried that also but the output do not match with that which I get from Modscan2.


Solution

  • Modbus variables are addressed in the 0-65535 range. You can have up to 65536 coils, discrete inputs, input registers and holding registers. You are not allowed to use 410001 as an input to PyModbus. 410001 is a very conventional (not standard) way to represent the 10000th holding register. Yes, it's strange. Modbus vendors are very creative when coming up with their memory maps.

    You can read that register using the read_holding_registers method with address=10000.