We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Not catching up with serial port data
Page Index Toggle Pages: 1
Not catching up with serial port data (Read 1016 times)
Not catching up with serial port data
Apr 26th, 2006, 11:28am
 
Hi,
im trying to get some flock of birds sensors running on my powerbook g4 using eclipse. The flock of birds runs at 115200baud but it seems to be unable toread out the whole buffer before it gets filled up, leaving me with thousands of bytes being available in the serial port.
I have modified the Serial class slightly so that it  is not dependent on the PApplet class, but rather sends the serialEvent to the class that interprets the sensor data. the code for the serialEvent in the Serial class looks like this now:
Quote:


synchronized public void serialEvent(SerialPortEvent serialEvent) {
 if (serialEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
   try {
     while (input.available() > 0) {
       synchronized (buffer) {
         if (bufferLast == buffer.length) {
           byte temp[] = new byte[bufferLast << 1];
           System.arraycopy(buffer, 0, temp, 0, bufferLast);
           buffer = temp;
         }
         buffer[bufferLast++] = (byte) input.read();
       }
     }
   }
   catch (IOException e) {
     errorMessage("serialEvent", e);
   }
 }
 parent.serialEvent(serialEvent);
}




and the code in the Flock class that handles the data interpretation (every data set begins with a byte where the most significant bit is high, this method searches from the beginning of a data set to the beginning of the next data set and passes it on for further processing.):

Quote:


private byte lastByte = 0x00;
private byte[] portBuffer = new byte[128];
private int phaseCount = 0;
private int byteCount = 0;

public void serialEvent(SerialPortEvent e){

 if(!flying) return;
 if(e.getEventType() != SerialPortEvent.DATA_AVAILABLE) return;
 logger.log(Logger.DEBUG, Logger.SERIAL_PORT, "SerialEvent(): bytes available: " + port.available());
 
 if(phaseCount == 0 && (lastByte & 0x80) > 0 ) {
   phaseCount = 1;
   byteCount = 0;
   portBuffer[byteCount] = lastByte;
   lastByte = 0x00;
 }

 while(port.available() > 0 && phaseCount < 2){
   byte b  = (byte) port.read();
   if((b & 0x80) > 0) {
     phaseCount++;
     if(phaseCount == 1) byteCount = 0;
     if(phaseCount == 2) lastByte = b;
   }
   if(phaseCount == 1){
     portBuffer[byteCount] = b;
     byteCount++;
   }
 }
 
 if(phaseCount >= 2){
   byte address = (byte) (portBuffer[byteCount - 1] - 1);
   byte[] data = PApplet.subset(portBuffer, 0, byteCount);

   if( (address < 0) || (address >= (int) birds.size( )) ) {
     //corrupt data, do nothing
     logger.log(Logger.WARNING, Logger.STREAM, "flock.update(): bad bird address: " +
       address + " birds size " + birds.size());
     return;
   }

   ((Bird) birds.get(address)).update(data);
   phaseCount = 0;
   byteCount = 0;
 }

}




once the data is passed on, no big amounts of processing is done(only some multiplication).
So my question is, can anyone see a reason why it cannot catch up with the incoming byte stream? is the algorithm too slow, or does the serialEvent not update often enough or can it be something else?

if anything is not clear please let me know...

thanks in advance

Henrik
Page Index Toggle Pages: 1