# If msg (Buffer) starts with “ABDC” direct

**URL:** <https://discourse.nodered.org/t/if-msg-buffer-starts-with-abdc-direct/31608>\
**Category:** General\
**Created:** [16 August 2020 13:31 UTC](https://discourse.nodered.org/t/if-msg-buffer-starts-with-abdc-direct/31608 "2020-08-16T13:31:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [16 August 2020 13:31 UTC](https://discourse.nodered.org/t/if-msg-buffer-starts-with-abdc-direct/31608/1 "2020-08-16T13:31:22Z")

</div>

Hi,  
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]

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 August 2020 13:48 UTC](https://discourse.nodered.org/t/if-msg-buffer-starts-with-abdc-direct/31608/2 "2020-08-16T13:48:18Z")

</div>

> [@New2Node](#):
>
> I think it will look something like
> 
> If (msg.payload == “0x1 0x50 0x30 0x2”) {return msg} else return [null]

More like...

```auto
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?

---

<div class="post-metadata">

**Author:** ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)\
**Post date:** [16 August 2020 14:40 UTC](https://discourse.nodered.org/t/if-msg-buffer-starts-with-abdc-direct/31608/3 "2020-08-16T14:40:04Z")

</div>

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 with`SOH P0 STX ` then I want to pass it to Python shell node as sys.argv to preform a new task.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 August 2020 15:00 UTC](https://discourse.nodered.org/t/if-msg-buffer-starts-with-abdc-direct/31608/4 "2020-08-16T15:00:42Z")

</div>

> [@New2Node](#):
>
> I want to pick this particular message out of a string of messages and pass it through

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

 ![Gib1mmmoob](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/cebb808c959a5457354ba7c0cbdd48f1436a4b16.gif)

```auto
[{"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"]]}]

```

> [@New2Node](#):
>
> So if the message starts with `SOH P0 STX ` then I want to pass it to Python shell node

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 August 2020 15:00 UTC](https://discourse.nodered.org/t/if-msg-buffer-starts-with-abdc-direct/31608/5 "2020-08-30T15:00:45Z")

</div>

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