Function code to convert 4 modbus registers to 64 bit UINT - emoncms energy monitoring

1,251 views
Skip to first unread message

lmc7...@gmail.com

unread,
Aug 20, 2017, 7:26:53 AM8/20/17
to Node-RED
Please excuse the limited .js knowledge. I am stuck trying to convert the output of 4 x modbus holding registers into the  64 bit UINT data format they represent. i have searched extensively and found solutions for converting 2 x registers to a float which is working well but cannot work out how to handle the UINT64...

This is the message payload from the modbus read node - 

object
dataarray[4]
056322
170
20
30
bufferbuffer[8]raw
0220
12
20
370
40
50
60
70

The data is the output of 4 consecutive holding registers from a Carlo Gavazzi WM30 energy meter. The documentation advises that the format is UINT64 - unsigned long integer. Byte order is MSB>LSB and word order is LSW>MSW.

I am successfully polling, converting format and sending the results to emoncms for other registers using 32 bit float IEEE-754. Any advice would be greatly appreciated.

Thanks,
Luke

Dave C-J

unread,
Aug 20, 2017, 9:16:50 AM8/20/17
to node...@googlegroups.com
I'm guessing - but I'd try something like

var m = msg.payload;  // just to make it easier to read next
msg.payload = ((((m.data[3] * 65536 ) + m.data[2] ) * 65536 ) + m.data[1] ) * 65536 + m.data[0];
return msg;

lmc7...@gmail.com

unread,
Aug 20, 2017, 10:42:17 AM8/20/17
to Node-RED
Thanks Dave.

I tried this but the debug of the function output shows - TypeError: Cannot read property '3' of undefined


I will read up on js and see if i can get something like your example to work. Appreciate the input.

Luke

Colin Law

unread,
Aug 20, 2017, 10:47:43 AM8/20/17
to node...@googlegroups.com
If it does not like m.data[3] then that suggests the payload is not of
the form you posted.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/d7885f52-4ca3-4dce-b75c-b6541150ae32%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

lmc7...@gmail.com

unread,
Aug 20, 2017, 11:03:55 AM8/20/17
to Node-RED
I changed the "graphical wiring" in node red from the top - 1st i think to the lower or 2nd output pin on the modbus read node and the code now works and presents the actual meter reading in Wh perfectly. it looks like the top pin outputs the values and the bottom the raw data.

Thank you very much!
Reply all
Reply to author
Forward
0 new messages