Skip blank lines from network data

Hi there,

Constitution:
bloody beginner

Situation:
I try to receive serial data into processing via LAN from a Raspberry Pi that in turn receives the data from /dev/ttyUSB0 connected Arduino (ADC Data, println every 2ms)
The data looks like that:

646
382
364
374
364
...

The chain looks like that:

Arduino ADC -> Serial.println ->RaspberryPi -> nc 192.168.1.100 5555 < /dev/ttyUSB0 -> ClientPC 192.168.1.100 port 5555 ->processing

Problem:
For some unknown reason I get a blank line between every data line. The blank line does not exist at RaspberryPi side… I checked that with “screen /dev/ttyUSB0 38400”.

Now I use this code in processing to receive the data and output it in the console:

import processing.net.*;

Server myServer;
int port = 5555;

void setup(){
 size(2,2);
 myServer = new Server(this, port);
}

void draw() {
    Client thisClient = myServer.available();
    if(thisClient != null){
      String whatClientSaid = thisClient.readString();
      if(whatClientSaid != null){
       println(whatClientSaid);
      }
    }
}

And the output looks like this:

589

592

602

614

621

622

I cant figure out where in my chain the blank line gets inserted, maybe netcat is doing that. Does anyone have a idea how to get rid of the blank line? Or how to skip those lines in processing since they seem not to match “!=null”…
Any ideas?

1 Like

yes,
“null” means empty
but the arduino uses to send CR LF

Thanks. I found another working approach:

On the RaspberryPi I now use “socat” to send the data from “ttyUSB0” to a listen port 5555, then on the ClientPC I use socat again to connect to the raspberryPi and
relay the data to a virtual device called “ttyVUSB0”:

RaspberryPi:
socat /dev/ttyUSB0,raw,echo=0 tcp-listen:5555,reuseaddr

ClientPC:
sudo socat PTY,raw,echo=0,link=/dev/ttyVUSB0cp:<RaspberryPi-IP>:5555

Now I can simply use processing serial interface to receive the data:
myPort = new Serial(this, "/dev/ttyVUSB0" , 38400);

Data is now received without blank lines.
I had to “chown youruser:users /dev/ttyVUSB0” to gain permission
to the device in processing.

3 Likes

Well, it turned out this approach was also not ideal.
Everytime I stopped processing and started it again it complains
about the port being busy, so I had to stop the whole chain and restart all.

I ended up in using the network way in processing again:

this time only using one socat on the raspberryPi when processing
has been started and is listening for an incoming connection from socat:

ClientPc(processing)

...
myServer = new Server(this, SERVERPORT);
...
Client thisClient = myServer.available();
...
myString = thisClient.readStringUntil('\n');
...

raspberryPi:
socat /dev/ttyUSB0,raw,echo=0 tcp:<ClientPcIp>:8888

Serial.println() adds a carriage return and line feed to end of string.
https://www.arduino.cc/reference/en/language/functions/communication/serial/println/

println() adds a carriage return and line feed to the console print.

So…

Serial.println("012");

is same as sending:
Serial.print("012\r\n");

On the receiving end you were not getting a “blank line” you were printing a string with a carriage return and line feed and printing a carriage return and line feed with:

println("012\r\n");

:slight_smile:

4 Likes

So if I understand correctly CR+LF is practically two newlines at once?
But why does the ArduinoIDE Serial Monitor not show both too? does it ignore one of it? Also “screen /dev/ttyUSB0 38400” does not show these
double newlines… why does processing show them?

Thanks for socat tip!

Came across this:

I will be exploring it further later in the year.

You may also want to explore:
https://processing.org/reference/libraries/net/Client.html
https://processing.org/reference/libraries/net/Client_readStringUntil_.html
https://processing.org/reference/trim_.html

:slight_smile:

1 Like

That is not correct.

Serial.println(“012”); sends “012” to serial monitor along with a carriage return and line feed from the Serial.println() to serial monitor.

Serial.println(“012”) that is sent as a string to Processing sends “012\r\n”.

If Processing receives this string “012\r\n” and does a print(“012\r\n”) it will print ONE carriage return and line feed that is already in the string.

If you do a println(“012\r\n”) you have TWO carriage return and line feeds; ONE from string and TWO from the println();

Just a Serial.println(“”) alone will send a carriage return and line feed.

It will come to you…

glv

1 Like

thanks a lot, got it :upside_down_face:

1 Like