Node-red-contrib-buffer-parser

@Steve-Mcl I just ran accross this node and must say I am impressed. I used it to parse out exactly what I needed. However I receive multiple buffers of varied length with different data so when I use the parser like below:

It works fine until another buffer string is ingested and then it will do exactly what I specified and pull out the data that I don't want. In the above example, I parse out 5 characters and then use a switch node to look for the correct string before I pass it on.

Is there a way to have the buffer parse out up to the offset and then if it does not match criteria ignore the string?

Regards,
Mick

Hi. Thanks for the kind words.

Can you provide a few sample data (capture them using a debug node and put them in inject nodes to create a self contained demo)

Export a minimal flow explaining a bit more what you hope to achieve and I'll take a look.

@Steve-Mcl I am trying to pull this data together. However when I put the string in an inject node the buffer parser does not pull out the data. So I started real easy with a inject string of '1'. Still I can't get the parser to pull out the string. Here is my 3 test flow to extract data from the inject. If you can tell me what I am doing wrong, I can move on to getting you the actual data.

Regards,
Mick

[{"id":"5c17b36f6974f1a0","type":"buffer-parser","z":"b5179ef83dffbee5","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"test","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":680,"y":360,"wires":[["522c0c159489ba71"]]},{"id":"522c0c159489ba71","type":"debug","z":"b5179ef83dffbee5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":360,"wires":[]},{"id":"38ea30d5ef33d17f","type":"inject","z":"b5179ef83dffbee5","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":440,"y":360,"wires":[["5c17b36f6974f1a0"]]}]

use buffer mode

9O17StbuRD

@Steve-Mcl Here are the test nodes...

[{"id":"bf9cba6877cbab26","type":"inject","z":"b5179ef83dffbee5","name":"Unwanted String 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,52,67,68,52,68,52,50,57,124,115,108,105,99,101,32,48,32,105,110,95,117,115,101,61,49,32,115,97,109,112,108,101,95,114,97,116,101,61,50,52,48,48,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,55,52,50,48,48,32,99,108,105,101,110,116,95,104,97,110,100,108,101,61,48,120,48,70,54,68,67,56,48,66,32,105,110,100,101,120,95,108,101,116,116,101,114,61,65,32,114,105,116,95,111,110,61,48,32,114,105,116,95,102,114,101,113,61,48,32,120,105,116,95,111,110,61,48,32,120,105,116,95,102,114,101,113,61,48,32,114,120,97,110,116,61,65,78,84,49,32,109,111,100,101,61,76,83,66,32,119,105,100,101,61,48,32,102,105,108,116,101,114,95,108,111,61,45,51,48,48,48,32,102,105,108,116,101,114,95,104,105,61,45,49,48,48,32,115,116,101,112,61,49,48,32,115,116,101,112,95,108,105,115,116,61,49,44,49,48,44,53,48,44,49,48,48,44,53,48,48,44,49,48,48,48,44,50,48,48,48,44,51,48,48,48,32,97,103,99,95,109,111,100,101,61,109,101,100,32,97,103,99,95,116,104,114,101,115,104,111,108,100,61,53,53,32,97,103,99,95,111,102,102,95,108,101,118,101,108,61,49,48,32,112,97,110,61,48,120,52,48,48,48,48,48,48,48,32,116,120,97,110,116,61,65,78,84,49,32,108,111,111,112,97,61,48,32,108,111,111,112,98,61,48,32,113,115,107,61,48,32,100,97,120,61,49,32,100,97,120,95,99,108,105,101,110,116,115,61,48,32,108,111,99,107,61,48,32,116,120,61,49,32,97,99,116,105,118,101,61,49,32,97,117,100,105,111,95,108,101,118,101,108,61,53,48,32,97,117,100,105,111,95,112,97,110,61,53,48,32,97,117,100,105,111,95,109,117,116,101,61,48,32,114,101,99,111,114,100,61,48,32,112,108,97,121,61,100,105,115,97,98,108,101,100,32,114,101,99,111,114,100,95,116,105,109,101,61,48,46,48,32,97,110,102,61,48,32,97,110,102,95,108,101,118,101,108,61,48,32,110,114,61,48,32,110,114,95,108,101,118,101,108]","payloadType":"bin","x":290,"y":140,"wires":[["40027a9d4ad0a20b"]]},{"id":"208c6a86ed951763","type":"inject","z":"b5179ef83dffbee5","name":"Unwanted String 2","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,70,54,68,67,56,48,66,124,115,108,105,99,101,32,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,50,50,52,49,48,32,119,105,100,101,61,48,32,108,111,99,107,61,48,10,83,70,54,68,67,56,48,66,124,115,108,105,99,101,32,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,50,50,52,49,48,32,119,105,100,101,61,48,32,108,111,99,107,61,48,10]","payloadType":"bin","x":290,"y":180,"wires":[["e38576c2450a139a"]]},{"id":"63e33cdac0f33d6e","type":"inject","z":"b5179ef83dffbee5","name":"Parse Dax value 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,51,57,66,69,65,55,66,124,115,108,105,99,101,32,48,32,100,97,120,61,49,32,100,97,120,95,99,108,105,101,110,116,115,61,48,32,10]","payloadType":"bin","x":290,"y":40,"wires":[["595eeaf931ca7b4b"]]},{"id":"ecc8d2c61ce2b110","type":"inject","z":"b5179ef83dffbee5","name":"Parse Dax Value 2","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,51,57,66,69,65,55,66,124,115,108,105,99,101,32,48,32,100,97,120,61,50,32,100,97,120,95,99,108,105,101,110,116,115,61,48]","payloadType":"bin","x":290,"y":80,"wires":[["8d923420727eb39a"]]},{"id":"5475269996fa50bb","type":"inject","z":"b5179ef83dffbee5","name":"Unwanted String 3","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,70,54,68,67,56,48,66,124,115,108,105,99,101,32,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,53,57,52,51,48,32,119,105,100,101,61,48,32,108,111,99,107,61,48,10]","payloadType":"bin","x":290,"y":220,"wires":[["8921f4ba45e7bd56"]]},{"id":"595eeaf931ca7b4b","type":"buffer-parser","z":"b5179ef83dffbee5","name":"DaxAudioChannel","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"dax","offset":21,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":false,"outputs":1,"x":690,"y":40,"wires":[["034c70f756f0baaa"]]},{"id":"034c70f756f0baaa","type":"debug","z":"b5179ef83dffbee5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":40,"wires":[]},{"id":"8d923420727eb39a","type":"buffer-parser","z":"b5179ef83dffbee5","name":"DaxAudioChannel2","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"dax","offset":21,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":690,"y":80,"wires":[["2048384ea37d701a"]]},{"id":"2048384ea37d701a","type":"debug","z":"b5179ef83dffbee5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":80,"wires":[]},{"id":"40027a9d4ad0a20b","type":"buffer-parser","z":"b5179ef83dffbee5","name":"DaxAudioChannel2","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"dax","offset":21,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":690,"y":140,"wires":[["1de6e8f751ac89b0"]]},{"id":"e38576c2450a139a","type":"buffer-parser","z":"b5179ef83dffbee5","name":"DaxAudioChannel2","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"dax","offset":21,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":690,"y":180,"wires":[["6dc46ae131088741"]]},{"id":"8921f4ba45e7bd56","type":"buffer-parser","z":"b5179ef83dffbee5","name":"DaxAudioChannel2","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"dax","offset":21,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":690,"y":220,"wires":[["3acd04d75b0e4d88"]]},{"id":"1de6e8f751ac89b0","type":"debug","z":"b5179ef83dffbee5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":140,"wires":[]},{"id":"3acd04d75b0e4d88","type":"debug","z":"b5179ef83dffbee5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":220,"wires":[]},{"id":"6dc46ae131088741","type":"debug","z":"b5179ef83dffbee5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":180,"wires":[]}]

I receive all the data on a TCP in node so there are many types of array data. The idea is to some how examine the prefix before the offset and determine if the data is valid.

Once again thank you

TBH, this data looks more like strings of key/values more suited to regex.

Try this - see if it is any help...

[{"id":"bf9cba6877cbab26","type":"inject","z":"f542fe9c.732af","name":"Unwanted String 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,52,67,68,52,68,52,50,57,124,115,108,105,99,101,32,48,32,105,110,95,117,115,101,61,49,32,115,97,109,112,108,101,95,114,97,116,101,61,50,52,48,48,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,55,52,50,48,48,32,99,108,105,101,110,116,95,104,97,110,100,108,101,61,48,120,48,70,54,68,67,56,48,66,32,105,110,100,101,120,95,108,101,116,116,101,114,61,65,32,114,105,116,95,111,110,61,48,32,114,105,116,95,102,114,101,113,61,48,32,120,105,116,95,111,110,61,48,32,120,105,116,95,102,114,101,113,61,48,32,114,120,97,110,116,61,65,78,84,49,32,109,111,100,101,61,76,83,66,32,119,105,100,101,61,48,32,102,105,108,116,101,114,95,108,111,61,45,51,48,48,48,32,102,105,108,116,101,114,95,104,105,61,45,49,48,48,32,115,116,101,112,61,49,48,32,115,116,101,112,95,108,105,115,116,61,49,44,49,48,44,53,48,44,49,48,48,44,53,48,48,44,49,48,48,48,44,50,48,48,48,44,51,48,48,48,32,97,103,99,95,109,111,100,101,61,109,101,100,32,97,103,99,95,116,104,114,101,115,104,111,108,100,61,53,53,32,97,103,99,95,111,102,102,95,108,101,118,101,108,61,49,48,32,112,97,110,61,48,120,52,48,48,48,48,48,48,48,32,116,120,97,110,116,61,65,78,84,49,32,108,111,111,112,97,61,48,32,108,111,111,112,98,61,48,32,113,115,107,61,48,32,100,97,120,61,49,32,100,97,120,95,99,108,105,101,110,116,115,61,48,32,108,111,99,107,61,48,32,116,120,61,49,32,97,99,116,105,118,101,61,49,32,97,117,100,105,111,95,108,101,118,101,108,61,53,48,32,97,117,100,105,111,95,112,97,110,61,53,48,32,97,117,100,105,111,95,109,117,116,101,61,48,32,114,101,99,111,114,100,61,48,32,112,108,97,121,61,100,105,115,97,98,108,101,100,32,114,101,99,111,114,100,95,116,105,109,101,61,48,46,48,32,97,110,102,61,48,32,97,110,102,95,108,101,118,101,108,61,48,32,110,114,61,48,32,110,114,95,108,101,118,101,108]","payloadType":"bin","x":870,"y":180,"wires":[["a8c03bc39ebcf732"]]},{"id":"208c6a86ed951763","type":"inject","z":"f542fe9c.732af","name":"Unwanted String 2","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,70,54,68,67,56,48,66,124,115,108,105,99,101,32,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,50,50,52,49,48,32,119,105,100,101,61,48,32,108,111,99,107,61,48,10,83,70,54,68,67,56,48,66,124,115,108,105,99,101,32,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,50,50,52,49,48,32,119,105,100,101,61,48,32,108,111,99,107,61,48,10]","payloadType":"bin","x":870,"y":220,"wires":[["a8c03bc39ebcf732"]]},{"id":"63e33cdac0f33d6e","type":"inject","z":"f542fe9c.732af","name":"Parse Dax value 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,51,57,66,69,65,55,66,124,115,108,105,99,101,32,48,32,100,97,120,61,49,32,100,97,120,95,99,108,105,101,110,116,115,61,48,32,10]","payloadType":"bin","x":870,"y":80,"wires":[["a8c03bc39ebcf732"]]},{"id":"ecc8d2c61ce2b110","type":"inject","z":"f542fe9c.732af","name":"Parse Dax Value 2","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,51,57,66,69,65,55,66,124,115,108,105,99,101,32,48,32,100,97,120,61,50,32,100,97,120,95,99,108,105,101,110,116,115,61,48]","payloadType":"bin","x":870,"y":120,"wires":[["a8c03bc39ebcf732"]]},{"id":"5475269996fa50bb","type":"inject","z":"f542fe9c.732af","name":"Unwanted String 3","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[83,70,54,68,67,56,48,66,124,115,108,105,99,101,32,48,32,82,70,95,102,114,101,113,117,101,110,99,121,61,55,46,49,53,57,52,51,48,32,119,105,100,101,61,48,32,108,111,99,107,61,48,10]","payloadType":"bin","x":870,"y":260,"wires":[["a8c03bc39ebcf732"]]},{"id":"a8c03bc39ebcf732","type":"buffer-parser","z":"f542fe9c.732af","name":"DaxAudioChannel","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"dax","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":false,"outputs":1,"x":1150,"y":180,"wires":[["15e1beda944c2914"]]},{"id":"15e1beda944c2914","type":"function","z":"f542fe9c.732af","name":"","func":"const str = msg.payload;\nconst regex1 = /(\\w+)\\|slice (\\d+)(.*)/g\nlet part2, m;\n// m = regex1.match(str)\nm = regex1.exec(str)\nconst topic = m[1]\nconst result = {\n    \"slice\": Number(m[2])\n}\nconst keyvalues = m[3];\n\nconst regex2 = /(?<key>[^=]+)=(?<value>[^ ]+)/g;\n\n\nwhile ((m = regex2.exec(keyvalues)) !== null) {\n    // This is necessary to avoid infinite loops with zero-width matches\n    if (m.index === regex2.lastIndex) {\n        regex2.lastIndex++;\n    }\n    if(m && m.length == 3) {\n        let key = m[1].trim();\n        let val = m[2].trim();\n        result[key] = val;\n    }\n}\n\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":300,"wires":[["63f0982dce5145f0"]]},{"id":"63f0982dce5145f0","type":"debug","z":"f542fe9c.732af","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1320,"y":300,"wires":[]}]

@Steve-Mcl Your function works great. I ran it with the node bypassed and it works fine. So now rethinking if I need to use the node at all. I do have a question regarding your function where you declared part2

let part2, m;

what does part2 do for the function?

thx

It was a left over from a trial. I didn't tidy up afterwards :slight_smile:

1 Like

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