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

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 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

Where did that come from - the serial port? If so, your Java app must have sent it? Or perhaps you have misconfigured the serial port settings?

Remember, I cannot see your flow so I can only guess unless you share flows/screenshot

Steve-
It seems we have already found the root of the problem. The command that we want to send with Java always ends in "\ n". We had to write this same line as is in node-red. We made that modification and it seems to be working. The little new challenge that we are seeing is that when we execute several lines of code nexted with IFs, they all work except the first one; for some reason the first one gives us a passed value that we no longer use inside our IF loops; but we have already solved it. These are the lines of code.

if (msg.payload == "true\n")
{
msg.payload = {opcuaCommand:"addVariable"}
msg.topic = "ns=4;s=TQ1_ftlbs;datatype=Double;value=2.03";
return msg;
}

else if (msg.payload == "false\n")
{
msg.payload = {opcuaCommand:"addVariable"}
msg.topic = "ns=4;s=TQ1_ftlbs;datatype=Double;value=2.02";
return msg;
}

else if (msg.payload == "2\n")
{
msg.payload = {opcuaCommand:"addVariable"}
msg.topic = "ns=4;s=KEB_torque_calc;datatype=Double;value=3.03";
return msg;
}

else if (msg.payload == "0\n")
{
msg.payload = {opcuaCommand:"addVariable"}
msg.topic = "ns=4;s=torque_peak;datatype=Double;value=4.04";
return msg;
}

else if (msg.payload == "3\n")
{
msg.payload = {opcuaCommand:"addVariable"}
msg.topic = "ns=4;s=gox_SW3GreenButtonLED;datatype=Boolean;value=False";
return msg;
}

else if (msg.payload == "4\n")
{
msg.payload = {opcuaCommand:"addVariable"}
msg.topic = "ns=4;s=gox_SW4OrangeButtonLED;datatype=Boolean;value=False";
return msg;
}

return msg;

Thank you

Please post code using the code formatting of the forum, it makes it far easier for people to read.
image

Using SPLIT to separate two parts of a message, so we can send only one part to a PLC

How can we use the Split function to separate the two parts of a message so that we can send only one part of the message to a PLC?

Our project has a total of 18 variables. Each variable must have a different value. My GUI must identify each variable, extract its corresponding value from the created GUI, and send it to a PLC. Here we share part of the code for 4 of the 18 variables.

if (msg.payload == 1)
{
    msg.payload = {opcuaCommand:"addVariable"}
    msg.topic = "ns=4;s=torque_peak;datatype=Double;value=4.04";
}
else if (msg.payload == 2)
{
    msg.payload = {opcuaCommand:"addVariable"}
    msg.topic = "ns=4;s=rValuePOT1;datatype=Double;value=1.01";
}
else if (msg.payload == 3)
{
    msg.payload = {opcuaCommand:"addVariable"}
    msg.topic = "ns=4;s=TQ1_ftlbs;datatype=Double;value=2.02";
}
else if (msg.payload == 4)
{
    msg.payload = {opcuaCommand:"addVariable"}
    msg.topic = "ns=4;s=KEB_torque_calc;datatype=Double;value=3.03";
}
else
{
    msg.payload={}
    msg.topic=""
}
return msg;

The GUI works in JAVA using Netbeans and we have created a String which we split with a &; the first word must identify the variable and the second give the value of the variable, example: variable1&value1. We have achieved this successfully and node-red reads it as it is sent using a virtual serial port.
This is the command used to send the message through the virtual serial port

String datainn=dataintxt.getText(); // datainn=50
serialtest.sendData("true"+"&"+datainn+"\n");

The problem we have now is being able to properly separate the String; the Split block perfectly divides my information into VARIABLE1 and its VALUE1 to be able to identify the variable on which we must act and give it its value. What we can't get is to get the function block sending the VALUE1 separately to the PLC.

When we execute the Split, the VARIABLE1 identifies the IF loop but it does not give us the value of VALUE1. We have even tried that the GUI send two separate messages: the first message to identify the variable and the second for its value, but it only takes the variable name and its value is left up in the air.

if (msg.payload == "true")
{
    msg.topic = "ns=4;s=TQ1_ftlbs;datatype=Double;value= "+msg.payload;
    msg.payload = {opcuaCommand:"addVariable"}
   
    return msg;
}    

ezgif.com-gif-maker-2

How can we use the Split function to separate the two parts of the message so that I can send only one part of the message to the PLC?

It seems you (or your team) are developing this Java app. Why don't you send JSON instead of custom strings joined by &?

You would have solved this days ago. No string splitting, data values & topics available with zero effort.

Do you understand what the benefit of sending JSON offers? Do you understand what JSON is?


Lastly, if you absolutely MUST use this custom strings format, please show me 2 demo strings and how they should look after the function node.

E.g. xyz&124 should send { "topic" : 124 }


Ps, to copy the string exactly as it appears in the debug window, use the "copy value" button that appears under your mouse when you hover over the value in the debug window.

Do you understand what I asked or have you solved this?

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