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
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++;
}
}
}
}
}