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

**URL:** <https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278>\
**Category:** General\
**Created:** [8 January 2022 02:59 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278 "2022-01-08T02:59:47Z")\
**Posts on this page:** 8\
**Page:** 2

<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:** [11 January 2022 15:31 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/21 "2022-01-11T15:31:08Z")

</div>

> [@crativ2000](#):
>
> I used your recomendation, but the debugger send me:  
> "Unrecognised comm '3«'

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

---

<div class="post-metadata">

**Author:** ![crativ2000](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@crativ2000](https://discourse.nodered.org/u/crativ2000)\
**Post date:** [11 January 2022 19:35 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/22 "2022-01-11T19:35:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 January 2022 22:53 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/23 "2022-01-11T22:53:41Z")

</div>

Please post code using the code formatting of the forum, it makes it far easier for people to read.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35877cf6ded7c9b37d828305810dcb238d2e24e1.png)

---

<div class="post-metadata">

**Author:** ![crativ2000](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@crativ2000](https://discourse.nodered.org/u/crativ2000)\
**Post date:** [13 January 2022 18:11 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/24 "2022-01-13T18:11:50Z")

</div>

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

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

```auto
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.

 ![20220113 flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55c5d321d9a973363b15de96ffcf0d4ad2807f58.jpeg)

 ![20220113 test1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c7b6a8f4755bc2123561b23ab5682710b4be5f3.jpeg)

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.

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

```

 ![20220113 test2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/335b983b5347cf0e238a06215019fbf4342d0c1a.jpeg)

![ezgif.com-gif-maker-2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d9cc78921c349be2edc637f21b348330295e9d2a.gif)

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?

---

<div class="post-metadata">

**Author:** ![crativ2000](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@crativ2000](https://discourse.nodered.org/u/crativ2000)\
**Post date:** [13 January 2022 18:13 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/25 "2022-01-13T18:13:10Z")

</div>

> [@Implementing virtual Serial port communication: java with node-red (The Figures are attached in subsequent postings)](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/24):
>
> 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) { …

---

<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:** [13 January 2022 18:50 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/26 "2022-01-13T18:50:04Z")

</div>

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.

---

<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:** [14 January 2022 15:05 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/27 "2022-01-14T15:05:26Z")

</div>

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

---

<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:** [15 March 2022 15:06 UTC](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278/28 "2022-03-15T15:06:00Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/implementing-virtual-serial-port-communication-java-with-node-red-the-figures-are-attached-in-subsequent-postings/56278.md?page=1)
