数据帧接收解析及拼帧

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xh_xinhua/article/details/83448404
#include <stdio.h>
#define DATA_BUFF_SIZE		(1024*60)
#define MAX_MESSAGE_LEN         4
static unsigned char RXBUFF[MAX_RXBUFF_SIZE] = { 0 };

typedef struct {
	unsigned char Frame_header[MAX_MESSAGE_LEN];
	int datatype;			/*Data type*/
	int data_subtype;		/*Data subtype*/
	int data_sub_subtype;	/*Data sub subtype*/
	int length;				/*Data length*/
	char *NcmDataPtr;		/*Data*/
	unsigned char Frame_tail[MAX_MESSAGE_LEN];
}data_frame;

data_frame Deal_Rx_Data()
{
	int Clilen = 0;
	int datalen =0;
	unsigned int count = 0;
	unsigned int pos = 0;
	unsigned char *data_pointer = NULL;
	unsigned char Rx_DATABUFF[DATA_BUFF_SIZE];
	memset(Rx_DATABUFF, 0x0, sizeof(Rx_DATABUFF));
    data_frame header_struct = {0, 0, 0, 0, 0, NULL, 0};
	while(1)
	{
		memset(RXBUFF, 0, sizeof(RXBUFF));
		Clilen = BSP_ETH_Rx(RXBUFF); //socket接收函数
		if(Clilen > 0)
		{
			data_pointer = RXBUFF;
			if((data_pointer[Clilen -4]==0x5A) && (data_pointer[Clilen -3]==0xFF) && (data_pointer[Clilen -2]==0xA5)&&(data_pointer[Clilen -1]==0xFF))
			{
				if((data_pointer[0] ==0xA5)&&(data_pointer[1] ==0xFF)&&(data_pointer[2] ==0x5A)&&(data_pointer[3] ==0xFF))
				{
										header_struct.datatype = ((data_pointer[4])& 0xFF)| ((data_pointer[5]<<8)& 0xFF00)| ((data_pointer[6]<<16)& 0xFF0000)| ((data_pointer[7]<<24)& 0xFF000000);		
					header_struct.data_subtype = ((data_pointer[8])& 0xFF)| ((data_pointer[9]<<8)& 0xFF00)| ((data_pointer[10]<<16)& 0xFF0000)| ((data_pointer[11]<<24)& 0xFF000000);
					header_struct.data_sub_subtype = ((data_pointer[12])& 0xFF)| ((data_pointer[13]<<8)& 0xFF00)| ((data_pointer[14]<<16)& 0xFF0000)| ((data_pointer[15]<<24)& 0xFF000000);
					header_struct.length = ((data_pointer[16])& 0xFF)| ((data_pointer[17]<<8)& 0xFF00)| ((data_pointer[18]<<16)& 0xFF0000)| ((data_pointer[19]<<24)& 0xFF000000);	
	

					memcpy(Rx_DATABUFF,  data_pointer +20, header_struct.length);
					header_struct.NcmDataPtr = Rx_DATABUFF;

					return header_struct;
				}
				else
				{
					memcpy(Rx_DATABUFF +pos,  data_pointer, Clilen -4);
					header_struct.NcmDataPtr = Rx_DATABUFF;
					return header_struct;
				}
			}
			else
			{
				if((data_pointer[0] ==0xA5)&&(data_pointer[1] ==0xFF)&&(data_pointer[2] ==0x5A)&&(data_pointer[3] ==0xFF))
				{					
										header_struct.datatype = ((data_pointer[4])& 0xFF)| ((data_pointer[5]<<8)& 0xFF00)| ((data_pointer[6]<<16)& 0xFF0000)| ((data_pointer[7]<<24)& 0xFF000000);		
					header_struct.data_subtype = ((data_pointer[8])& 0xFF)| ((data_pointer[9]<<8)& 0xFF00)| ((data_pointer[10]<<16)& 0xFF0000)| ((data_pointer[11]<<24)& 0xFF000000);
					header_struct.data_sub_subtype = ((data_pointer[12])& 0xFF)| ((data_pointer[13]<<8)& 0xFF00)| ((data_pointer[14]<<16)& 0xFF0000)| ((data_pointer[15]<<24)& 0xFF000000);
					header_struct.length = ((data_pointer[16])& 0xFF)| ((data_pointer[17]<<8)& 0xFF00)| ((data_pointer[18]<<16)& 0xFF0000)| ((data_pointer[19]<<24)& 0xFF000000);
					datalen = header_struct.length;
					if(datalen < (MAX_RXBUFF_SIZE -4)){
						if((data_pointer[datalen +20]==0x5A) && (data_pointer[datalen +21]==0xFF) && (data_pointer[datalen +22]==0xA5)&&(data_pointer[datalen +23]==0xFF))
						{
							memcpy(Rx_DATABUFF,  data_pointer +20, datalen);
							header_struct.NcmDataPtr = Rx_DATABUFF;
							return header_struct;
						}
					}
					else{//第一步
						pos = Clilen -20;
						printf("==sv== ~~~~~~~~~~~~ start %d~~~~~~~~~~~~~~~~~~~\n", datalen);
						memcpy(Rx_DATABUFF,  data_pointer +20, pos);
						printf("==sv== ~~~~~~~~~~~~ end ~~~~~~~~~~~~~~~~~~~\n");
					}
				}
				else//第二步;有第一步之后才有第二步操作
				{
					if((pos -Clilen * count) == (Clilen -20)){//判断数据是否已经有帧头
                                                if(pos <= datalen){//判断所要接收的数据是否已经超出范围
						    memcpy(Rx_DATABUFF +pos, data_pointer, Clilen);
						    pos = pos + Clilen;
						    count ++;
                                                }
					}
				}
			}
		}
		else{
			printf("receive Clilen =%d\n", Clilen);
		}
	}
}

 

展开阅读全文

没有更多推荐了,返回首页