给你贴在这吧。
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 100
#define MSG_SIZE 10
int (*fpState)(void); // funchtion pointer for state switch
static int r_offset = 0; // read pointer of buffer which stores all the data from serial port
static int r_offset_mirror = 0;
static int w_offset = 0; // write pointer of buffer which stores all the data from serial port
static int d_offset = 2;
int bValidMsg; // Flag of valid 0xAA72 message.
unsigned char MsgBuf[BUF_SIZE]; // circle buffer, MAX fullness is only one free byte to avoid write ptr catch read ptr.
unsigned char Msg[MSG_SIZE]; // 10 bytes message
int ReadHeader0(void);
int ReadHeader1(void);
int ReadData(void);
int ReadTail0(void);
int ReadTail1(void);
int ReadTail2(void);
int ReadTail3(void);
int GetBufFullness(void)
{
int valid_byte_cnt;
valid_byte_cnt = w_offset - r_offset;
if ( valid_byte_cnt < 0 )
{
valid_byte_cnt += BUF_SIZE;
}
return valid_byte_cnt;
}
int ReadHeader0(void)
{
unsigned char temp;
temp = MsgBuf[r_offset];
d_offset = 2;
bValidMsg = 0; // means no valid msg received.
r_offset += 1;
if (r_offset == BUF_SIZE)
{
r_offset = 0;
}
if (temp == 0xAA)
{
Msg[0] = 0xAA;
fpState = &ReadHeader1; // enter next state at find first 0xAA
return temp;
}
return 0;
}
int ReadHeader1(void)
{
unsigned char temp;
temp = MsgBuf[r_offset];
if ((temp == 0x72) || (temp == 0x73))
{
r_offset += 1;
if (r_offset == BUF_SIZE)
{
r_offset = 0;
}
Msg[1] = temp;
r_offset_mirror = r_offset; // save this value, if errors, then re-search 0xAA from this byte
fpState = &ReadData; // next state to read data
return temp;
}
else
{
fpState = &ReadHeader0; // re-search 0xAA from this byte
}
return 0;
}
int ReadData(void)
{
Msg[d_offset] = MsgBuf[r_offset];
d_offset += 1;
if (d_offset == 6)
{
fpState = &ReadTail0; // next state after read 4 bytes data.
}
r_offset += 1;
if (r_offset == BUF_SIZE)
{
r_offset = 0;
}
return 1;
}
int ReadTail0(void)
{
unsigned char temp;
temp = MsgBuf[r_offset];
if (temp == 0xCC)
{
r_offset += 1; // read pointer ++
if (r_offset == BUF_SIZE)
{
r_offset = 0;
}
Msg[6] = temp;
fpState = &ReadTail1; // next state
return temp;
}
else
{
fpState = &ReadHeader0; // re-search 0xAA
r_offset = r_offset_mirror; // need to re-search from the byte following AA73 or AA72.
}
return 0;
}
int ReadTail1(void)
{
unsigned char temp;
temp = MsgBuf[r_offset];
if (temp == 0x33)
{
r_offset += 1; // read pointer ++
if (r_offset == BUF_SIZE)
{
r_offset = 0;
}
Msg[7] = temp;
fpState = &ReadTail2; // next state
return temp;
}
else
{
fpState = &ReadHeader0; // re-search 0xAA
r_offset = r_offset_mirror; // need to re-search from the byte following AA73 or AA72.
}
return 0;
}
int ReadTail2(void)
{
unsigned char temp;
temp = MsgBuf[r_offset];
if (temp == 0xC3)
{
r_offset += 1; // read pointer++
if (r_offset == BUF_SIZE)
{
r_offset = 0;
}
Msg[8] = temp;
fpState = &ReadTail3; // next state
return temp;
}
else
{
fpState = &ReadHeader0; // re-search 0xAA
r_offset = r_offset_mirror; // need to re-search from the byte following AA73 or AA72.
}
return 0;
}
int ReadTail3(void)
{
unsigned char temp;
temp = MsgBuf[r_offset];
fpState = &ReadHeader0; // next state will read 0xAA
if (temp == 0x3C)
{
r_offset += 1;
if (r_offset == BUF_SIZE)
{
r_offset = 0;
}
Msg[9] = temp;
bValidMsg = 1; // means that one valid msg received.
return temp;
}
else
{
r_offset = r_offset_mirror; // need to re-search from the byte following AA73 or AA72.
}
return 0;
}
void main(void)
{
int i,j;
char Msg1[10] = {0xAA,0x73,0x66,0x66,0x66,0x66,0xCC,0x33,0xC3,0x3C};
char Msg2[10] = {0xAA,0x72,0x77,0x77,0x77,0x77,0xCC,0x33,0xC3,0x3C};
int valid_data_cnt;
// Test example
w_offset = 29;
r_offset = 0;
for (i=0; i<10; i++)
MsgBuf[i] = Msg1[i];
for (i=10; i<20; i++)
MsgBuf[i] = Msg2[i-10];
for (i=20; i<w_offset; i++)
MsgBuf[i] = Msg1[i-20];
fpState = &ReadHeader0;
while ( GetBufFullness() )
{
fpState();
if (bValidMsg)
{
if(Msg[1] == 0x72)
{
// valid msg received.
// data processing
printf("find a valid message\n");
}
}
}
} |