cancel
Showing results for 
Search instead for 
Did you mean: 

Regarding modbus to write negative values.

Lchal.1
Associate II

Hi, i am new to stm32 i need to write negative values (signed integers or float) in my modbus and transmit it to my PC. But i am not able to write negative values it shows some other values if i transmit negative values . can anyone help me with it please if you know the answer.

Thank you

5 REPLIES 5
Ozone
Lead II

The modbus spec only knows bits (called single coils) and registers, which are just 16-bit values.

https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf

Any interpretation (signed/unsigned) happens in the receiver.

> But i am not able to write negative values it shows some other values if i transmit negative values 

You could be more specific why do you think that.

What do you observe ?

What do you expect, and what do you see ?

if i write -800 in modbus and transmit it to my pc . i am going to receive 64736. but i need to get -800.

Should i need to write any line of code to modify the value coming from my modbus.

Thank you

That is the correct (-800) value, just cast it to the desired 16-bit signed integer type. :)

if(Target_Pos1[0] > 32767 && Target_Pos1[0] < 65535)

 {

   Target_Pos1[0] = 65536 - Target_Pos1[0] ;

 }

i did like this is it correct and my Target_Pos1[0] is my buffer where my modbus value -800 is storing.

Thank you