打印
[AVR单片机]

判断数据

[复制链接]
1582|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chrisbo|  楼主 | 2010-6-12 08:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ATmega128与终端进行串口通信,终端一有动作就会一直向MCU发送10个字节一组的AA 73 XX XX XX XX CC 33 C3 3C数据,动作结束后发送AA 72 XX XX XX XX CC 33 C3 3C数据,而MCU要进行处理和有用的数据就是最后的这组,要用什么样的语句结构来判断串口已经收到这组数据呢?请高手们给予指点指点。。

相关帖子

沙发
ershisi| | 2010-6-12 22:44 | 只看该作者
发送的数据还有空格?

使用特权

评论回复
板凳
wuzhaolie| | 2010-6-12 23:19 | 只看该作者
那不是发送的空格,而是显示软件自己加上的

使用特权

评论回复
地板
chrisbo|  楼主 | 2010-6-13 10:26 | 只看该作者
3# wuzhaolie
3楼的正解

使用特权

评论回复
5
aresc| | 2010-6-13 10:57 | 只看该作者
给你贴在这吧。

#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");
                        }
                }
        }

}

使用特权

评论回复
评分
参与人数 1威望 +1 收起 理由
chrisbo + 1
6
chrisbo|  楼主 | 2010-6-13 16:45 | 只看该作者
5# aresc
让我来慢慢消化下。。再次感谢ARESC!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

55

帖子

0

粉丝