# Delay Reading Serial Port and Displaying Data

**URL:** <https://discourse.processing.org/t/delay-reading-serial-port-and-displaying-data/10071>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 8, 2019, 5:59am UTC](https://discourse.processing.org/t/delay-reading-serial-port-and-displaying-data/10071 "2019-04-08T05:59:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SearayCA](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@SearayCA](https://discourse.processing.org/u/SearayCA)\
**Post date:** [April 8, 2019, 5:59am UTC](https://discourse.processing.org/t/delay-reading-serial-port-and-displaying-data/10071/1 "2019-04-08T05:59:32Z")

</div>

We are using a line scan camera,10 bit data, 128 pixels per line. We reduce this to 8 bit data. We are attempting to read serial data at 115200 baud, 130 bytes at a time, header plus data. We are displaying a vertical line of 128 samples at a time, and then scrolling 1 pixel to the right for each update. It works with one exception: it pauses every 2-3 seconds. Ugh.

I have tried running on both Mac and Windows, both fast machines. If I eliminate the serial read and dummy the data, it is very fast.

If I guessed, it seems like garbage collection might be the issue. You could test using an Arduino at 115200 baud, 130 byte data packets like “0x0a 0x0d 1 2 3 4 …”

Any assistance or suggestions would be appreciated.

Thank you,

Greg

```auto
import processing.serial.*;

Serial myPort; // The serial port
int[] inBuffer = new int[1000]; // where to put data...

void setup()
  {
  size(1500, 128);

  // List all the available serial ports:
  printArray(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my FTDI adaptor, so I open Serial.list()[0].
  // In Windows, this usually opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 115200);
  frameRate(24); // draw method is called this many times per second
  }

int inByte = -1;  
int count =0;
int prevIn = -1;
int newdata = 0;
int packetCount = 0;

long now = millis()+1000;

void serialEvent(Serial myPort) // this get triggered with each serial event
  {
    inByte = myPort.read(); // read data
    inBuffer[count++] = inByte; // buffer it
    if (count>150) 
    {
// println("Overflow...");
      count = 0; // if packet overflows... restart packet
    }
    
    if ((prevIn==0x0a) && (inByte==0x0d)) // end of packet... linefeed & return
      {
        count = 0;
        newdata ++;
        packetCount++;
        if (millis()>now)
        {
// println("packets = " + packetCount);
          packetCount=0;
          now = millis()+1000;
        }
      }
    prevIn = inByte;
  }

int BLOCK_SIZE = 1000;

void draw()
  {

    for (int l=0; l<BLOCK_SIZE; l++) // loop for BLOCK_SIZE within draw
    { 
    if (newdata>0)
    {
      newdata--;
      loadPixels(); // get screen buffer
        for (int y = 1; y<height; y++) // move all pixels right line by line
          {
          for (int x = width-1; x>0 ; x--) // column by column
            {
            color c = pixels[x-1+ (y*width)]; // get the color
            pixels[x+ (y*width)] = c; // move the color
            }
          }
        updatePixels(); // put back buffer
  //color red = color(random(255),random(255),random(255)); // simulate thomas data
        for (int y = 0; y < height; y++) // step throught the newly collected line
          {     
            int r = inBuffer[y]; // get the shade
            color c = color(r*1, r*1, r*1); // set the color
            set(0, y, c); // set the pixel
          }
        }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 10, 2019, 4:34am UTC](https://discourse.processing.org/t/delay-reading-serial-port-and-displaying-data/10071/2 "2019-04-10T04:34:34Z")

</div>

I’m not a serial user, but this reminds me of an old @kfrajer answer about frame rate vs serial speed, and filling up the buffer.

- [Delay in Serial plot using Processing - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/108440/#Comment_108440)

another possibly related past post:

> [@Using Serial data in real time?](https://discourse.processing.org/t/using-serial-data-in-real-time/5056/3):
>
> Yes, of course, here is the code for Arduino IDE: /\* Arduino Starter Kit example Project 14 - Tweak the Arduino Logo This sketch is written to accompany Project 14 in the Arduino Starter Kit Parts required: - 10 kilohm potentiometer Software required: - Processing (3.0 or newer) http://processing.org - Active Internet connection created 18 Sep 2012 by Scott Fitzgerald http://www.arduino.cc/starterKit This example code is part of the public domain. \*/ void setup()…
