Modbus/Arrays extract divide and give in stream

#1

I don't understand how to grab and
divide array
He comes in the form

msg.payload : Object
object
data: array[2]
0: 317
1: 205
buffer: buffer[4]raw
0: 0x1
1: 0x3d
2: 0x0
3: 0xcd

I need to consistently pull out the data temperature and humidity.
How do I write a function?
What I wrote does not work...
FunctionBlock
{
...
var TH = [ ];
for (var i=0;i<10;i++)
{
TH[i] = { payload: msg.payload[i] };
}
return TH;
...
}

#2

I would use the helper functions provided by the NodeJS buffer in msg.payload.buffer.

As far as I remember Modbus uses Big Endian, so readInt16BE() might be used in this case.

Have a look at https://nodejs.org/docs/latest-v8.x/api/buffer.html.

EDIT: Have a look at the array msg.payload.data. These seem to already contain the decoded values of your Modbus registers, so you can use them directly.

Something like:

let temp = msg.payload.data[0];
let humidity = msg.payload.data[1];

The rest depends on what you want to do with these values.

1 Like
#3
[{"id":"48d06761.fd16d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4fe752d5.9b481c","type":"debug","z":"48d06761.fd16d8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":680,"y":360,"wires":[]},{"id":"5a3bdd91.26c744","type":"modbus-read","z":"48d06761.fd16d8","name":"TH","topic":"","showStatusActivities":true,"logIOActivities":false,"showErrors":true,"unitid":"50","dataType":"InputRegister","adr":"1","quantity":"2","rate":"1","rateUnit":"m","delayOnStart":false,"startDelayTime":"","server":"3f5eb7a5.24f1d8","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":170,"y":220,"wires":[["d7328020.9b962"],[]]},{"id":"d9acf9dc.c75418","type":"function","z":"48d06761.fd16d8","name":"ConvertData","func":"let base = new ArrayBuffer(4); //4 bytes\nlet int16_view = new Int16Array(base);\nlet float32_view = new Float32Array(base);\n\nfloat32_view[0] = msg.payload;\n\nlet rValue = [];\nrValue.push(int16_view[0]);\nrValue.push(int16_view[1]);\n\nmsg.payload = rValue;","outputs":1,"noerr":0,"x":530,"y":180,"wires":[[]]},{"id":"d7328020.9b962","type":"change","z":"48d06761.fd16d8","name":"","rules":[{"t":"set","p":"payload.th","pt":"msg","to":" (payload.TH)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":240,"wires":[["4fe752d5.9b481c"]]},{"id":"3f5eb7a5.24f1d8","type":"modbus-client","z":"","name":"2ndser","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM8","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]

"TypeError: Cannot read property '0' of undefined"

I can't open an array yet.
But if remove the function, the array comes entire [ ccc , hhh ]
...
I don’t know how to pull these numbers out of the array. =(

#4

Did you realise you have posted a flow containing just a debug node and nothing else?
See here for how to post a flow in this forum.

#5

Ok! Everything worked out! Address for my device = 50 (d)

[{"id":"48d06761.fd16d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4fe752d5.9b481c","type":"debug","z":"48d06761.fd16d8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":580,"y":240,"wires":[]},{"id":"5a3bdd91.26c744","type":"modbus-read","z":"48d06761.fd16d8","name":"TH","topic":"","showStatusActivities":true,"logIOActivities":false,"showErrors":true,"unitid":"50","dataType":"InputRegister","adr":"1","quantity":"2","rate":"1","rateUnit":"m","delayOnStart":false,"startDelayTime":"","server":"3f5eb7a5.24f1d8","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":110,"y":100,"wires":[["d7328020.9b962"],[]]},{"id":"d9acf9dc.c75418","type":"function","z":"48d06761.fd16d8","name":"ConvertData","func":"var arr = msg.payload;\nvar temp = arr[0];\nvar hum = arr[1];\nmsg.payload =hum;\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":120,"wires":[["4fe752d5.9b481c"]]},{"id":"d7328020.9b962","type":"change","z":"48d06761.fd16d8","name":"","rules":[{"t":"set","p":"payload.th","pt":"msg","to":" (payload.TH)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":120,"wires":[["d9acf9dc.c75418","53d501e8.fdb63"]]},{"id":"53d501e8.fdb63","type":"function","z":"48d06761.fd16d8","name":"ConvertData","func":"var arr = msg.payload;\nvar temp = arr[0];\nvar hum = arr[1];\nmsg.payload =temp;\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":80,"wires":[["4fe752d5.9b481c"]]},{"id":"3f5eb7a5.24f1d8","type":"modbus-client","z":"","name":"2ndser","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM8","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]