Hi Geoff,
Thanks for your quick replies.
We actually do drop out of the function if the number of available bytes is less than 15 (I didn't mention that the constant COMMANDLENGTH is 15, I apologize):
if(bufferBytes >= COMMANDLENGTH)
{
// New Command Ready, Process It
My initial thought was to do what you suggested by using this if statement and exiting the function until there were at least 15 bytes in the buffer. I included the flushing of the buffer to ensure that if I received a bad packet that somehow had say 14 bytes that it would just flush completely and ensure that I could read a full 15 byte packet next time we received one. I suppose that if this function was executed half-way through receiving a packet that we could inadvertently flush part of a good packet... Something I had yet to consider. Any thoughts on that?
Also, I'm not exactly sure what you mean by "shape" of the command but we do have some validation criteria when we perform the "processCommand" function (the first byte is a "header byte" of value 255 and the last byte is a checksum that is checked in the "checksum_Test" function). The following is the first few lines of code from the "processCommand" function:
// Processes a given command
void processCommand(unsigned char command[])
{
unsigned char newNumericalObjectStatus[] = {command[5], command[4], command[3], command[2]}; //reverses the order of the byte array so that it can be properly converted from the array to a float
// Determine Command
if(command[0] == 0xFF && checksum_Test(command) == 0)
{
switch(command[1])
{
case 0x00: // Sync Packet
Serial.print("sync");
Serial.flush();
isSynced = true; //sets isSynced flag to true so that commands within main loop will run
break;
Thanks again,
Eric