# Reading & writing global variable at the same time

**URL:** <https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070>\
**Category:** General\
**Created:** [23 January 2023 08:49 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070 "2023-01-23T08:49:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [23 January 2023 08:49 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/1 "2023-01-23T08:49:51Z")

</div>

Need help in understanding the best way to use global.set & global.get

I'm reading OMRON plc D tags using [FINS multiple node](https://flows.nodered.org/node/node-red-contrib-omron-fins) using below steps:-

injecting below code using function node

```auto
var tag_1 = 'D1,D2,D3 .............. D50';
var tag_2 = 'D51,D52,D53 .............. D100';
.........................
var tag_n = ............;

var msg1 = { payload : tag_1 };
var msg2 = { payload : tag_2 };
........................
var msgn = { payload : tag_n };

setTimeout(function(){node.send(msg1);},100); 
setTimeout(function(){node.send(msg2);},300);
.........................
setTimeout(function(){node.send(msgn);},n);

```

> \*\*this method I'm using because if I read all tags at once there's always timeout error so reading 50 tags at max at once.

then joining the output using join node and assigning it to global.set("values",msg.payload) then repeating again at (n+200).  
And at the same time reading via global.get("values"). Now the problem I'm facing is that some values are constant but when reading them they are not.  
Like four tags holds time values 1300 and 1330 but gives values

```auto
╔════════╦════════╗
║ time_1 ║ time_2 ║
╠════════╬════════╣
║ 30 ║ ║
╠════════╬════════╣
║ 100 ║ 130 ║
╠════════╬════════╣
║ 1300 ║ 1330 ║
╠════════╬════════╣
║ 1330 ║ ║
╠════════╬════════╣
║ 300 ║ 330 ║
╚════════╩════════╝

```

Also, while reading PLC for some parts of msg I get timeout errors.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f18db9fbf1c26f5cc174f1b57751e62438c35eb3.png)

Is it becasue of this timeout error that causing it?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 January 2023 08:58 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/2 "2023-01-23T08:58:01Z")

</div>

> [@sahsha](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f18db9fbf1c26f5cc174f1b57751e62438c35eb3.png)
> 
> image959×80 6.46 KB

Can you share the flow for this?

There are several things you appear to be doing that are completely unnecessary (like `setTimeout` in a function, using Read Multiple, etc)

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [23 January 2023 09:02 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/3 "2023-01-23T09:02:30Z")

</div>

my flow :-  
[flows.json](https://discourse.nodered.org/uploads/short-url/jaIN5twlqC4N7DjgBpj3XMtpNc8.json) (42.0 KB)

Also, to Parse I do like this:

```auto
msg.TIME_1 =
    Buffer.from(parseInt(msg.payload.D2788).toString(16), 'hex').toString().split("").reverse().join("") +
    Buffer.from(parseInt(msg.payload.D2789).toString(16), 'hex').toString().split("").reverse().join("");

msg.TIME_2 =
    Buffer.from(parseInt(msg.payload.D2792).toString(16), 'hex').toString().split("").reverse().join("") +
    Buffer.from(parseInt(msg.payload.D2793).toString(16), 'hex').toString().split("").reverse().join("");

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [23 January 2023 10:21 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/4 "2023-01-23T10:21:09Z")

</div>

Hi @sahsha !  
After a short check of your flow, I'd initially propose you read the chapter discussing output of [multiple messages](https://nodered.org/docs/user-guide/writing-functions#multiple-messages) in the Node-RED docu. As Steve already indicated: You're doing things that make your code complicated & lengthy - without any need.

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [23 January 2023 12:08 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/5 "2023-01-23T12:08:50Z")

</div>

It might be lengthy but not complicated.

It just delays msg2.payload to _200 ms_ so msg1.payload is executed in FINS to give output

the reason for doing this was not to get this error 👇 while reading all tags at once.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/6224f42ae25172a45ba23f17fca1f78fc33040df.png)  
So I divided the payload into parts with delay then join later.

> [@ralphwetzel](#):
>
> chapter discussing output of [multiple messages](https://nodered.org/docs/user-guide/writing-functions#multiple-messages)

In fact I referred it for creating the code in function node. There is just a small (200 milliseconds) delay with each payload

```auto
var msg1 = { payload:"first out of output 1" };
var msg2 = { payload:"second out of output 1" };
var msg3 = { payload:"third out of output 1" };
var msg4 = { payload:"only message from output 2" };

setTimeout(function () { node.send([msg1,null]); }, 100);
setTimeout(function () { node.send([msg2,null]); }, 300);
setTimeout(function () { node.send([msg3,null]); }, 500);
setTimeout(function () { node.send([null,msg4]); }, 700);

```

If there is a better way to do it then I'm unaware of it due to my limited/low coding skills 😐

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [23 January 2023 12:39 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/6 "2023-01-23T12:39:17Z")

</div>

The result of what you're doing is quite unpredictable ... based on my experience.  
I would create a `msg` package, send this via the `multiple message` functionality (as one batch) & put a `delay` node afterwards.

The `delay` node is able to rate limit the messages ... what seems to be the effect you try to achieve.

As the docu says:

> A function can return multiple messages on an output by returning an array of messages within the returned array.

```auto
let msg = [[ msg1, msg2, msg3, ...] ];
return msg;

```

then _(Attention: This is wrong. Check correct setting below!)_

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b908d2148da959384119768c71922b1c93797a6.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 January 2023 12:57 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/7 "2023-01-23T12:57:35Z")

</div>

Where to start 🤔

Firstly,  
According to [this manual](https://www.edata.omron.com.au/eData/NX/W596-E1-01.pdf) (this is a guess on the manual since you did not mention PLC model), the max request size is 2012 bytes. Or, in FINS 16 bit word terms 1006 WORDS.

My first recommendation would be to put all PLC data (that are related to one another) into memory blocks smaller than the read limit. Then read these addresses in 1 go to ENSURE data consistency.

Additionally, I would separate the reads for live data (production status, production count) config data (break times etc) and config data (cycle time settings etc)

Lastly, I recommend the Buffer Parser over function nodes to make this more graphical (and avoid hacks like this...

Manual conversions, string splits, joins, parsing, buffer creation - not pretty (or necessary)

```auto
msg.TIME_1 =
    Buffer.from(parseInt(msg.payload.D2788).toString(16), 'hex').toString().split("").reverse().join("") +
    Buffer.from(parseInt(msg.payload.D2789).toString(16), 'hex').toString().split("").reverse().join("");

```

  
  

> [@ralphwetzel](#):
>
> ```auto
> let msg = [[msg1, msg2, msg3, ...] ];
> return msg;
> 
> ```
> 
> then
> 
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b908d2148da959384119768c71922b1c93797a6.png)

That wont work - it wil just sent every message at once after 200ms. It should be in "Rate Limit" mode

Also, instead of

```auto
let msg = [[ msg1, msg2, msg3, ..., msg50] ];
return msg;

```

it is nicer to send an array in the payload then use the split node (this gives a graphical element to what is happening)

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [23 January 2023 12:58 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/8 "2023-01-23T12:58:50Z")

</div>

If I have payloads i.e. _msg1.payload.D1_, _msg2.payload.D2_, _msg3.payload.D3_ and after joining I get

`{D1 : "some_value_1", D2 : "some_value_2", D3 : "some_value_3"}`

And if msg1.payload.D1 stays empty or any sort of error occurs then can this happen?

`{D1 : "some_value_2", D2 : " some_va", D3 : "lue_3 "}`

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [23 January 2023 13:03 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/9 "2023-01-23T13:03:44Z")

</div>

> [@Steve-Mcl](#):
>
> this is a guess

It is NX series (NX102-9020 DB)

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [23 January 2023 13:24 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/10 "2023-01-23T13:24:34Z")

</div>

> [@Steve-Mcl](#):
>
> It should be in "Rate Limit" mode

You're fully right. This is the correct setting:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b252bfef728bb3ac2c1d3ce40413a63f4f890732.png)

---

<div class="post-metadata">

**Author:** ![sahsha](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahsha/32/43845_2.png) [@sahsha](https://discourse.nodered.org/u/sahsha)\
**Post date:** [23 February 2023 06:15 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/11 "2023-02-23T06:15:42Z")

</div>

Update\*  
Changed the PLC ladder where tags on which no input was fed were given default value as '####'. This solved the problem for me.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 April 2023 06:16 UTC](https://discourse.nodered.org/t/reading-writing-global-variable-at-the-same-time/74070/12 "2023-04-24T06:16:17Z")

</div>

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