"Junk" data on first connect


i've written simple arduino program, monitors state of equipment. talks c program running on pc via text commands using usb-serial link. works great apart fact first time plug arduino (an uno) computer (or unplug , re-plug in) pc program receives byte of "junk" data before else happens.

i've reduced code snippet below. sets serial port up, , reads lines of text. if receives whole line of text before timeout nothing. if receives text without terminating lf before timeout sends text "error\n" serial port.

if comment out code sends "error\n" serial port in case of timeout don't see junk data. similarly, if keep "charsread" @ 0 (so never reach timeout) don't see data. however, if leave code as-is first time plug arduino in , run pc program sees byte of junk data.

given symptoms, seems possible when plug arduino computer (ubuntu 11.10 32-bit) computer sends arduino data code doesn't recognise. code responds error message gets left in buffer until run pc program. seem plausible or have suggestions other potential problems?

thanks,

tom

code: [select]

#include <string.h>

#define maxnumchars 30                     // size of buffer use when reading commands usb serial bus (don't make large or run out of ram)
#define readtimeout 100                    // timeout (ms) use reading serial port in ms n.b. maximum time wait between chars (not limit on total read time)

unsigned long lastreadtime;                // used check timeout when reading commands usb serial
unsigned int charsread = 0;
char readbuffer [maxnumchars + 1];         // used read commands (n.b. room null terminator)
const char terminators []  = " \t\f\v\r\n";

void setup(void)
{
  serial.begin(115200);
  lastreadtime = millis();
}


void loop(void)
{
processcommand();                  // process commands recieved on usb-serial bus
}

void processcommand()
{
  if(!serial.available())                                  // no data in serial buffer, check timeout , return
  {
    if(!charsread)                                         // no chars in buffer nothing do
      return;

    if( (millis()-lastreadtime) > readtimeout || millis() < lastreadtime)            // we've reached timeout before getting full line of data, discard current buffered data , begin read again
    {
      serial.write("error\n");
      charsread = 0;
    }
    return;
  }
 

  lastreadtime = millis();                              // update timeout
  char inbyte = serial.read();

  if('\n' != inbyte)                                    // we've read character check buffer overruns , save char
  {
    if(charsread == maxnumchars)
    {
      serial.print("error\n");
      charsread = 0;
      return;
    }
    readbuffer[charsread++] = inbyte;
    return;
  }

  readbuffer[charsread] = '\0';                          // full command read, null terminate string , process
  charsread = 0;

  char *pos = strtok(readbuffer, terminators);
  if('\0' == pos)
  {
    serial.print("error\n");
    return;
  }

  charsread = 0;
 
}





Arduino Forum > Using Arduino > Programming Questions > "Junk" data on first connect


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial