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

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.