Hmm... the logic of the code is fine because I'm using if() else if() statements... when one matches it should automatically jump out of them. It seems that the Bi-directional chip I was using was creating some sort of feedback to the receive pin on the Teensy... just took care of the problem by doing a little Serial.clear() after sending my error code. Thank you for the help guys, especially Paul for sending me the scope pics.
Here is my fixed code to get rid of the issue.
int temp;
int k = 0;
char array[5] = {0x3A,0xD8,0x00,0x01,0xE9};
//char array[8] = {0xD,0x8,0x0,0x0,0x0,0x1,0xE,0x9};
char inchar[5]={0};
void setup()
{
Serial1.begin(19200);
pinMode(13,OUTPUT);
}
void loop()
{
char inChar = -1;
byte numBytesAvailable = 0;
int ValidCmd = 0;
numBytesAvailable = Serial1.available();
char cmdword[4]={
0x3,0xA,0xD,0x8 };
if (numBytesAvailable > 0)// if there is something to read
{
delay(30);
numBytesAvailable = Serial1.available();
int i;
int j = 0;
if(numBytesAvailable == 5)
{
for (i = 0; i < numBytesAvailable; i++)// store everything into "inChar"
{
inChar = Serial1.read();
}
}
else if(numBytesAvailable > 5)
{
// Error Code - Received Command Word is too Long
char temp[5] = {
0x3A,0x79,0x00,0x02,0xEE };
digitalWrite(13,HIGH);
for(int i=0;i<5;i++)
{
Serial1.print(temp
,HEX);
Serial1.flush();
}
Serial1.clear();
digitalWrite(13,LOW);
}
else if(numBytesAvailable < 5 && numBytesAvailable > 0)
{
// Error Code - Received Command Word is too Short
char temp[5] = {
0x3A,0x79,0x00,0x01,0xEF };
digitalWrite(13,HIGH);
for(int i=0;i<5;i++)
{
Serial1.print(temp,HEX);
Serial1.flush();
}
Serial1.clear();
digitalWrite(13,LOW);
}
}
}