Search code examples
pythonmodbus

Pymodbus adding float to server context register


I'm new to pymodbus and modbus in general, I've been trying for some time now to add a float directly
to server context without any success, I wondered if you had any lead on how to do it. I already try to use payload by doing something like :

builder = BinaryPayloadBuilder(endian=Endian.Little)
builder.add_32bit_float(long(69000))
payload = builder.build()

context[slave_id].setValues(register, address, payload)

However I get an error about pymodbus trying to cast the payload to int, my code is mostly the one from the sync server example. Any help would be very nice


Solution

  • builder = BinaryPayloadBuilder(endian=Endian.Little)
    builder.add_32bit_float(30.3)
    payload = builder.to_registers()
    
    context[slave_id].setValues(register, address, payload)
    

    This is the correct way, take note that "register" is the starting registers and all modbus registers are 16 bit wide so a 32 bit float occupies 2 (register and register+1).