自定义协议传输格式

参考自定义协议格式:
自定义协议传输格式_第1张图片

串口接收处理程序:`

void DealNAVIComMag(void)
{
    u8 i,data;        
    static u16 MagLen,MagLenMax;    
    static u8 ComMagStatus;
    static u8 checksum;
    static u8 MsgPackage[256];

     //DealLinHostProc();
     //return;

    if( FIFO_GET_ONE(McuUart3_RxFifo,&data))
    {        

        switch (ComMagStatus)
        {
        case 0:
            if (data == 0xA0) 
            {
                ComMagStatus = 1;
                checksum  = 0;
                //printf("\r\n0x%02X ",data);
            }
            else ComMagStatus = 0;
            break;
        case 1:
            {
                ComMagStatus = 2;
                MagLenMax = data;
                MagLen = 0;
                checksum += data;
                //printf("\r\n0x%02X ",data);
            }
            break;
        case 2:
            if(MagLen < MagLenMax)
            {
                MsgPackage[MagLen] = data;
                checksum += data;
                MagLen ++;
            }
            else
            {
                MsgPackage[MagLen] = data;
                if(data == checksum)
                {
                #if 1
                    printf("\r\n[GPS >>>>>>> MCU]:");
                    for(i=0;i<MagLen;i++) 
                    {
                        printf("0x%02X ",MsgPackage[i]);
                    }
                    printf("\r\n"); 
                #endif
                    NAVICmdAnalysis(MsgPackage,MagLen);
                }
                else
                {
                      printf("\n[GPS >>>>>>> MCU]:--->ERROR:");
                      for(i=0;i<(MagLen+1);i++)
                      {
                            printf("0x%02X ",MsgPackage[i]);
                      }
                      printf("\n"); 
                }
                ComMagStatus = 0;
            }
            break;
        default:
            ComMagStatus = 0;
            break;
        }
    }
}

你可能感兴趣的:(c语言)