# Struggeling with function to devide serial node response

**URL:** <https://discourse.nodered.org/t/struggeling-with-function-to-devide-serial-node-response/75584>\
**Category:** General\
**Created:** [22 February 2023 13:06 UTC](https://discourse.nodered.org/t/struggeling-with-function-to-devide-serial-node-response/75584 "2023-02-22T13:06:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DirkB](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dirkb/32/76546_2.png) [@DirkB](https://discourse.nodered.org/u/DirkB)\
**Post date:** [22 February 2023 13:06 UTC](https://discourse.nodered.org/t/struggeling-with-function-to-devide-serial-node-response/75584/1 "2023-02-22T13:06:23Z")

</div>

Hello all,

I am pretty much stuck with the following problem.  
I am querying a device via serial node which is providing me with data.  
But the response of the device consists of different parts.  
The first part is an "OK I am ready" (0x63). Then I send a "Fine, send me the data" (0x6C). The device then answers with the complete data set consisting of 256 bytes which I want to process. So far everything works.  
Now I want to separate the 0x63 response from the 256 byte response with a function. According to the motto "If you are 0x63 then output1, if you are the 256Byte then output2".  
Could someone please assist me with this. The function I have created so far somehow makes no difference between the two data contents.

Here is the flow for info and a screen shot for understanding.

Thank you very much in advance for your appreciated help.

Many greetings  
Dirk

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68eb5d80911bedf3353fe148acf99ff18217f3b5.png)

```auto
[
    {
        "id": "3a58b49072fa5ca5",
        "type": "inject",
        "z": "2d4d43d8d26b1255",
        "name": "sende 0x0F",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": true,
        "onceDelay": "0.2",
        "topic": "",
        "payload": "[\"0x0F\"]",
        "payloadType": "bin",
        "x": 190,
        "y": 120,
        "wires": [
            [
                "a346d2459f1c05a2"
            ]
        ]
    },
    {
        "id": "a6eb3487afa0916b",
        "type": "debug",
        "z": "2d4d43d8d26b1255",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 760,
        "y": 160,
        "wires": []
    },
    {
        "id": "dac2d985e2624677",
        "type": "inject",
        "z": "2d4d43d8d26b1255",
        "name": "sende 0x6C",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "10",
        "crontab": "",
        "once": true,
        "onceDelay": "0.2",
        "topic": "",
        "payload": "[\"0x6C\"]",
        "payloadType": "bin",
        "x": 190,
        "y": 200,
        "wires": [
            [
                "a346d2459f1c05a2"
            ]
        ]
    },
    {
        "id": "a346d2459f1c05a2",
        "type": "serial request",
        "z": "2d4d43d8d26b1255",
        "name": "USB2TTL",
        "serial": "075b53648c67eb04",
        "x": 360,
        "y": 160,
        "wires": [
            [
                "a6eb3487afa0916b",
                "d9366dc81308a172"
            ]
        ]
    },
    {
        "id": "d9366dc81308a172",
        "type": "function",
        "z": "2d4d43d8d26b1255",
        "name": "function 2",
        "func": "var payload = msg.payload\n\nif (payload===[99]){ //if the incoming data is 0x63 send the data to output 1 and send nothing to output 2\n return [msg,null]\n} else \n return [null,msg] // if the incoming data is not 0x63 send nothing to output 1 and the data\n // I would like to work with ( 256 bytes) send to output 2\n\nreturn msg;",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 560,
        "y": 260,
        "wires": [
            [
                "1b01097e76208abb"
            ],
            [
                "dc428cf8f4828fd6"
            ]
        ]
    },
    {
        "id": "1b01097e76208abb",
        "type": "debug",
        "z": "2d4d43d8d26b1255",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 760,
        "y": 240,
        "wires": []
    },
    {
        "id": "dc428cf8f4828fd6",
        "type": "debug",
        "z": "2d4d43d8d26b1255",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 760,
        "y": 320,
        "wires": []
    },
    {
        "id": "075b53648c67eb04",
        "type": "serial-port",
        "serialport": "/dev/serial/by-id/usb-FTDI_TTL232R_FTB5MUAY-if00-port0",
        "serialbaud": "115200",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "500",
        "bin": "bin",
        "out": "interbyte",
        "addchar": "",
        "responsetimeout": "1000"
    }
]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 February 2023 13:26 UTC](https://discourse.nodered.org/t/struggeling-with-function-to-devide-serial-node-response/75584/2 "2023-02-22T13:26:04Z")

</div>

> [@DirkB](#):
>
> Now I want to separate the 0x63 response from the 256 byte response with a function. According to the motto "If you are 0x63 then output1, if you are the 256Byte then output2".

Surely all you need is a split node so that the 63 response triggers 1 leg of a flow and everything else goes down a different leg.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [22 February 2023 13:40 UTC](https://discourse.nodered.org/t/struggeling-with-function-to-devide-serial-node-response/75584/3 "2023-02-22T13:40:06Z")

</div>

There is also a `Buffer.compare` method that you may use ([source](https://www.geeksforgeeks.org/node-js-buffer-compare-method/))

```auto
var buf = msg.payload

//if the incoming data is 0x63 send the data to output 1 and send nothing to output 2
if (Buffer.compare(buf, Buffer.from("63", "hex")) === 0) {
    return [msg, null]
}
// if the incoming data is not 0x63 send nothing to output 1 and the data
else {
    return [null, msg] // I would like to work with ( 256 bytes) send to output 2
}

```

---

<div class="post-metadata">

**Author:** ![DirkB](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dirkb/32/76546_2.png) [@DirkB](https://discourse.nodered.org/u/DirkB)\
**Post date:** [22 February 2023 14:04 UTC](https://discourse.nodered.org/t/struggeling-with-function-to-devide-serial-node-response/75584/4 "2023-02-22T14:04:53Z")

</div>

Thanks a lot. The buffer compare is working well for me.

---

<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:** [8 March 2023 14:05 UTC](https://discourse.nodered.org/t/struggeling-with-function-to-devide-serial-node-response/75584/5 "2023-03-08T14:05:23Z")

</div>

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