So this is the first analysis of your issues...
So here is an example flow demonstrating how you access bytes of a buffer, check values & reply as required - note in the demo GIF i get different responses in the debug window and the terminal depending on what I write.
In the terminal I enter ok
followed by a new line (CTRL+J) - i get the response from node-red "I got OK!"
Try entering hi
followed by a newline (CTRL+J)
...
the function code "check and reply"
//some really simple buffer access, validation & reply
//first lets see what we are playing with - send some info to the debug window...
try {
node.warn(["msg.payload.constructor.name", msg.payload.constructor.name]);
} catch(e){}
var data = msg.payload; //create an easy to access var named 'data'
//check to see if it is > than 1 in length
if(data && data.length > 1){
//get individual bytes
msg.byte0 = data[0]; //get byte 0
msg.byte1 = data[1]; //get byte 1
//read first 2 bytes as an UINT16
msg.uint16 = data.readUInt16BE(0); //refer to "nodejs buffer" documentation for more functions
//show the data in debug window
node.warn(["byte0=",msg.byte0,"byte1=",msg.byte1]);
//now some simple processing of data so we can reply appropriately
if((msg.byte0 == 79 && msg.byte1 == 75) || (msg.byte0 == 111 && msg.byte1 == 107)) {
msg.info = "Excellent - I got 'OK' in the first 2 bytes of " + msg.payload.length
msg.payload = "I got OK!\r\n";//send a string response
} else if(msg.uint16 == 26729) {
msg.info = "Good stuff - I got UINT 26729 in the first 2 bytes of " + msg.payload.length
msg.payload = "You entered 26729 (or 'hi' which is 26729 as a UINT!)\r\n";//send a string response
} else {
msg.info = "Sending a NAK - You did not sent OK or 26729 in the first 2 bytes of " + msg.payload.length
msg.payload = new Buffer([2,16,3]);//create an NAK (as a buffer response) reply
}
} else {
msg.info = "The buffer is too short - try sending more characters next time!"
msg.payload = new Buffer([2,0xff,3]);//create an ERR (as a buffer response) reply
}
return msg;
demo flow...
[{"id":"71cbb245.9cfe0c","type":"function","z":"8436a7e2.5ac798","name":"REPLACE ME WITH A SERIAL IN NODE","func":"\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":40,"wires":[["97aebc6d.e7ea7","3671908c.d380d"]]},{"id":"97aebc6d.e7ea7","type":"debug","z":"8436a7e2.5ac798","name":"","active":true,"console":"false","complete":"payload","x":630,"y":40,"wires":[]},{"id":"3671908c.d380d","type":"function","z":"8436a7e2.5ac798","name":"check and reply","func":"\n//some really simple buffer access and validation\n\n//first lets see what we are playing with - send some info to the debug window...\ntry {\n node.warn([\"msg.payload.constructor.name\", msg.payload.constructor.name]);\n} catch(e){}\n\n\nvar data = msg.payload; //create an easy to access var named 'data'\n\n//check to see if it is > than 1 in length\nif(data && data.length > 1){\n \n //get individual bytes\n msg.byte0 = data[0]; //get byte 0\n msg.byte1 = data[1]; //get byte 1\n \n //read first 2 bytes as an UINT16\n msg.uint16 = data.readUInt16BE(0); //refer to \"nodejs buffer\" documentation for more functions \n \n //show the data in debug window\n node.warn([\"byte0=\",msg.byte0,\"byte1=\",msg.byte1]);\n\n //now some simple processing of data so we can reply appropriately \n if((msg.byte0 == 79 && msg.byte1 == 75) || (msg.byte0 == 111 && msg.byte1 == 107)) {\n msg.info = \"Excellent - I got 'OK' in the first 2 bytes of \" + msg.payload.length\n msg.payload = \"I got OK!\\r\\n\";//send a string response\n } else if(msg.uint16 == 26729) {\n msg.info = \"Good stuff - I got UINT 26729 in the first 2 bytes of \" + msg.payload.length\n msg.payload = \"You entered 26729 (or 'hi' which is 26729 as a UINT!)\\r\\n\";//send a string response\n } else { \n msg.info = \"Sending a NAK - You did not sent OK or 65535 in the first 2 bytes of \" + msg.payload.length\n msg.payload = new Buffer([2,16,3]);//create an NACK (as a buffer response) reply\n }\n} else {\n msg.info = \"The buffer is too short - try sending more characters next time!\"\n msg.payload = new Buffer([2,0xff,3]);//create an ERR (as a buffer response) reply\n}\n\n\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"x":410.5115432739258,"y":118.90969848632812,"wires":[["9ea50f62.64683","24878233.029c3e"]]},{"id":"9ea50f62.64683","type":"debug","z":"8436a7e2.5ac798","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":608.8859596252441,"y":117.67571258544922,"wires":[]},{"id":"24878233.029c3e","type":"function","z":"8436a7e2.5ac798","name":"REPLACE ME WITH A SERIAL OUT NODE","func":"\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":200,"wires":[[]]},{"id":"d339184f.6c3118","type":"inject","z":"8436a7e2.5ac798","name":"a string of hello","topic":"","payload":"hello","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":340,"y":200,"wires":[["24878233.029c3e"]]},{"id":"e546f912.896818","type":"inject","z":"8436a7e2.5ac798","name":"a buffer of hello","topic":"","payload":"[104,101,108,108,111,13,10]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":340,"y":260,"wires":[["24878233.029c3e"]]}]