Something like this should do it, in a Function node
let state = context.get("state") || "wait_171"
let buff = context.get("buff") || []
let count = context.get("count") || 0
const maxCount = 8 // number of additional chars to get
switch(state) {
case "wait_171":
if (msg.payload[0] == 171) {
buff = [171]
state = "wait_205"
}
// otherwise ignore this character
// send nothing back
msg = null
break;
case "wait_205":
if (msg.payload[0] == 205) {
buff.push(205)
state = "buffering"
count = 0
} else {
// only 205 is valid here, so start again
state = "wait_171"
}
msg = null
break;
default: // buffering
buff.push(msg.payload[0])
count++
if (count >= maxCount) {
// have now received a full buffer
msg.payload = buff
state = "wait_171"
count = 0
} else {
msg = null
}
}
context.set("state", state)
context.set("buff", buff)
context.set("count", count)
node.status({ text: `state: ${state}, count: ${count}` });
return msg;
Test flow:
[{"id":"6586e0f3363d2f8f","type":"inject","z":"e672d6322ed2b137","name":"171","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[171]","payloadType":"bin","x":130,"y":220,"wires":[["31c19a45d4d1ba99"]]},{"id":"5eef4a3f33425859","type":"debug","z":"e672d6322ed2b137","name":"debug 47","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":260,"wires":[]},{"id":"6915fc970d0ba5e0","type":"inject","z":"e672d6322ed2b137","name":"205","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[205]","payloadType":"bin","x":130,"y":260,"wires":[["31c19a45d4d1ba99"]]},{"id":"2c2d25c611fb7c8f","type":"inject","z":"e672d6322ed2b137","name":"1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1]","payloadType":"bin","x":130,"y":340,"wires":[["31c19a45d4d1ba99"]]},{"id":"074de7f8c91057b5","type":"inject","z":"e672d6322ed2b137","name":"22","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[22]","payloadType":"bin","x":130,"y":380,"wires":[["31c19a45d4d1ba99"]]},{"id":"31c19a45d4d1ba99","type":"function","z":"e672d6322ed2b137","name":"Build message","func":"let state = context.get(\"state\") || \"wait_171\"\nlet buff = context.get(\"buff\") || []\nlet count = context.get(\"count\") || 0\nconst maxCount = 8 // number of additional chars to get\nswitch(state) {\n case \"wait_171\":\n if (msg.payload[0] == 171) {\n buff = [171]\n state = \"wait_205\"\n }\n // otherwise ignore this character\n // send nothing back \n msg = null\n break;\n\n case \"wait_205\":\n if (msg.payload[0] == 205) {\n buff.push(205)\n state = \"buffering\"\n count = 0\n } else {\n // only 205 is valid here, so start again\n state = \"wait_171\"\n }\n msg = null\n break;\n \n default: // buffering\n buff.push(msg.payload[0])\n count++\n if (count >= maxCount) {\n // have now received a full buffer\n msg.payload = buff\n state = \"wait_171\"\n count = 0\n } else {\n msg = null\n }\n}\ncontext.set(\"state\", state)\ncontext.set(\"buff\", buff)\ncontext.set(\"count\", count)\nnode.status({ text: `state: ${state}, count: ${count}` });\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":260,"wires":[["5eef4a3f33425859"]]}]