Modbus TCP array to readable value

Hi,
I've been through several sites and forums but i'm still struggling to read a frequency value from a power meter to node red. The debug node shows an array of two values but i don't know how to manipulate them into the actual value of around 50Hz.
From another app called modbus poll i can pull up the register and the convert it to float CD AB and it works.

msg.payload = [0, 16833];
let pay = msg.payload;

const buf = Buffer.allocUnsafe(4);
buf.writeInt16BE(pay[0],2);
buf.writeInt16BE(pay[1],0);

msg.payload = buf.readFloatBE(0);
return msg;

This code does the trick but i'm not sure how to inject the message from the modbus read node instead of using the constant "[0, 16833]"

1 Like

Attach a debug node to the output of the modbus node.

See how the values are returned in msg.payload and look similar?

So wire the modbus node to your function node and in your function node - instead of setting the msg.payload to an array, just don't. (e.g. delete the top line )

Good day

I managed to get some modbus communcations to work on a power electronics drive. what is the actual address that you want to access perhaps I can help.

Hi,

This is the device and register
Phoneix Contact: EEM-MA770
Address (dec): 32780
Address (hex): 0x800C
Number of registers: 2
Name: Frequency
Unit: Hz
Factor: 1
Read only

A debug node returns this out of the Modbus read node:
array[2]
0: 15991
1: 16968

After removing the msg.payload it works but sometimes it returns an error:
"RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= -32768 and <= 32767. Received 33554"

When the value is in range i also need to round it off to 2 decimals from 50.01499938964844

[{"id":"3a59fc59.ee0454","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"74dd717f.076f2","type":"modbus-client","z":"","name":"Power meter","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.0.98","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true},{"id":"f90ef26a.a529a","type":"modbus-client","z":"","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"29344771.8d9648","type":"ui_group","z":"","name":"GSM network","tab":"5abf1f53.dc4c7","order":1,"disp":true,"width":"6","collapse":false},{"id":"60fd2c2e.5d5ce4","type":"ui_group","z":"","name":"Router","tab":"5abf1f53.dc4c7","order":2,"disp":true,"width":"6","collapse":false},{"id":"48110a04.687a34","type":"ui_group","z":"","name":"Data usage","tab":"5abf1f53.dc4c7","order":3,"disp":true,"width":"6","collapse":false},{"id":"bb35e927.f880e8","type":"modbus-client","z":"","name":"Router","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"192.168.1.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"5abf1f53.dc4c7","type":"ui_tab","z":"","name":"Router","icon":"router","order":5,"disabled":false,"hidden":false},{"id":"22905fa6.bb9e8","type":"modbus-client","z":"","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectTimeout":2000},{"id":"7abaf24b.aedc1c","type":"modbus-read","z":"3a59fc59.ee0454","name":"Frequency","topic":"Frequency","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"255","dataType":"HoldingRegister","adr":"32780","quantity":"2","rate":"5","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"74dd717f.076f2","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":500,"y":220,"wires":[["99cc33f3.dd3c9","68c1bd80.92d7f4"],["44ce6e95.72803"]]},{"id":"587f0034.ec73b","type":"debug","z":"3a59fc59.ee0454","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":300,"wires":[]},{"id":"44ce6e95.72803","type":"function","z":"3a59fc59.ee0454","name":"","func":"let pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeInt16BE(pay[0],2);\nbuf.writeInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":300,"wires":[["587f0034.ec73b"]]},{"id":"99cc33f3.dd3c9","type":"debug","z":"3a59fc59.ee0454","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":220,"wires":[]},{"id":"63368447.81be0c","type":"debug","z":"3a59fc59.ee0454","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":260,"wires":[]},{"id":"68c1bd80.92d7f4","type":"function","z":"3a59fc59.ee0454","name":"","func":"let pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeInt16BE(pay[0],2);\nbuf.writeInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":260,"wires":[["63368447.81be0c"]]}]

Hi,
Simple solution was to just make it a Uint and it works

let pay = msg.payload;

const buf = Buffer.allocUnsafe(4);
buf.writeUInt16BE(pay[0],2);
buf.writeUInt16BE(pay[1],0);

msg.payload = buf.readFloatBE(0);
return msg;

So you changed the top line as I suggested?

yes thanks that got the data into the function and then the data type just needed to be a uint

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