question

Mike Casey avatar image
Mike Casey asked

Register 2700 - Cannot write as a negative number using pymodbus?

Hi guys,

Excel sheet in whitepaper section clearly says 2700 can be set as a negative number however when I call client.write_register(2700, -5000) I get the following exception


File "/usr/local/lib/python3.6/site-packages/pymodbus/register_write_message.py", line 42, in encode

packet += struct.pack('>H', self.value)

struct.error: argument out of range


Line executes as expected with a positive number. Is this a pymodbus issue? Any thougths on how to get around it?


Mike


Modbus TCP
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

1 Answer
Mike Casey avatar image
Mike Casey answered ·

Figured it out - must use a 16bit int

from pymodbus.payload import BinaryPayloadBuilder, Endian, BinaryPayloadDecoder


builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)

builder.reset()

builder.add_16bit_int(-5000)

payload = builder.to_registers()

client.write_register(2700, payload[0])

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

Related Resources