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