Going crazy trying to read signed INT32 from buffer


I'm trying to convert values from a modbus power meter. The value is given as:
INT32 ( DINT Double integer ) so it's a signed INT32
LSW->MSW Means Little Endian order? right?

I have tried msg.payload=((msg.payload[1])+(-msg.payload[0] << 32));
return msg , but the value seems way too small.

This is the output from the modbus node:

I don't know how to correctly get this into a decimal value to put into my database.

Can anyone help me?

I'd reach for the buffer-parser node - node-red-contrib-buffer-parser (node) - Node-RED
and let it parse the complete buffer for you

1 Like


I downloaded and installed that but I cannot figure it out :slight_smile: Can you help me set it up for my case?

can you cut/paste the actual buffer from the debug (use the copy value button to the right of the debug window) so we have some example data.

when I copy the values it gives me: [0,4,89,237]
But the Debug node says <Buffer 00 04 59 ee>

(So these cryptic numbers need to magically become a kWh value I can feed into influxdb)

That doesn't look long enough for the complete buffer... from your spec - 16+16+32+32+64+32 = 24 bytes not 4

Many people have succeeded - so it does work.

Be sure to check out the readme, the built-in help and the example flows (ctrl+I)

1 Like

Hmm... I don't really understand that... the instructions for getting that value, the 32bit int is to get 2 words from modbus, starting at holding register 53.

  • which is what I'm doing in the modbus node.

And that's what I'm getting buffer-wise

ah - so you are just getting the one register - ok
So I would say just set

But that value does look more BE than LE to me so you may need to try some other options

1 Like

If I try

I get
which looks like a potentially realistic number

1 Like

Oh! So that's for the input, that makes sense! I thought that was for the output.
That works as BE and that gives me a value of 285169 (working with live values so they change from what you have) and I need to just divide by 10 to get kWh
However the number seems a bit too small, 28516,9 kWh is too small to be able to be correct.

Unfortunately the display in the meter has died so I cannot double check the figures... :frowning:

Are we sure that we have the word order right? The instructions confuse me.
"For all the formats the byte order (inside the single word) is MSB->LSB. In INT32, UINT32 and UINT64 formats, the word order is LSW-> MSW."

Maybe it's correct, it seems to increase steadily, if the bit or word order was wrong I guess the value would not increase normally.

You may need to do swap 16 or swap 32 on the data and mess about with the BE and LE functions

If you can identify another int32 register where you know what the value will be, you could try grabbing that and see if the current int32be is working as expected (adjust swap/le if not)

1 Like

That's a good idea, I'll see what I can find in the registers
I checked with Register 1 which is also a 32Bit INT, it is for L1 voltage which i know is supposed to be around 240. Swapping to that register in the modbus node gives 240.4 so by all accounts the byte order is correct.

Now a really Noob question:

the output seems to be {kWh: 28517.4} what format is that? how can I get just the number out to be able to put it into the database?

Edit: This function seemed to help:
var value= parseFloat(msg.payload.kWh);
msg.payload = value;
return msg

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.



1 Like

Also, you don't need to parsefloat it, it is already a float. You can use a Change node to move it into msg.payload if that is what you want. Whether you actually need to do that depends on what you want to do with it next.

1 Like

Oh I remember that now! I did all my Node Red flows some years ago and have since then forgotten a lot it seems! I was just adding a new value to my log and I was so lost :slight_smile:

Anyway, I realized the Gavazzi manual actually states the modbus addresses as starting from 1, but with my modbus reader it starts from address 0. So I was reading the wrong address. That is why the numbers didnt add up . I thought kWh tot was at address 53 but it should be read at 52.

This also meant the bytes were all screwed up, so the final setting for me in the parser as long as I was reading the correct modbus address was int32LE with a swap16

I think :stuck_out_tongue_winking_eye: I guess I'll know tomorrow if the values add up or not...

1 Like

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