If msg (Buffer) starts with “ABDC” direct

How do I direct a payload (Buffer) to a particular location depending on start values in the payload.

For example I have a buffer which starts with
[0x1 0x50 0x30 0x2]

I want to send the message to a function note to preform a task, else I want it to “Do nothing”

I think it will look something like

If (msg.payload == “0x1 0x50 0x30 0x2”) {return msg} else return [null]

More like...

if(Buffer.isBuffer(msg.payload) && msg.payload.length >= 4) {
    if(msg.payload[0] != 0x1) return null;
    if(msg.payload[1] != 0x50) return null;
    if(msg.payload[2] != 0x30) return null;
    if(msg.payload[3] != 0x2) return null;
return msg;

However, this looks like serial data with ascii characters in (SOH P0 STX) - there may be a better way to handle all this.

What is the overall goal and can you describe your project in a bit more detail?

Yes it’s serial data, so your right it’s controll characters.

I want to pick this particular message out of a string of messages and pass it through a Python script. It starts with the same “PO” data.

So if the message starts withSOH P0 STX then I want to pass it to Python shell node as sys.argv to preform a new task.

So the function i wrote will do just that. See here...

[{"id":"96eaea5a.44b318","type":"inject","z":"db525c47.ab486","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":860,"y":120,"wires":[["53cf112.94802f"]]},{"id":"53cf112.94802f","type":"function","z":"db525c47.ab486","name":"fake serial buffer SOH P0 STX","func":"msg.payload = Buffer.from([0x1, 0x50, 0x30, 0x2, 109,111,114,101,32,100,97,116,97,32,104,101,114,101, 0x3])\nreturn msg;","outputs":1,"noerr":0,"x":1070,"y":120,"wires":[["c83b3cbd.0f4fe"]]},{"id":"c83b3cbd.0f4fe","type":"function","z":"db525c47.ab486","name":"Is P0?","func":"if(Buffer.isBuffer(msg.payload) && msg.payload.length >= 4) {\n    if(msg.payload[0] != 0x1) return null;\n    if(msg.payload[1] != 0x50) return null;\n    if(msg.payload[2] != 0x30) return null;\n    if(msg.payload[3] != 0x2) return null;\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":1290,"y":160,"wires":[["53a1d8b.acea928"]]},{"id":"53a1d8b.acea928","type":"debug","z":"db525c47.ab486","name":"So, this message IS SOH P0 STX - lets do something with it","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1100,"y":220,"wires":[]},{"id":"2d5250b0.9339b","type":"inject","z":"db525c47.ab486","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":860,"y":160,"wires":[["69dcb19e.b24af"]]},{"id":"69dcb19e.b24af","type":"function","z":"db525c47.ab486","name":"fake serial buffer SOH P7 STX","func":"msg.payload = Buffer.from([0x1, 0x50, 0x37, 0x2, 109,111,114,101,32,100,97,116,97,32,104,101,114,101, 0x3])\nreturn msg;","outputs":1,"noerr":0,"x":1070,"y":160,"wires":[["c83b3cbd.0f4fe"]]}]

So do you mind me asking why you want to pass the data to python? Perhaps you can avoid the overhead of shelling python processes & do it all in node-red?

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