ljean / modbus-tk

Create Modbus app easily with Python
Other
566 stars 212 forks source link

How to write float value in slave #136

Open capslocknanda opened 3 years ago

capslocknanda commented 3 years ago

I was using slave.set_values() command. It's working fine for integer and long. But when it comes to floating points I am not able to set a float value to it. Any help will be appreciated.

ljean commented 3 years ago

Hello, You can have a look to mysimu.py in the examples folder.

This shows how to write 3.14 in 2 registers

        pi_bytes = [int(a_byte) for a_byte in struct.pack("f", 3.14)]
        pi_register1 = pi_bytes[0] * 256 + pi_bytes[1]
        pi_register2 = pi_bytes[2] * 256 + pi_bytes[3]
        slave.set_values("foo", address, [pi_register1, pi_register2])

I hope it helps Best