Revert byte stuffing

Hello community,
I am struggling with extracting and replacing specific bytes from a buffer.
Sensor is sending out data in buffers with between 47 and 50 bytes, see examples.

Buffer w/th 47 bytes:

{"payload":[126,0,3,0,40,64,190,87,236,64,201,71,245,64,201,71,254,64,201,72,6,66,36,8,221,66,61,28,45,66,61,227,64,66,61,247,90,66,62,3,157,62,224,139,128,219,126],"port":"/dev/ttyUSB0","_msgid":"b0f0c54c.394638"}

Buffer w/th 48 bytes:

{"payload":[126,0,3,0,40,64,188,87,127,64,199,42,22,64,199,42,30,64,199,42,39,66,34,79,67,66,59,31,18,66,59,228,13,66,59,247,241,66,60,4,125,51,62,224,178,168,144,126],"port":"/dev/ttyUSB0","_msgid":"1c539c3a.c734e4"}

Buffer w/th 49 bytes:

{"payload":[126,0,3,0,40,64,199,93,209,64,210,210,150,64,210,210,159,64,210,210,167,66,43,207,125,93,66,70,125,51,27,66,70,227,158,66,70,248,171,66,71,5,131,62,228,6,64,251,126],"port":"/dev/ttyUSB0","_msgid":"e5e1fca9.4abb"}

Buffer w/th 50 bytes:

{"payload":[126,0,3,0,40,64,201,228,249,64,213,125,94,238,64,213,125,94,247,64,213,127,0,66,45,253,51,66,72,150,18,66,73,105,57,66,73,125,94,139,66,73,139,141,62,227,150,196,159,126],"port":"/dev/ttyUSB0","_msgid":"27fe6e5d.0c9952"}

Converting the hex-data to float of course delivers erratic results, if the buffer size is varying.
Sensor documentation asks to replace bytes as follows:

Revert byte stuffing:
Replace 0x7D, 0x5E with 0x7E
Replace 0x7D, 0x5D with 0x7D
Replace 0x7D, 0x31 with 0x13
Replace 0x7D, 0x33 with 0x11

End result would then be a buffer with 47 bytes.

Is there any node that can handle the job without explicit coding?
I tried -amongst others- “Split” – “Change” – “Join”, but did not succeed.

[
    {
        "id": "9ed4ad94.2501a",
        "type": "tab",
        "label": "Flow 4",
        "disabled": false,
        "info": ""
    },
    {
        "id": "89eef8f.43c2308",
        "type": "inject",
        "z": "9ed4ad94.2501a",
        "name": "BUFFER 50",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[126,0,3,0,40,64,189,50,101,64,200,125,49,145,64,200,125,49,153,64,200,125,49,161,66,35,11,232,66,59,248,142,66,60,190,110,66,60,210,104,66,60,222,152,62,241,255,36,39,126]",
        "payloadType": "bin",
        "x": 270,
        "y": 280,
        "wires": [
            [
                "826a73f5.562218",
                "7bb39c4d.5aac34"
            ]
        ]
    },
    {
        "id": "8afdf3ef.9fa7f8",
        "type": "inject",
        "z": "9ed4ad94.2501a",
        "name": "BUFFER 47",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[126,0,3,0,40,64,192,214,87,64,203,235,16,64,203,235,24,64,203,235,33,66,38,47,10,66,63,150,118,66,64,96,36,66,64,116,129,66,64,128,237,62,237,195,88,156,126]",
        "payloadType": "bin",
        "x": 270,
        "y": 160,
        "wires": [
            [
                "e5332a55.970f78",
                "7bb39c4d.5aac34"
            ]
        ]
    },
    {
        "id": "9ac2b2d0.f4237",
        "type": "inject",
        "z": "9ed4ad94.2501a",
        "name": "BUFFER 48",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[126,0,3,0,40,64,196,119,231,64,207,194,10,64,207,194,125,51,64,207,194,28,66,41,80,31,66,67,50,0,66,67,255,123,66,68,20,58,66,68,32,226,62,238,31,132,11,126]",
        "payloadType": "bin",
        "x": 270,
        "y": 200,
        "wires": [
            [
                "2e6fa7a2.cb79",
                "7bb39c4d.5aac34"
            ]
        ]
    },
    {
        "id": "19e82eab.0f1b49",
        "type": "inject",
        "z": "9ed4ad94.2501a",
        "name": "BUFFER 49",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[126,0,3,0,40,64,210,178,149,64,222,206,10,64,222,206,125,51,64,222,206,28,66,53,147,81,66,81,85,125,51,66,82,49,112,66,82,71,175,66,82,85,66,62,234,135,96,47,126]",
        "payloadType": "bin",
        "x": 270,
        "y": 240,
        "wires": [
            [
                "7f5a6371.3fe6dc",
                "7bb39c4d.5aac34"
            ]
        ]
    },
    {
        "id": "e5332a55.970f78",
        "type": "debug",
        "z": "9ed4ad94.2501a",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 160,
        "wires": []
    },
    {
        "id": "2e6fa7a2.cb79",
        "type": "debug",
        "z": "9ed4ad94.2501a",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 200,
        "wires": []
    },
    {
        "id": "7f5a6371.3fe6dc",
        "type": "debug",
        "z": "9ed4ad94.2501a",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 240,
        "wires": []
    },
    {
        "id": "826a73f5.562218",
        "type": "debug",
        "z": "9ed4ad94.2501a",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 280,
        "wires": []
    },
    {
        "id": "7bb39c4d.5aac34",
        "type": "split",
        "z": "9ed4ad94.2501a",
        "name": "Split array",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": "1",
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 540,
        "y": 480,
        "wires": [
            [
                "f12443d.a0d8fc"
            ]
        ]
    },
    {
        "id": "972a76fd.336e08",
        "type": "join",
        "z": "9ed4ad94.2501a",
        "name": "",
        "mode": "auto",
        "build": "string",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": "false",
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "x": 950,
        "y": 480,
        "wires": [
            [
                "ce248cd5.f265a"
            ]
        ]
    },
    {
        "id": "ce248cd5.f265a",
        "type": "debug",
        "z": "9ed4ad94.2501a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 1110,
        "y": 480,
        "wires": []
    },
    {
        "id": "ec6fe9e5.fcc02",
        "type": "change",
        "z": "9ed4ad94.2501a",
        "name": "Change - Test #2",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D 5E",
                "fromt": "str",
                "to": "7E",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D 31",
                "fromt": "str",
                "to": "11",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D 33",
                "fromt": "str",
                "to": "13",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D 5D",
                "fromt": "str",
                "to": "7D",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "badf72d5.5f7258",
        "type": "change",
        "z": "9ed4ad94.2501a",
        "name": "Change - Test #1",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "\"7D5E\"",
                "fromt": "str",
                "to": "\"7E\"",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "\"7D31\"",
                "fromt": "str",
                "to": "\"11\"",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "\"7D33\"",
                "fromt": "str",
                "to": "\"13\"",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "\"7D5D\"",
                "fromt": "str",
                "to": "\"7D\"",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "824b2b33.c3ddc",
        "type": "change",
        "z": "9ed4ad94.2501a",
        "name": "Change - Test #3",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D5E",
                "fromt": "str",
                "to": "7E",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D31",
                "fromt": "str",
                "to": "11",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D33",
                "fromt": "str",
                "to": "13",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "7D5D",
                "fromt": "str",
                "to": "7D",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 400,
        "wires": [
            []
        ]
    },
    {
        "id": "f12443d.a0d8fc",
        "type": "change",
        "z": "9ed4ad94.2501a",
        "name": "Change - Test #5",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125 94",
                "fromt": "str",
                "to": "126",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125 49",
                "fromt": "str",
                "to": "17",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125 51",
                "fromt": "str",
                "to": "19",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125 93",
                "fromt": "str",
                "to": "125",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 480,
        "wires": [
            [
                "972a76fd.336e08"
            ]
        ]
    },
    {
        "id": "c7674b08.fb06f",
        "type": "change",
        "z": "9ed4ad94.2501a",
        "name": "Change - Test #4",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125,94",
                "fromt": "str",
                "to": "126",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125,49",
                "fromt": "str",
                "to": "17",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125,51",
                "fromt": "str",
                "to": "19",
                "tot": "str"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "125,93",
                "fromt": "str",
                "to": "125",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 439,
        "wires": [
            []
        ]
    }
]

Just a formatting issue?

Thanks for your support!

for the byte stuffing, you might just have to scan the buffer and write to a 2nd buffer...

var src = msg.payload;
var dst = Buffer.alloc(src.length*2);
var pos = 0;
for(let i = 0 ; i < src.length ; i++) {
  var b, bNext 
  b = src[i];
  if(i < src.length-1) bNext = src[i+1];
  if(b == 0x7D) {
    if(bNext == 0x5E) {
      b = 0x7E;
      i++;
    } else if(bNext == 0x5D) {
      b = 0x7D;
      i++;
    } else if(bNext == 0x5D) {
      b = 0x7D;
      i++;
    } else if(bNext == 0x31) {
      b = 0x13;
      i++;
    } else if(bNext == 0x33) {
      b = 0x11;
      i++;
    }
  }
  dst.writeUInt8(b,pos);
  pos++;
}
msg.payload = dst.slice(0,pos);
return msg;

NOTE ^ there is no error checking


For extracting floats etc, maybe node-red-contrib-buffer-parser will be of some use (it does pretty much everything for you)

Thanks Steve, the issue is solved with your function code!

I will test node-red-contrib-buffer-parser , as recommended!

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