Struggeling with function to devide serial node response

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

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

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.

There is also a Buffer.compare method that you may use (source)

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
}

1 Like

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

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