Exemple #1
0
bool MHZ14::Updata()
{	
	//向模块发送获取数据
	   CO2.SendData(Command_getvalue,9);
	
	//判断是否有数据返回
	  if(CO2.ReceiveBufferSize()<9)
	  {
		  CO2.ClearReceiveBuffer(); //清空接收缓存
		  return false;
	  }
	  else
	  {
		  CO2.GetReceivedData(rev_buffer,9); //取出一帧数据
		   CO2.ClearReceiveBuffer(); //清空接收缓存
		  
		  if(SumCheck(rev_buffer)==false)  //校验和
			  return false;
		  else
		  {
			  DATA_H=rev_buffer[2];
			  DATA_L=rev_buffer[3];
			  CO2_Concentration=rev_buffer[2]*256+rev_buffer[3];  //计算浓度
			  return true;
		  }
	  }
}
Exemple #2
0
//判断所接收的数据帧是否正确
bool isValid(u8 *Data,u16 Data_Length)
{
	if(SumCheck(Data,Data_Length)==(*(Data+Data_Length-1)))
		return true;
	else
		return false;
}
Exemple #3
0
/*******************************************************************************
* Function Name  : xtcp_out
* Description    : 送数据包,填充通道buf后,调用该函数启动发送
* Input          : channel 通道号  busi_len 业务数据长度(预先在业务层填充好)
* Output         : None
* Return         : 
*******************************************************************************/
void xtcp_out(U8 channel,U16 busi_len)
{	xsocket *sck;

	if(channel == 0)
	{	sck = &xsck[0];
	}
	else
	{	sck = NULL;
	}
	
	if(sck)
	{	if( !(sck->flag & SCK_REMIT_FLAG) )	//若之前的包发送成功	
		{	//填充包头
			xys_tcp_hdr *hdr = (xys_tcp_hdr *)&sck->buf;
			hdr->sync[0] = 0x55;
			hdr->sync[1] = 0xaa;
			hdr->port    = 0;
			hdr->flag    = DATA_PK;
			hdr->id      = CM_ID;
			hdr->sn      = sck->sn_s;	
			hdr->len[0]  = busi_len & 0x00ff; 
			hdr->len[1]  = busi_len >> 8;
			hdr->reserve = 0;
			hdr->crc = crc_cal(&xsck[0].buf,sizeof(xys_tcp_hdr)-1);

			//配置套接字信息,以启用发送
			sck->pk_len      = HDR_SIZE + busi_len + 1;	//包长度,用于重传			
			sck->flag       |= SCK_REMIT_FLAG;					
			sck->flag       &= ~SCK_REMIT_TIMEOUT;
			sck->time_out    = PK_REMIT_TIMEOUT;
			sck->remit_count = PK_REMIT_COUNT-1;
			sck->buf[HDR_SIZE + busi_len] = SumCheck(pBUSI,busi_len);
	
			//send_pk(sck->buf); //send packet data
			_log("snd pk:");
			xtcp_outs(sck->buf,sck->pk_len);
#if DEBUG == 1
			printf("id:%d sn:%d busi_len:%d",hdr->id,hdr->sn,hdr->len[0]+hdr->len[1]*256);	
#endif			
		}
		else
		{	_log("KO xtcp: the prev pk is sending, can't send a new pk");
Exemple #4
0
/******************************************
Fun: 发送数据到设备
Input:firstsend:0:重发,1:第一次发送
Output:void
Return:true:数据发送成功 false:失败
******************************************/
_Bool SendData(u8 firstsend)
{
	u8 static i=0;
	u8 static tempbuf[250]={0};
	if(0 != firstsend)
	{
#ifdef OLD	
//		u16 funnum = 0;
		u16 funindex = 0;
	    
//		funnum = dataup.framedata.data[1]<<8 | dataup.framedata.data[2];
		funindex = datadown.framedata.data[3]<<8 | datadown.framedata.data[4];
		
		tempbuf[0] = 0xF5;
		tempbuf[1] = 0xF5;
		tempbuf[2] = 0xF5;
		tempbuf[3] = 0xF5;
		tempbuf[4] = 0;
		tempbuf[5] = 1;
		tempbuf[6] = 0x03;			//cmd
		tempbuf[7] = 11;			//len
		tempbuf[8+0] = 1;			//版本H
		tempbuf[8+1] = 0;			//版本L
        tempbuf[8+2] = 0;			//开关机
		tempbuf[8+6] = 11;			//网络状态

		memcpy(&tempbuf[11], laststate, 4);	//上次的状态
		
		if(11 == funindex)		//查询全部状态
		{
			tempbuf[6] = 0x04;			//cmd
			memset(&tempbuf[10], 0, 9);
		}
		
		else if(31 == funindex)
		{
			if(0 == datadown.framedata.data[5])
			{
				tempbuf[8+2] = 2;	//关机
			}
			else
			{
				tempbuf[8+2] = 1;	//开机
			}
		}
		else if(5005 == funindex)		//自动
		{
			tempbuf[8+3] = datadown.framedata.data[5];
		}
		else if(5022 == funindex)		//加湿
		{
			if(0 == datadown.framedata.data[5])
			{
				tempbuf[8+4] = 0;
			}
			else
			{
				tempbuf[8+4] = 0x10;
			}
		}
		else if(608 == funindex)		//风量
		{
			tempbuf[8+5] = datadown.framedata.data[5];
		}
		else if(5023 == funindex)		//定时
		{
			tempbuf[8+5] = datadown.framedata.data[5];
		}

		u16 tempcrc;
		tempcrc = crc_cal((u8*)&tempbuf, 19);				//从头开始校验
		tempbuf[8+11] = tempcrc >> 8;			//crcH
		tempbuf[8+12] = tempcrc & 0xff;			//crcL
        i = 21;
#else
		u8 j=0;

		i = 0;
		tempbuf[i++] = 0xF5;
		tempbuf[i++] = 0xF5;
		tempbuf[i++] = datadown.framectrl.byte;	//控制字
		tempbuf[i++] = datadown.framedata.datalen;		//长度
		
		for(j=0; j<(datadown.framedata.datalen-2); j++)			//减去校验两个字节
		{
			TransData(tempbuf, &i, datadown.framedata.data[j]);
		}
		
		if(CRCCHECK == datadown.framectrl.bit.crctype)
		{
			union_u16 temp;
			
			temp.val= crc_cal(tempbuf, i);				//从头开始校验
			TransData(tempbuf, &i, temp.u8adr[1]);
			TransData(tempbuf, &i, temp.u8adr[0]);
		}
		else if(SUMCHECK == datadown.framectrl.bit.crctype)
		{
			u8 temp = 0;
			
			temp = SumCheck(&tempbuf[3], (i-3));	//从长度开始校验
			TransData(tempbuf, &i, temp);
			TransData(tempbuf, &i, 0);
		}
		else if(BCCCHECK == datadown.framectrl.bit.crctype)
		{
			u8 temp = 0;
			
			temp= XorCheck(&tempbuf[3], (i-3));	//从长度开始校验
			TransData(tempbuf, &i, temp);
			TransData(tempbuf, &i, 0);
		}
#endif
	}
Exemple #5
0
/******************************************
Fun: 完整帧数据处理
Input: void
Output:void
Return:void
******************************************/
void FrameDataHandle()
{
    DataInterfaceStu tempbuf = {0};
	u8 tempbuffer[250]={0};
	u8 templen = 0;
	u8 i=0, j=0;
	
	templen = framerevfifo.Datalen-framerevpara.offset;
	FifoCopy(tempbuffer, &framerevfifo, framerevpara.offset, templen);
	FifoDeletHead(&framerevfifo, templen);

#ifdef OLD
	u16 tempcrc = 0;
	u8 errortype = 0;

	tempcrc = tempbuffer[templen-2]<<8 | tempbuffer[templen-1];//转换为小端模式
	if(tempcrc != crc_cal(tempbuffer, (templen-2)))		//从帧头开始校验
	{
		errortype = 1;
	}
	else
	{
		memcpy(laststate, &tempbuffer[8+4], 4);
		OldToNew((u8*)&tempbuf, tempbuffer);
	}
#else
	tempbuf.framehead[i] = tempbuffer[i++];			//帧头
	tempbuf.framehead[i] = tempbuffer[i++];			//帧头
	tempbuf.framectrl.byte = tempbuffer[i++];		//控制域 
	tempbuf.framedata.datalen = tempbuffer[i++];	//长度域

	for(j=0; j<tempbuf.framedata.datalen-2; j++,i++)
	{
		tempbuf.framedata.data[j] = tempbuffer[i];
		if(FRAMEHEADER == tempbuffer[i])
		{
			i++;
		}
	}
	
	tempbuf.framedata.data[j] = tempbuffer[i];
	if(FRAMEHEADER == tempbuffer[i])
	{
		tempbuf.framedata.data[j+1] = tempbuffer[i+2];
	}
	else
	{
		tempbuf.framedata.data[j+1] = tempbuffer[i+1];
	}

	u16 tempcrc = 0;
	u8 errortype = 0;

	if(0 != tempbuf.framectrl.bit.encrypt)
	{
		if(OK != Decoding((u8*)&tempbuf.framedata, (templen-3)))//解密长度减去头和控制域
		{
			errortype = 2;
			goto endhandle;
		}
	}
	
	switch(tempbuf.framectrl.bit.crctype)
	{
	case CRCCHECK:
		tempcrc = tempbuf.framedata.data[j]<<8 |tempbuf.framedata.data[j+1];//转换为小端模式
		if(tempcrc != crc_cal(tempbuffer, i))		//从帧头开始校验
		{
			errortype = 1;
		}
		break;
		
	case SUMCHECK:
		if(tempbuf.framedata.data[j] != SumCheck(&tempbuffer[3], (i-3)))		//从长度域开始
		{
			errortype = 1;
		}
		break;
		
	case BCCCHECK:
		if(tempbuf.framedata.data[j] != XorCheck(&tempbuffer[3], (i-3)))//从长度域开始
		{
			errortype = 1;
		}
		break;
		
	default:
		break;
	}
#endif

endhandle:
	if(0 == errortype)
	{
		framerevpara.errortype = 4;
		if(FifoInput(&framerevfifo, (u8*)&tempbuf, (tempbuf.framedata.datalen+4)))	
        {
			framerevpara.offset += tempbuf.framedata.datalen+4;
		}
	}
	else
	{
		framerevpara.errortype = errortype;
	}
	FrameRevInit();
}