How to read strings and negative floats?

181 views
Skip to first unread message

Jaco Fourie

unread,
Nov 5, 2020, 12:20:29 AM11/5/20
to libmodbus

Hi all.

 

I am able to read float values from my VSD. How do I read strings and negative floats using this lib with serial RTU.

I can read the values using Python with the minimal modbus lib. But I am developing and application with a free pascal binding.

 

This is the Python code using Minimalmodbus

 

amps = vsd.read_register(registeraddress=0x0000,number_of_decimals=2, functioncode=3 ,signed=True)

serial_number = vsd.read_string(170, number_of_registers=10, functioncode=3)

 

 

This is the free pascal code using the libmodbus binding

 

ret := modbus_read_registers(ctx, 0, Length(regbuf), regbuf);

amps :=  regbuf[0] / 100;

 

amps is fine as long as it stays positive. As soon as I have a negative value it will give me the wrong value.

 

I have no idea how to get strings with this lib as I do not see a function for it.

 

Thanks.

Jaco Fourie

unread,
Nov 5, 2020, 3:42:56 AM11/5/20
to libmodbus
I had a look at the way it is done in Python and this is the comment


Some manufacturers allow negative values for some registers. Instead of

an allowed integer range 0 to 65535, a range -32768 to 32767 is allowed.

This is implemented as any received value in the upper range (32768 to

65535) is interpreted as negative value (in the range -32768 to -1).


Use the parameter ``signed=True`` if reading from a register that can hold

negative values. Then upper range data will be automatically converted into

negative return values (two's complement).


============== ================== ================ ===============

``signed`` Data type in slave Alternative name Range

============== ================== ================ ===============

:const:`False` Unsigned INT16 Unsigned short 0 to 65535

:const:`True` INT16 Short -32768 to 32767
============== ================== ================ ===============


As the buffer type is unsigned 16-bit integer how will I be able to get a - value?

Jaco Fourie

unread,
Nov 6, 2020, 3:05:58 PM11/6/20
to libmodbus
OK I have got it working
Reply all
Reply to author
Forward
0 new messages