Help with nodejs changes to new Buffer()

I have a function node that extracts some numbers from binary data. It has been running for years wonderfully but of course I decided to upgrade nodejs and nodered and new Buffer is no longer working and I can seem to understand how to change it to the new safe-buffer.

Can anyone tell me what I’m doing wrong converting new Buffer() to the new safe buffer..

Original code that worked until I upgraded…

var amps = [msg.payload.data[0],msg.payload.data[1]];
var ampsBuff = new Buffer(amps);
amps = ampsBuff.readInt16LE(0)
global.set('globalAmps', amps);

I have tried changing
var ampsBuff = new Buffer(amps) to var ampsBuff = Buffer.from(amps);

I have also tried changing
var ampsBuff = new Buffer(amps) to var ampsBuff = Buffer.alloc(amps);

Sorry buffers are not my strength. I’m just not sure how to create this safe-buffer so that I can extract the number from it.

Thanks for any help

Hi!

... can't work, as Buffer.alloc(size) only crates an empty buffer of size size.

... looks ok though - according to the documentation.

To support further, it would be helpful to know the exact issue you have...

Also tell us what version you have upgraded to. It should be 16 or 18.

Maybe some of this can get you going.

[{"id":"40bab1327d0a160d","type":"inject","z":"f1cd617e3bd1934a","name":"","props":[{"p":"payload.data","v":"[44,55]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":460,"wires":[["203fec3416a1e8eb"]]},{"id":"203fec3416a1e8eb","type":"function","z":"f1cd617e3bd1934a","name":"function 35","func":"let msg1 = {}\nlet amps = [msg.payload.data[0], msg.payload.data[1]]\nlet ampsBuff = Buffer.from(msg.payload.data)\nlet ampsBuff1 = ampsBuff.readInt16LE(0)\n\nmsg1.payload = ampsBuff1\nreturn msg1;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":460,"wires":[["cb5f68233100f104"]]},{"id":"cb5f68233100f104","type":"debug","z":"f1cd617e3bd1934a","name":"debug 98","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":460,"wires":[]}]

Got it figured out.. I looked at the raw data and there was nothing there.. turns out two things went wrong at the same time. Hardware wiring had come undone. So wired that up and then changed all the new Buffer() to Buffer.From() and it all works fine…

Thanks for taking the time to offer help.

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