How to read a 32bit signed on ModBus

Hi everyone !

I have a problem with a Socomec I31. It send me a DWord Signed on ModBus and I can't add the two Word for make a Decimal Value.

How I can read my S32 in decimal ?

Thank you !

Feed the value into a debug node and post the result here so we can see exactly what you have

Hi !

This is the result of my debug node :

Blockquote :
msg.payload : array[2]
array[2]
0: 65535
1: 65534

Thanks !

I should think you need something like

msg.payload = (msg.payload[0] << 16) + msg.payload[1]
return msg

Your code return me :

-2

which would be correct for a 32 bit signed int when passed that data.
In hex that is FFFFFFFE which is -2.

I create a function :

msg.payload=parseInt(msg.payload[0]+msg.payload[1] << 16);
return msg;

The message returned is : -3604480

With a Usigned 32 bits it's OK...

I don't understand what you are saying. What result are you trying to get?

In my test function :

msg.payload=(msg.payload[1] << 32)+msg.payload[0]
return msg;

In my simple add function :

msg.payload=parseInt(msg.payload[0]+msg.payload[1] << 16);
return msg;

You have not made it clear whether everything is now working as you expect. If not the what output are you expecting?

Sorry,

It does'nt work. I expected read 1 500w or even 2 000w...

Well the problem is not in the conversion. The value 0xfffffffe as a 32 bit signed integer is -2. Considering it as an unsigned integer gives a huge number.

Yeah, the MSB is : 65535, but, how do I read those two words correctly ?

I have found the solution.

For me, I read with this function :

msg.payload=(msg.payload[0])+(-msg.payload[1] << 32)
return msg;

And it's work !

In output, it's my value in decimal.

Thanks @Colin

Can you give an example of the data in and out for that formula, I would like to see why it works.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.