|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Filtering incoming serial data.... |
Posted: Fri Jun 01, 2007 10:16 am |
|
|
Hi All,
I'm trying to create some code that will ignore all incoming serial data except that which matches a particular criteria. I'm attempting to use a state machine concept to do this, but the code is becoming more and more unwieldy, and it's not working correctly. My code should only accept serial data strings that start with a '&' character, have an 'M' as the 5th character, and end with a 'CR' - everything else should be ignored. The desired serial string will look like this '&XX MXXXXXXXCR' - it's a variable length string with only the start character (&), the 5th character (M) and the end character (CR) being known ahead of time.
Here is what I've got so far:
Code: |
#INT_RDA // Interrupt driven RS232 routine
void rs232_isr(void)
{
char temp; // local data storage
temp = fgetc(Network); // get rx data
switch (RxState)
{
case 0: // If we got a '&', then the command is beginning.
if (temp == '&')
{
Index = 0;
RxState = 1;
RxBuffer[Index] = temp;
Index++;
//return;
}
break;
case 1: // Here we collect the checksum characters.....
RxBuffer[Index]=temp;
Index++;
if (Index = 5)
RxState = 2;
case 2: // If we got a 'M' then we have a correct command.
if (temp == 'M')
{
RxState = 3;
RxBuffer[Index] = temp;
Index++;
//return;
}
else
RxState = 0;
break;
case 3: // Store the character. If we got a CR or a LF, then the command is completed.
// Save the character to the receive buffer.
RxBuffer[Index]=temp;
// Check for buffer overflow.
if ( Index >= (RX_SIZE - 1) )
Index = 0;
else
Index++;
if ((temp == '\r') || (temp == '\n'))
{
RxState = 4;
RX_Command_Ready = TRUE;
//return;
}
break;
}
}
|
If someone could have a look and make some comments about this code, I'd appreciate it!!
Dan |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Filtering incoming serial data.... |
Posted: Fri Jun 01, 2007 10:46 am |
|
|
If someone could have a look and make some comments about this code, I'd appreciate it!!
Dan[/quote]
The compiled code is going to be the same as if statements. You might as well use if/else statements. I would suggest testing the index value for the if/else statements.
Code: | temp = fgetc(Network); // get rx data
if(!RX_Command_Ready)
{ RxBuffer[Index] = temp;
If (index==0 && temp=='&' ) index++;
if (index==5)
{ if(temp=='M') index++;
else index=0;
}
else
{ If (index!=0) index++;
}
if ( Index >= (RX_SIZE - 1) ) index=0;
if (((temp == '\r') || (temp == '\n')) && (index>5)) RX_Command_Ready = TRUE;
}
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|