Using push into a for loop

Hi there,

I'm trying to push values from modbus into an array. The values are encoded i floating format. I managed to convert bytes (4) into single floating :sweat_smile:
The "function" node returns all this values into an array if I push them one by one:

[{"id":"b2559ff0.bd2228","type":"modbus-read","z":"4f90fdb9.b07554","name":"","topic":"","showStatusActivities":true,"logIOActivities":false,"showErrors":false,"unitid":"","dataType":"HoldingRegister","adr":"121","quantity":"6","rate":"5","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"badbc08.08534c","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":190,"y":180,"wires":[[],["ab597b10.1d0db8"]]},{"id":"ab597b10.1d0db8","type":"function","z":"4f90fdb9.b07554","name":"","func":"const buf = Buffer.from(msg.payload.buffer);\nbuf.swap16();\n\nvar my_list, i;\nconst len = (msg.payload.buffer.length);\nmy_list = [];\n\n// push values 1 by 1 in array\nmy_list.push(buf.readFloatLE(0));\nmy_list.push(buf.readFloatLE(4));\nmy_list.push(buf.readFloatLE(8));\n\n// push values in for loop\n//for (i=0; i<=len; i=+ 4){\n//    my_list.push(buf.readFloatLE(i));\n//}\n\nmsg.payload = my_list;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":80,"wires":[["fdbbcc26.59d768"]]},{"id":"fdbbcc26.59d768","type":"debug","z":"4f90fdb9.b07554","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":710,"y":80,"wires":[]},{"id":"badbc08.08534c","type":"modbus-client","z":"","name":"KACO","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"192.168.200.111","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"}]
]

It wouldn't be a problem if there were just few values. But I have plenty of registers. To make my code more efficient, I need to push all this values into an array inside a for loop (ref. the commented lines in the code above), but when I run this part of code, Node-Red crashes every time.
Some help would be appreciated :slight_smile:

Regards,

Yoba

Maybe its the
i<=len
that should be
i<len
?

Have a look at node-red-contrib-buffer-parser. Whilst it may not do exactly what you are asking for (or may, I am not sure) it may be that it would be useful for you.

wrong syntax

for (i=0; i<=len; i=+ 4){

correct syntax

for (i=0; i<=len; i += 4){

Thank you @hotNipi for your quick answer :slight_smile:

@yoba as @Colin suggested, buffer parser takes all the pain out of making sense of your data.

e.g...

Demo flow...

[{"id":"ab597b10.1d0db8","type":"function","z":"5bd4aa78.31d114","name":"","func":"const buf = Buffer.from(msg.payload.buffer);\nbuf.swap16();\n\nvar my_list, i;\nconst len = (msg.payload.buffer.length);\nmy_list = [];\n\n// push values 1 by 1 in array\nmy_list.push(buf.readFloatLE(0));\nmy_list.push(buf.readFloatLE(4));\nmy_list.push(buf.readFloatLE(8));\n\n// // push values in for loop\n// for (i=0; i<len; i=+ 4){\n//     my_list.push(buf.readFloatLE(i));\n// }\n\nmsg.payload = my_list;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":924,"y":96,"wires":[["fdbbcc26.59d768"]]},{"id":"fdbbcc26.59d768","type":"debug","z":"5bd4aa78.31d114","name":"Function","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1100,"y":96,"wires":[]},{"id":"cb3e7a56.719758","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":623,"y":192,"wires":[["ed6c26a7.0bfb08"]],"l":false},{"id":"ed6c26a7.0bfb08","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":192,"wires":[["25617943.c50d26"]]},{"id":"25617943.c50d26","type":"buffer-parser","z":"5bd4aa78.31d114","name":"","data":"payload.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"item2","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"item3","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":934,"y":192,"wires":[["b1fec79a.da4a78"]]},{"id":"b1fec79a.da4a78","type":"debug","z":"5bd4aa78.31d114","name":"Parser","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1090,"y":192,"wires":[]},{"id":"c2aee09b.5ac13","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":623,"y":96,"wires":[["567c6031.02666"]],"l":false},{"id":"567c6031.02666","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":96,"wires":[["ab597b10.1d0db8"]]},{"id":"97782186.7c8bf","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":623,"y":464,"wires":[["c8767342.8d7d3"]],"l":false},{"id":"c8767342.8d7d3","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus 3 floats","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":740,"y":464,"wires":[["102a549f.38297b"]]},{"id":"102a549f.38297b","type":"buffer-parser","z":"5bd4aa78.31d114","name":"","data":"payload.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"item1","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":934,"y":464,"wires":[["b365ecd6.bfe85"]]},{"id":"b365ecd6.bfe85","type":"debug","z":"5bd4aa78.31d114","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1110,"y":464,"wires":[]},{"id":"ed450c73.8a799","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":623,"y":512,"wires":[["d0aa6936.981118"]],"l":false},{"id":"d0aa6936.981118","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus 5 floats","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":740,"y":512,"wires":[["102a549f.38297b"]]},{"id":"11dd3da7.31a7f2","type":"comment","z":"5bd4aa78.31d114","name":"Dynamic length","info":"","x":656,"y":432,"wires":[]},{"id":"73e2049d.d0ac6c","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":623,"y":304,"wires":[["f7146e75.96401"]],"l":false},{"id":"f7146e75.96401","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":304,"wires":[["4b1fe3a0.88767c"]]},{"id":"4b1fe3a0.88767c","type":"buffer-parser","z":"5bd4aa78.31d114","name":"","data":"payload.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"item1","offset":0,"length":3,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":934,"y":304,"wires":[["6dff71a4.7b454"]]},{"id":"6dff71a4.7b454","type":"debug","z":"5bd4aa78.31d114","name":"Parser","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1090,"y":304,"wires":[]},{"id":"c04113cd.82e42","type":"comment","z":"5bd4aa78.31d114","name":"As a fixed length array","info":"","x":676,"y":272,"wires":[]},{"id":"4c462fe4.cd887","type":"comment","z":"5bd4aa78.31d114","name":"As 3 items (buffer parser)","info":"","x":686,"y":160,"wires":[]},{"id":"34e9a1cf.4082ee","type":"comment","z":"5bd4aa78.31d114","name":"As 3 items (original)","info":"","x":666,"y":64,"wires":[]},{"id":"e7cd2782.f17988","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":639,"y":800,"wires":[["a8479ff8.65711"]],"l":false},{"id":"a8479ff8.65711","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":736,"y":800,"wires":[["489df60e.e63538"]]},{"id":"489df60e.e63538","type":"buffer-parser","z":"5bd4aa78.31d114","name":"","data":"payload.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"voltage","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"current","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"freq","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":950,"y":800,"wires":[["852c70cc.67a61"]]},{"id":"852c70cc.67a61","type":"debug","z":"5bd4aa78.31d114","name":"Parser","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1106,"y":800,"wires":[]},{"id":"3cb3e1e2.c74fce","type":"comment","z":"5bd4aa78.31d114","name":"As named items in an object","info":"","x":712,"y":768,"wires":[]},{"id":"cfcbc82e.a1b488","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":639,"y":944,"wires":[["7481222b.c1f99c"]],"l":false},{"id":"7481222b.c1f99c","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":736,"y":944,"wires":[["454201fc.cc2a8"]]},{"id":"454201fc.cc2a8","type":"buffer-parser","z":"5bd4aa78.31d114","name":"","data":"payload.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"voltage","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"current","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"freq","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":950,"y":944,"wires":[["d393f9c4.cdf1c8"]]},{"id":"d393f9c4.cdf1c8","type":"debug","z":"5bd4aa78.31d114","name":"Parser","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1106,"y":944,"wires":[]},{"id":"785aa1dd.9dcad","type":"comment","z":"5bd4aa78.31d114","name":"As individual named items ","info":"","x":702,"y":912,"wires":[]},{"id":"6cab2d8c.6957a4","type":"inject","z":"5bd4aa78.31d114","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":639,"y":1216,"wires":[["ca7502dc.43e59"]],"l":false},{"id":"ca7502dc.43e59","type":"function","z":"5bd4aa78.31d114","name":"Fake Modbus","func":"msg.payload = {\n    buffer : Buffer.from([0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":736,"y":1216,"wires":[["d05a32ed.38439"]]},{"id":"d05a32ed.38439","type":"buffer-parser","z":"5bd4aa78.31d114","name":"","data":"payload.buffer","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"voltage","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"current","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"floatle","name":"freq","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":3,"x":950,"y":1216,"wires":[["3af61c42.774c84"],["5ac54fd0.f5baa"],["b31006a2.ad4f58"]]},{"id":"3af61c42.774c84","type":"debug","z":"5bd4aa78.31d114","name":"float 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1106,"y":1184,"wires":[]},{"id":"8bd1cac3.6f5098","type":"comment","z":"5bd4aa78.31d114","name":"As individual items fanned out","info":"","x":712,"y":1184,"wires":[]},{"id":"5ac54fd0.f5baa","type":"debug","z":"5bd4aa78.31d114","name":"float 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1106,"y":1216,"wires":[]},{"id":"b31006a2.ad4f58","type":"debug","z":"5bd4aa78.31d114","name":"float 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1106,"y":1248,"wires":[]}]

Hi @Steve-Mcl ,

I took a look at the buffer parser as @Colin suggested. It's exactly what I need. The only problem is I use IoT2040 from Siemens, and I tried to install it without any success. I wonder if the problem is coming from the Debian used. It's a Yocto Rocko.
Do you have any other suggestion of OS to use on IoT2040?

I really appreciate all the good and quick answers. This forum rocks !!!

Ah, that old chestnut. An overpriced out of date piece of hardware!

The reason is likely the node version on that unit is obsolete.

The solution is: replace it with a Ā£35 raspberry pi :wink:

Mind you, that said, i am certain it still installs - just dont try to use BigInt64

What error do you get?

EDIT:
Are you using custom firmware on the IoT2040 ?

What node version is installed?

from a terminal, enter node -v

Also, what node-red version are you on?

Hi again,

Sorry for the late answer. I was busy trying to install new image (v2.6.1) from Siemens. Cause I was not sure which version was running on SD card. I used this time npm commands from the terminal and installed buffer parser inside folder .node-red
To answer your question, the versions are:
root@iot2000:~# node -v
v8.16.0
root@iot2000:~# npm -v
6.4.1

Are they the latest versions?

No where near. Node 8's End of Life is December 31, 2019

But I think buffer-parser will install on V8

Try and if it fails, show me the error(s)

PS: dont forget to tell me NODE-RED version. (its on the node-red menu)

Node-RED version (from menu): v0.20.7

wow - yeah - that wont work either.

I do believe you can update node-red to very latest V1.2.9 (even on node v8)

npm install -g --unsafe-perm node-red

Hello!

I struggled to upgrade this IoT2000, but I managed to get this result:
18 Mar 15:24:28 - [info] Node-RED version: v1.2.9
18 Mar 15:24:28 - [info] Node.js version: v8.16.0
18 Mar 15:24:28 - [info] Linux 4.4.185-cip35 ia32 LE

So now I have buffer parser into Node Red as you recommended, but I get error:
Error: data is not an array or a buffer

[{"id":"67a1ac1.d025d54","type":"buffer-parser","z":"ea67c445.9798f8","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatle","name":"item1","offset":0,"length":4,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"object","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":430,"y":280,"wires":[["fbd9cc4a.104be"]]}]

I've tried different properties, but it didn't help. Could you please tell me how to convert msg.payload (array of integers) into individual topic/payload messages. It's for sending MQTT :slight_smile:

image

Also, check out the actual working demo i posted...

Hi @Steve-Mcl ,

Thank you for your quick answer. I appreciate a lot.
I didn't know that the property (as you circled) could be changed. I set it to msg.payload.buffer
Now everthing seems to work properly :ok_hand:

Bonus question: could it be possible to put your sample code into the node contrib?
https://flows.nodered.org/node/node-red-contrib-buffer-parser
This one was way better than the two presented

Have a nice week-end

I will consider it :stuck_out_tongue:

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