OPC Communication

Hey,

I am new to Node-Red and trying to learn.
What i am trying to do is read 6 signals from the OPC. I send then with a Join block and read them out in array.

Code looks like this after the Join block:

let msg1 = {};
let msg2 = {};
let msg3 = {};
let msg4 = {};
let msg5 = {};

if (msg.payload[5].payload === true && msg.payload[0].payload === false) {

  • msg1.payload = true;*
    } else {
  • msg1.payload = false;*
    }

if (msg.payload[5].payload === true && msg.payload[1].payload === false) {

  • msg2.payload = true;*
    } else {
  • msg2.payload = false;*
    }

if (msg.payload[5].payload === true && msg.payload[2].payload === false) {

  • msg3.payload = true;*
    } else {
  • msg3.payload = false;*
    }

if (msg.payload[5].payload === true && msg.payload[3].payload === false) {

  • msg4.payload = true;*
    } else {
  • msg4.payload = false;*
    }

if (msg.payload[5].payload === true && msg.payload[4].payload === false) {

  • msg5.payload = true;*
    } else {
  • msg5.payload = false;*
    }

return [msg1, msg2, msg3, msg4, msg5];

This all works fine, what bothers me is sometimes it somehow jumps thrue every IF statement and makes a false +1 to the counter even tho the IF statement is not true.
It seems to happen when one off the IF statements are true it jumps thrue all of them untill it comes to the correct one.

I am sorry if the explanation was bad , let me know and i will try my best to explain what my problem is. :slight_smile:

Thank you!

This code is a pain to look at. Can you put it in normal code block and format it correctly? And can you capture an incoming message which makes the code not work fine?

Hey,
Sorry i will try to provide better information.

This is the blocks how the blocks are put togheter.

Here a TRUE signal gets send on [2] msg from the array in one cycle then goes back to false even tho the OPC did not send the signals so the IF statment in function 6 should not have been true. ( 5 messages every secound thats why i split it with the red line to seperate the cycle time )

Function 5 example :

var msg1 = {};
var msg2 = {};
var msg3 = {};
var msg4 = {};
var msg5 = {};
var msg6 = {};

msg1.topic = "ns=2;s=A";
msg2.topic = "ns=2;s=B";
msg3.topic = "ns=2;s=C";
msg4.topic = "ns=2;s=D";
msg5.topic = "ns=2;s=E";
msg6.topic = "ns=2;s=F";

return [msg1, msg2, msg3, msg4, msg5, msg6];

Function 6 example :

let msg1 = {};
let msg2 = {};
let msg3 = {};
let msg4 = {};
let msg5 = {};


if (msg.payload[5].payload === true && msg.payload[0].payload === false) {
    msg1.payload = true;
}  else {
    msg1.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[1].payload === false) {
    msg2.payload = true;
} else {
    msg2.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[2].payload === false) {
    msg3.payload = true;
} else {
    msg3.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[3].payload === false) {
    msg4.payload = true;
} else {
    msg4.payload = false;
}

if (msg.payload[5].payload === true && msg.payload[4].payload === false) {
    msg5.payload = true;
} else {
    msg5.payload = false;
}


return [msg1, msg2, msg3, msg4, msg5];

I am not sure what you ment by normal code block and to format it correctly sorry , its my first time with node-red and json coding. Only been coding PLCs before this. Hopefully its easier to read now ?

Thank you for your time!

I just now saw when debugging the JOIN node that the objects gets swaped around. This is most likely to cause the problem. But i don't know how to fix it.

Example :

So on one cycle 5th object is looking at the right signal and secound cycle it swaps and looks at another signal then back again for next cycle.

This i can happen due to the async nature of network communications. In short, do not use array mode on the join node when async responses are possible since results can arrive out of order. In these cases, use key/Val object mode.

Better still, if you use readmultiple mode on the opcua node, you can avoid using the join node altogether. That has the benefit of providing all results in one message and more importantly, consistent data.

1 Like

Hey Steve, Thanks for the reply.

If i set the OPC Client to Read Multiple the node says " nodeid stored" and does not send anything to the debugg node.

In read multiple node, you send the topics once , then for polling, you only need to send a topic of "readmultiple" and all results will arrive once in the bottom output payload.

I'm pretty sure there is an example flow built in - examples are available when you hit CTRL + I

1 Like

I will try to figure it out :smiley: . I will let you know if i succeed or not. Thanks alot for the help!

Maybe too late now, but the code you shared originally could be replaced with something like this:

const messages = [];
for(let i = 0; i < 5; i++){
  messages.push({
    payload: msg.payload[5].payload === true && msg.payload[i].payload === false
  });
}

return [messages];
2 Likes

Compare the code you posted in OP and later. It's hard to read in your first post. You made it very easy to read in the 2nd post, thanks!

1 Like

Yeah i looked at other posts to see what the correct way was :smiley: Thank you all for the amazing help. I am still trying to figure out OPC node with multiple read.

1 Like

I am still trying my best to figure this out with the examples,

The problem i face now is that the message is not in an array in objects as it was before with the objects.

The "array of nodeids" function looks like this :

msg.topic = "readmultiple"
msg.payload = ["ns=2;s=A",
"ns=2;s=B",
"ns=2;s=C",
"ns=2;s=D",
"ns=2;s=E",
"ns=2;s=F"
];
return msg;

Did you read Steve's post carefully? He said:
In read multiple node, you send the topics once , then for polling, you only need to send a topic of "readmultiple" and all results will arrive once in the bottom output payload.

1 Like

Also...

And in case you cannot find that - its here: node-red-contrib-opcua/examples/OPCUA_READMULTIPLE.json at master · mikakaraila/node-red-contrib-opcua · GitHub

1 Like

Thank you I will look into that.
I did read it altho i did not really understand it and felt stupid to ask what you ment by polling.
This is my bad sorry.

I will look at the example you sent the one i found was abit different as i did not find it with ctrl + I.

Thank you so much for your time :slight_smile:

The resault are the same as the other example i did. Problem is i need to use the payloads in a funktion where if one is true and one is false send true that then goes to a counter.
But i don't know how to use it in a funkction when the outputs i get are like this :

as all i see is "msg payload : boolean - false " instad of object[1] topic "variable" paload : false "

I might be explaning it bad, but basicly what I am trying to make is :
If variable 1 is true and not 2 then send signal to counter to start counting.

show me a screenshot of your flow

I have read multiple items on interval every 1 sec

As stated before:

Also, your node does not look right, it should have 3 outputs.

Ensure it is set to "READ MULTIPLE" mode
image

1 Like