Can not get this USB serial code to fill up a receive buffer - aaahh

Status
Not open for further replies.

Phoebus

Member
Hello Everyone,

Trying to get this serial code to fill up a receive buffer. Using $ as the start of msg and CR as the end. Seemed like a simple thing to do but nooo . Spent days on this and am getting nowhere.

The code has a lot of troubleshooting println 's in it. I'll attached as much documentation as I can for this code as I would appreciate fresh eyes taking a look and what the @#$! I'm doing wrong.

The doc's consist of the ino code, a word doc of three screen caps whoing the output I'm getting from three iterations of code (each iteration contains more troubleshooting code, and lastly a vizio file of the flowchart I'm basing the code on. (and Yes I've been told that using hile statements instead of Goto's would be better.

Anyways, any help or comments would be greatly appreciated.
Thanks


Code:
void setup()
{
   Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
   Serial.println("starting");
   pinMode(6,OUTPUT);
}
void checkForRecvdChar (); //protoype declaration

void loop()
{
  checkForRecvdChar();
}

void checkForRecvdChar()
{
  static int recByteCtr = 0; //declare and initialize the rec'd byte counter variable
  char byteRead;
  char recBuffer [255];
  //char *recBufferPtr = recBuffer;
  
  top:
  if (Serial.available()) //then let's read the byte
  { 
    if (recByteCtr == 0) //then check to see if it's the start of a message
    {
      byteRead = Serial.read(); //read the byte
      if (byteRead == '$')
      {
        Serial.print ("byte ctr @ ");
        Serial.print (recByteCtr);
        Serial.print (" read ");      
        Serial.println(byteRead);
        recBuffer[recByteCtr] = byteRead; //copy the byte read into the rec buffer
        recByteCtr++;
        goto top;
      }
      else //recByteCtr ==0 && byteRead != '$'
      {
        Serial.print ("byte ctr @ ");
        Serial.print (recByteCtr);
        Serial.print (" read ");      
        Serial.println(byteRead);
        goto top; //'cause first byte read wasn't == $
        //ie: we're receiving somewhere in the middle of a message
      }
    }
    if (recByteCtr != 0) //then we're reading bytes after receiving an STX in the message stream
    {
      byteRead = Serial.read(); //read the byte
      Serial.print ("BYTE CTR @ ");
      Serial.print (recByteCtr);
      Serial.print (" read ");      
      Serial.println(byteRead);
      if (byteRead != 13) //13 = CR
      {
        Serial.println("Still not at end of msg");
        Serial.print("Writing: ");
        Serial.print(byteRead);
        Serial.print(" to recBuffer[");
        Serial.print(recByteCtr);
        Serial.println("]");
        recBuffer[recByteCtr] = byteRead; //copy the byte read into the rec buffer
        recByteCtr++;
      }
      else //recbyte == 13
      {
        Serial.println("At end of msg");
        Serial.print("Writing: nul");
        Serial.print(" to recBuffer[");
        Serial.print(recByteCtr);
        Serial.println("]");
 
        recBuffer[recByteCtr] = '\0'; //null terminate the rec'd string in rec buffer
        recByteCtr = 0;
        int x = 0;
        while (recBuffer[x] !='\0')
       {
         Serial.print("recBuffer[");
         Serial.print(x);
         Serial.print("] = ");
         Serial.print(recBuffer[x]);
         Serial.print(" ");
         x++;
       }
      }
    }
  }
}
 

Attachments

  • sketch_jun05a.zip
    921 bytes · Views: 115
  • serial comm receive flowchart - no msglen.zip
    60.5 KB · Views: 133
  • serial recBuffer grief.zip
    573.7 KB · Views: 131
Should recBuffer[] be static or global scope, so it continues to exist across each run of the checkForRecvdChar() function?
 
of course, it's either got to be static, so it's not reinitialzed each time the function is called, or Global. I'm gonna try both, and let you know.
 
Thank you Paul, both techniques suggested work. Seems so obvious now. Any advantage or disadvantage to using Static vs Global?
 
Thank you Paul, both techniques suggested work. Seems so obvious now. Any advantage or disadvantage to using Static vs Global?
Global creates an externally visible symbol, that could be referenced in other modules given the right declaration. Static is only visible in the current module. In general, unless you need to share the buffer with other functions in different files (where you are not passing the pointer to the buffer), you should use static.
 
Sometimes the compiler will apply special optimizations if the variable is static, either local to the function or local to the entire file. But if code compiled from other files can access it, some optimizations can't be taken.
 
Sometimes the compiler will apply special optimizations if the variable is static, either local to the function or local to the entire file. But if code compiled from other files can access it, some optimizations can't be taken.
Yep, though if you use -flto, it can optimize across modules.
 
Status
Not open for further replies.
Back
Top