How to access the individual serial port data buffer bytes and send a reply?

Hi, I am getting the hex commands from the device through serial in node as can be seen in the picture attached. The buffer contains the five bytes.


I want to do the following things and looking forward to your suggestions.
1: I need to access the individual bytes of the buffer.
2: I need to compare the individual bytes to other set bytes and then accordingly write the buffer to the serial out node.
Right now if I get this buffer through serial in data from the device, I write the replying buffer manually to the serial out node using function node and timestamp inject node. As I am new to node-red, Is there any way to automate this whole thing?
Note: I am using an older version of node-red (0.15.2) with node.js version of 0.10.40.
Thanks in advance

I guess this is related to your original thread?

That info is in the buffer docs Nick pointed you to in your other thread.

Yes, of course, but you will need to spell out the steps. And you will need to provide sample data and demo flows.

1 Like

Thank you for your reply. I have read the buffer docs, the only problem which I face is that I could not read the buffer out of msg.payload. The complete steps are as follows:
Step 1: To access the individual byte of the received data and compare it with other set bytes to evaluate whether I have gotten the desired data or not (I am receiving the data buffer through serial in port as can be see in the picture but I am facing a problem to access the individual bytes for comparison at this step).
Step 2: Based on step 2, sending the replying buffer to the device through the serial out node (I was able to write the buffer to the device, but this step is not automated yet and not linked with step 2, I want these two steps linked and automated). Please have a look at sample flow and data

Could you please look at the my flow and data? Looking forward to your help.

A picture of a flow is not enough I'm afraid. You will have to export your flow and paste into a reply between three backticks for correct formatting.

Ps. Don't forget to add sample data

1 Like

@Waled22 if msg.payload is a Buffer object, then you can access its contents in the same way you would an array - msg.payload[0], msg.payload[1] etc.

1 Like

Please have a look

[{"id":"a44829a1.6c2a38","type":"tab","label":"Ser"},{"id":"b02ff5ca.86f358","type":"debug","z":"a44829a1.6c2a38","name":"","active":true,"console":"false","complete":"payload","x":634.2497482299805,"y":58.74996471405029,"wires":[]},{"id":"2c1cd057.13e1b","type":"serial in","z":"a44829a1.6c2a38","name":"Serial in Node","serial":"fb6eb2f.6434e5","x":205.9999008178711,"y":58.999961853027344,"wires":[["b02ff5ca.86f358"]]},{"id":"ea8f2bc6.d01328","type":"serial out","z":"a44829a1.6c2a38","name":"Serial Out Node ","serial":"fb6eb2f.6434e5","x":625.9993515014648,"y":190.9999237060547,"wires":[]},{"id":"32211b27.506524","type":"function","z":"a44829a1.6c2a38","name":"WritingBuffer","func":"msg.payload = new Buffer([180, 0, 1, 1, 192, 20]);\nreturn msg;","outputs":1,"noerr":0,"x":413.8985176086426,"y":191.93743133544922,"wires":[["ea8f2bc6.d01328"]]},{"id":"740532ce.2e65ec","type":"debug","z":"a44829a1.6c2a38","name":"","active":true,"console":"false","complete":"payload","x":628.8859596252441,"y":117.67571258544922,"wires":[]},{"id":"b4d35a77.5a3658","type":"inject","z":"a44829a1.6c2a38","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":201.88284301757812,"y":191.43744468688965,"wires":[["32211b27.506524"]]},{"id":"ccfc73b.878cd9","type":"inject","z":"a44829a1.6c2a38","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":203.2476806640625,"y":120.27996444702148,"wires":[["e8be9d36.1fcda"]]},{"id":"e8be9d36.1fcda","type":"function","z":"a44829a1.6c2a38","name":"BufferElement","func":"var x = msg.payload[0];\nmsg.payload = x; \nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"x":410.5115432739258,"y":118.90969848632812,"wires":[["740532ce.2e65ec"]]},{"id":"fb6eb2f.6434e5","type":"serial-port","z":"","serialport":"/dev/ttyS2","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"bin","out":"char","addchar":false}]

Thank you @knolleary for the reply. I tried this before but it is not working.

I've been trying yo assist someone on another thread where debug shows "buffer" in payload but they cant access [elements]

Long story short - got the OP to put node.warns in code and it turned out to be an ArrayBuffer.

1 Like

@Waled22 I imported your flow & this is the first thing I noticed...

Is this how it looks on your linux box? (i'm using windows at the moment)

I'll get back to you shortly on your issue - but first please send a screen shot of your serial config

1 Like

@Steve-Mcl, thank you for the reply. Please check this out

I think that's the difference in versions. Those inputs got added to the serial node in later versions.

2 Likes

Seems to be an import issue with the serial node :frowning:

OK, I will set up as you have.

PS what version of NODE, NODE-RED and Serial port do you have...

image

image

image

NOTE: I will reply soon with a solution to your original question - but please provide this info first

1 Like

part one of the answer :slight_smile:

1 Like

ah haha - i did see that initially but forgot (or maybe hoped OP had updated). Thanks.

1 Like

@Steve-Mcl I know you would be amazed knowing that I am using quite old versions.
Node-Red Version
image

Here you can see that I cannot find the 'Manage Palette' option in which I could have seen the version of Serial port.

As I thought, the serial node came builtin at the time: https://www.npmjs.com/package/node-red/v/0.15.2
Which brings you to...
https://github.com/node-red/node-red/blob/9516da01e38e3d5a6bdf17d48c2ace5d6608f205/package.json#L65
@Steve-Mcl there you go, serial port 0.4.x

2 Likes

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"]]}]
1 Like

@Steve-Mcl Thank you so much for identifying my issue. I really appreciate your effort and the time which you have invested. May God bless you.

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