Delay Reading Serial Port and Displaying Data

#1

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

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
          }
        }
    }
  }
#2

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.

another possibly related past post: