# Trouble with the Serial port read

**URL:** https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724
**Category:** Electronics (Arduino, etc.)
**Created:** [April 28, 2019, 6:26pm UTC](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724 "2019-04-28T18:26:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [April 28, 2019, 6:26pm UTC](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724/1 "2019-04-28T18:26:56Z")

</div>

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

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

```auto

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 29, 2019, 1:25am UTC](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724/2 "2019-04-29T01:25:50Z")

</div>

your serial code is based on?  
[https://processing.org/reference/libraries/serial/Serial\_readStringUntil\_.html](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](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](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](https://processing.org/reference/libraries/serial/serialEvent_.html)  
is better as a check inside draw()

* * *

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [April 29, 2019, 5:46pm UTC](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724/3 "2019-04-29T17:46:30Z")

</div>

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:

```auto
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];
    }
  }
} 

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 30, 2019, 6:22am UTC](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724/4 "2019-04-30T06:22:46Z")

</div>

still using trim and match can make it better,  
add

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

```

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [May 2, 2019, 12:16pm UTC](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724/5 "2019-05-02T12:16:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 2, 2019, 12:29pm UTC](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724/6 "2019-05-02T12:29:11Z")

</div>

see  
[Issue with using array after splitTokens()](https://discourse.processing.org/t/issue-with-using-array-after-splittokens/10826/4) here
