"Junk" data on first connect

I’ve written a simple Arduino program, which monitors the state of some equipment. It talks to a c program running on a PC via text commands using the USB-serial link. Everything works great apart from the fact that the first time I plug the Arduino (an Uno) into the computer (or unplug it and re-plug it in) my PC program receives a byte of “junk” data before anything else happens.

I’ve reduced the code to the snippet below. This just sets the serial port up, and reads lines of text. If it receives a whole line of text before a timeout then it does nothing. If it receives some text without a terminating LF before the timeout then it sends the text “ERROR\n” to the serial port.

If I comment out the code that sends “ERROR\n” to the serial port in case of timeout then I don’t see the junk data. Similarly, if I keep “charsRead” at zero (so I never reach a timeout) then I don’t see the data. However, if I leave the code as-is then the first time I plug the Arduino in and run it my PC program sees a byte of junk data.

Given the symptoms, it seems possible that when I plug the arduino into my computer (Ubuntu 11.10 32-bit) the computer sends the Arduino some data that my code doesn’t recognise. My code then responds with an error message which gets left in a buffer until I run my PC program. Does this seem plausible or does anyone have any suggestions for other potential problems?

Thanks,

Tom

#include <string.h>

#define maxNumChars 30                     // Size of buffer to use when reading commands from USB serial bus (don't make too large or we run out of RAM)
#define readTimeout 100                    // Timeout (ms) to use for reading from serial port in ms N.B. this is the maximum time to wait between chars (not a limit on the total read time)

unsigned long lastReadTime;                // Used to check for timeout when reading commands from USB serial
unsigned int charsRead = 0;
char readBuffer [maxNumChars + 1];         // Used to read commands into (N.B. room for NULL terminator)
const char terminators []  = " \t\f\v\r\n";

void setup(void)
{
  Serial.begin(115200);
  lastReadTime = millis();
}


void loop(void)
{
 processCommand();                  // Process any commands recieved over the USB-serial bus
}

void processCommand()
{
  if(!Serial.available())                                  // No data in serial buffer, so check for timeout and return
  {
    if(!charsRead)                                         // No chars in buffer so nothing to do
      return;

    if( (millis()-lastReadTime) > readTimeout || millis() < lastReadTime)            // We've reached a timeout before getting a full line of data, so discard current buffered data and begin read again
    {
      Serial.write("ERROR\n");
      charsRead = 0;
    }
    return;
  }
  

  lastReadTime = millis();                              // Update timeout
  char inByte = Serial.read(); 

  if('\n' != inByte)                                    // We've read a character so check for buffer overruns and save the char
  {
    if(charsRead == maxNumChars)
    {
      Serial.print("ERROR\n");
      charsRead = 0;
      return;
    }
    readBuffer[charsRead++] = inByte;
    return;
  }

  readBuffer[charsRead] = '\0';                          // Full command read, so null terminate string and process
  charsRead = 0;

  char *pos = strtok(readBuffer, terminators);
  if('\0' == pos)
  {
    Serial.print("ERROR\n");
    return;
  }

  charsRead = 0;
  
}