Implementing virtual Serial port communication: java with node-red (The Figures are attached in subsequent postings)

Implementing virtual Serial port communication: java with node-red

We have created a GUI using Netbeans (Java) and we used node-red to communicate with a PLC. We are also using virtual serial port because we have been working with the OPC UA Protocol. We have already connected Node-Red with our GUI to generate a graph with random numbers; Figure 1 shows it.

Our challenge is to send data with some conditionals. To test our progress, we have created this function on our Flow; Figure 2:

However, the answer we get is shown in Figure 3 on the debug.

We tried some modifications, for example, we put “COM7” , and Figure 4 shows the answer we get when we run it:

Figure 5 shows the message of each button of our GUI.

If we delete “/n” of our java code, the node-red doesn’t work. The next picture shows what happens when we click 5 times on the buttom Led 2 “encender (turn on).” After we click only 1 time on another button, the debug sends a message after the last click, here is a picture— Figure 6—about that:

We think Node-Red is waiting an enter or “/n” to send a message. The key questions is Where can we save the variable from serial port?

1 Like

These are the figures 1 and 2 related to my posting
Figure 1
Figure 2

This is figure 2 of my post
Figure 2

This is figure 3 of 6 of my post
Figure 3

This is figure 4 of 6 of my post
Figure 4

This is figure 5 of 6 of my post
Figure 5

This is figure 6 of 6 of my post
Figure 6

Hi.

It is not really clear what you are trying to achieve. Where does opcua come into this?

Are you are trying to access opcua data in node-red but are using a Java app to do the opcua and virtual serial ports as communication between java and node-red?

Thoughts...

  1. Can't you use opcua nodes in node-red without the use of Java app?
  2. What kind of plc are you communicating with - perhaps a protocol node would be better (e.g. node-red-contrib-omron-fins or Node-red-contrib-mcprotocol etc)
  3. If you must involve Java, why use virtual serial ports? Surely MQTT would greatly simplify this.

What node are you using for that, node-red-node-serial? I can't read your posted images.

Those nodes have various configurations for dealing with message size I seem to remember including some that control the use of /n.

What I really want is to store the information that arrives from the Serial in into a variable, so I can later use that variable in the function (a conditional function). The variable can be boolean or string

What I really want is to store the information that arrives from the Serial in into a variable, so I can later use that variable in the function (a conditional function). The variable can be boolean or string. Can this be done?

I can certainly help you or point you in the right direction however you haven't answered any of my questions. I feel there may be a better way to achieve your final goal than messing on with virtual serial ports and custom string parsing.

Thank you for your kind note:
1.- No we can't do it
2.- No, we can't do it
3.- We must use Java. We are using serial ports due to other parts of the system that we can't describe.

In more succinct terms, what we really want is to store the information that arrives from the Serial In into a variable, so we can later use that variable in the function (a conditional function). The variable can be Boolean or String.

Ok. Can you provide some sample data strings/buffers and describe what they mean and what parts you need to be "retained" for later use?

Ps, please provide text copies of this sample data (not screenshots)

Yes, I will do it in a few minutes, thank you

Yes, of course.

What you described in your first post doesn't really relate to this. You just need to know what terminates a "message" from the serial port. Typically this would be an end of line marker. But it could be that all of your messages have a fixed byte or character length. In that case, you need to configure the serial node accordingly.

Thank you very much for the tip:
if (COM7 == 2)
{
msg.payload = {opcuaCommand:"addFolder"}
msg.topic = "ns=1;s=Variables";
}
return msg;

The answer we get:
function : (error)
"ReferenceError: COM7 is not defined
(line 1, col 1)"

We tried some modifications, for example, we put “COM7” , and Figure 4 (above) shows the answer we get when we run it. Here we transcribe it:
msg payload : string[2]
"2«"

Looking at figure 4, you receive 2« from COM7. As you can see, this value arrives in msg.payload so your function should be checking for msg.payload === "2«"

E.g...

if (msg.payload === "2«") {
  msg.payload = {
    opcuaCommand:"addFolder"
  }
  msg.topic = "ns=1;s=Variables";
  return msg;
}
node.warn(`Unrecognised comm '${msg.payload}'`);
return null;




Forgive me for saying but this is a basic fundamental of node-red - I believe you would benefit from spending a bit time learning the basics....


1


I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.


2


There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages


3


To make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

Thank you very much for your kind feedback.

Hi.
I used your recomendation, but the debugger send me:
"Unrecognised comm '3«'
I don't know why