Trouble with the Serial port read

I am getting a periodic string from an Arduino Mega, about once a second.
I parse it and get various values from it.
About every 20 messages I get this weird error, and I dont know why.

Here is my receiving code

  while ( myPort.available() > 0 ) 
  {  // myPort.clear();
  
 
  IncomingSTR = myPort.readStringUntil('\n');   // read it and store it in val
   println (IncomingSTR);
   if (IncomingSTR != null)
   {
  String[] list = split(IncomingSTR, ' ');
  MACH_ID= list [0];
  PeriodMillis =  list [1];
  Channel = list [2];
  Operator= list [3];
  MoreInfo = list [4];
   }

Here is the read of the String:


32A 1868 3 NAME INFO ;

43A 1337 3 NAME INFO ;

30A 1800 3 NAME INFO ;

35A 1607 3 NAME INFO ;

37A 1571 3 NAME INFO ;

null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
26B 2855 3 NAME INFO ;

34B 1703 3 NAME INFO ;


How could I get a “null” 20 times in a row?

When I look at the Arduino output in the Serial Monitor, all strings are nice and clear, no garbage…

I am running this at 112500 baud. I need the speed.

I tried various arduino manufacturers , including the orignal Italian one. THey all do it, but some chinese clones do it less.
At times the Processing Sketch crushes and I get a :ArrayIndexOutOfBoundsException: 3

The Index number varies. (Not always 3)

What hat am I doing wrong?

Thanks
Mitch

your serial code is based on?
https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html

so if you want print the "null"s up to you.


a arduino might send a CR LF,
so you guess why you have empty lines in your print
pls use a
https://processing.org/reference/trim_.html
on the instring


for the rest of the instring work need still much more error checking
-a-
the split will fail if there is no " "
so try a
https://processing.org/reference/match_.html
first

-b-
also first make sure that the array is that long before you use a list[4]
by using
if ( list.length >=5 ) // …


generally using the
https://processing.org/reference/libraries/serial/serialEvent_.html
is better as a check inside draw()


Thank you , thank you,

Using Serial Event was fixed all my problems. I tried it on 3 different arduinos and they all work fine.

Here is the code for other folks:

int lf = 10;      // ASCII linefeed 

void setup() { 
  size(400, 200); 
  printArray(Serial.list()); 
  myPort = new Serial(this, Serial.list()[0], 115200); 
  myPort.bufferUntil(lf);
} 

void draw() { 
  background(0); 
  //text("received: " + inString, 10,50);
} 

void serialEvent(Serial p) { 
  String IncomingSTR = p.readString(); 
  println(  IncomingSTR);

  if (IncomingSTR != null)
  {
    String[] list = split(IncomingSTR, ' ');
    if ( list.length >=5 )
    {
      MACH_ID= list [0];
      PeriodMillis =  list [1];
      Channel = list [2];
      Operator= list [3];
      MoreInfo = list [4];
    }
  }
} 
1 Like

still using trim and match can make it better,
add

 if ( list.length >=5 )    {
      MACH_ID= list [0];
      PeriodMillis =  list [1];
      Channel = list [2];
      Operator= list [3];
      MoreInfo = list [4];
    } else println("lost line: "+IncomingSTR);

Yes, I implemented that too.
But now I get an error if I start the sketch in the middle of a transmission, or the string is not complete.
Then the sketch does not run.

I get

34B 33664 O ;
5:12:32
lost line: 34B 33664 O ;

Error, disabling serialEvent() for COM7
null

I would rather just trash the partial string, and continue receiving.
Any thoughts?

Thanks
Mitch

see
Issue with using array after splitTokens() here