How can I get array components values

Hi Guy, I´m a beginner on node-red and I tried get a values from a sample array of number. I tried to use function with a lot of methods inside.
my array have the follow format.

array%20format

I need to separate each component of array to do a different data processing.

somebody can help me?

Best regards,
Alex

  1. List item

Note that this is not an Array it is a Buffer, which is not the same thing. However, if you just want individual bytes as numbers then you can access as if it is an array of 8 bit integers. So you can use, for example
var x = msg.payload[2]
to get element 2. There are a number of questions though. Is that what you are trying to do or do you want the buffer split into a sequence of messages each containing one value or what?

If you want one number per message then first convert it to an explicit array using

msg.payload = [...msg.payload]
return msg

then feed it into a Split node and it will give you a sequence of messages, each with one number.

1 Like

Hi Colin, thanks a lot for your support.
to complement your questions, I would like to handling all those elements received in the buffer. I need to parser each one element to trigger a new actions.
As you advise me , I build an array using msg.payload[n] as you can see bellow:
msg.payload = msg.payload[0],msg.payload[1],msg.payload[2],msg.payload[3]]
return msg

And get sucess, but my buffer according specific msg, have a variable elements, for example:
Cmd1 response:
cmd1_setch

Cmd response 2:

Cmd response 3: and etc....

To improve my parser I need to analyse a number of array received and length for each cmd response for each message and finally analyze each array element.

Do you can help me with examples to implement intelligence in my parser?

I am not certain exactly what you want to do, but I think you will have to use a Function node for that and write some javascript. You can get the length of the array using
msg.payload.length
and you can, for example, loop through the array with something like

var i;
for (i = 0; i < msg.payload.length; i++) {
    // do something with msg.payload[i]
}

Does that help?

Hi Colin, thanks a lot!
I made this code to test and working well:
var i;
for (i = 0; i < msg.payload.length; i++) {
var a;
a= msg.payload[i]
if (a==1){
return msg; //(used to see if works)

}

}
right now I need to send a message, via serial port, according for each parameter received using an inject node.

I have a few injection with all those command, I need to building a flow, for example:
if (a ==2) > Call Inject 1.
How can I call a injection in the code to send send a command inside each one object inject?

If you call node.send(msg) inside a function then it will send that message and then carry on. So you can loop through your array working out what to do for each one and sending the appropriate messages on the way through. Then you feed the output of the function node into whatever nodes you need to do what you want.

Hi Colin, thanks a lot. I had progress in my parser after you help me!
As you see bellow, right now I can handling the parameters responses and send a command using node.send().

//variables
var i;

for (i = 0; i < msg.payload.length; i++) {

// handling parameters and send the response message
switch (msg.payload[i]){
//Analyze GetVersion msg and sending the Cmd_SetCh11
case 149:{
//Send Cmd_SetCh11
msg.payload = new Buffer([0x01,0x02,0x10,0x21,0x02,0x10,0x02,0x14,0x2D,0x02,0x10,0x02,0x10,0x02,0x18,0x02,0x10,0x03]);
node.send(msg);
}
break;

}
break;
}

I can see a slight possible problem with that. The first time it finds msg.payload[i] is 149 then you are overwriting msg.payload with a new buffer, so the next time round the loop it will use the new msg.payload. That will be ok provided you never want to use the original msg.payload again. Even if you don't think you will be ever need the old msg.payload again it might be safer to use

var newMsg = {payload: new Buffer(..)}
node.send(newMsg)

then it will not affect the msg passed in.
Also I am a bit confused by the last break, that will cause it to break out of the for loop after just going through once, with i=0. I suspect that is not what you want.
Finally do you actually need a Buffer rather than an Array? Arrays are generally easier to handle unless you have a specific need for a Buffer.

For the future, if you are posting code if you put a line containing three backtick characters before it and another after it then that will stop the forum reformatting it and messing up the indentation and so on.

Hi Mr Colin,

I using buffer to send cmd, via serial, to my exchange device and array to handling responses received and make a specific decision.
I have two parts in my code. I building a msg flow according msg received, detect a specific byte and than need to send another msg.
Right now is working well. Thanks a lot!!!!

I need to building a html page, with button inside, to catch in specific function to send msgs. Do you have an link to suggest me to start to study it?

I changed my code as you advise me. As you can see bellow:

//variables
var i;

// tratamento das respostas aos comandos do gateway
for (i = 0; i < msg.payload.length; i++) {

// handling parameters and send a response mesage
switch (msg.payload[i]){
//Analyze GetVersion msg and sending the Cmd_SetCh11
case 149:{
//Send Cmd_SetCh11
newMsg = {payload: new Buffer([0x01,0x02,0x10,0x21,0x02,0x10,0x02,0x14,0x2D,0x02,0x10,0x02,0x10,0x02,0x18,0x02,0x10,0x03])}
node.send(newMsg);
}
break;
//Analyze Cmd_SetCh11 msg and sending the Cmd_SetDev-Coord
case 164:{
//Send Cmd_SetDev-Coord
newMsg = {payload: new Buffer([0x01,0x02,0x10,0x23,0x02,0x10,0x02,0x11,0x22,0x02,0x10,0x03])}
node.send(newMsg);
}
break;

please see this post which explains how you
should be posting a flow in the forum

Probably the standard node red dashboard will do what you need.

Hi Mr Colin. thanks a lot for your assistance. You are the best!!!
Mr Ukmose thanks for your advise about how to post a code here!!!

I´ll closing this post, my difficult was overcome.

to contribute to the richness of information, I include below a PIC with information of my code.

Best Regards
Alex