Modbus Write to Register

Hello, I am attempting to write a modbus value over TCP to a register. I am able to do this but I can only write integer values, meaning no decimal values. Is there a way to allow writing of float values to external modbus registers?

You can’t write a decima value because the register is a word of 16bit

Hello,

I might be a bit late to the party with my response but to help others that try to do the same thing: it is in fact possible to perform a multi-16bit write to a register over modbus to write e.g. a float32 value.

To do this you need to pass the consecutive 16-bit values as an array to the write_register function.

E.g. to write the decimal value 10 to a float32 register use the following:

service: modbus.write_register
 data:
   address: <target register address>
   unit: <target slave address>
   hub: <hub name>
   value: [0x4120, 0x0000]

I’ve also added a pull request to the modbus documentation page to add this example.

Best regards,
Hollie.

1 Like

Dear Holllie, old thread but maybe you’re kind to help ?
I try to send the value of an entity ( [sensor.esp32_scale_dominic_s_weight] coming from a wireless scale to a Float ModBus register loacted in a PLC. Using your example above, I see well the value “10.0” reaching the register in the PLC so the service works.
Now here is my question is: how can I transform a decimal value (eg. 82.5) coming from an entity into an “array” that will be understood by this service ? Thanks in advance.

Hey @Dominicd

please see this example code that you can modify to your needs:

Best regards,
Hollie

Thank you Hollie to have spent time on this: much appreciated. I love this community ! :slight_smile:

1 Like