Multiple messages in a for cycle

Hi,
I'm trying to read a sequence of global variables and send a message for each of them.
This is my code:

var Reg = "FFFF"; // Address of Register, 16 bit
var RegH = "FF"; //MSB
var RegL = "FF"; //LSB
var Value = "FF"; //Value for Reg
var PL = ["FF","FF"]; //LSB, Value

for (let i = 1; i < 6; i++){
    Reg = global.get('Reg'+i);
    Value = global.get('Val'+i);

    RegH = Reg.substring(0, 2);
    //RegH =parseInt(RegH, 16);

    RegL = Reg.substring(2, 4);
    //RegL =parseInt(RegL, 16);

    //Value =parseInt(Value, 16);

    PL = [RegL,Value];

    msg.pltable= ["MSB: "+RegH,"LSB: "+RegL,"Value: "+Value, i];
    
    msg.command=RegH;
    msg.payload = PL;
    
    return msg;

} 

The global variables are named Reg1...Reg5 and Val1...Val5. so I want to get Reg+i and Val+i globals and for each couple send a message.
The function send only the first couple Reg1, Val1 and stops.
The globals have data and if manual addressing i, the output message is formed with right values.
If "return msg;" is outside the for cycle, the function, obviously, sends the last (5th) couple.
Seems a very symple function but I can't find the mistake.

Maybe the docs can help ? Writing Functions : Node-RED

Docs always helps.
It's a day that I'm looking in docs and in topics but I'm still with one message.

If you want to send a message in a loop, you can use node.send(object)

Which is the line in the docs I pointed at.

2 Likes

Many thanks and sorry if I misunderstood your suggestion.

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