unsigned char ReturnAckPack(unsigned char CMDType,unsigned char databuf1,unsigned char databuf2,unsigned char databuf3)
{
	uint8_t Ackbuf[8]={0};

	if(lsSn != LsSn)
	{
		LsSn = lsSn;
		Ackbuf[0] = 0xEB;
		Ackbuf[1] = lsSn;
		Ackbuf[2] = 0x08;
		Ackbuf[3] = CMDType;
		Ackbuf[4] = databuf1;
		Ackbuf[5] = databuf2;
		Ackbuf[6] = databuf3;
		Ackbuf[7] = XorCheck((unsigned char *)Ackbuf,(Ackbuf[2]-1));
		Uart0PutStr(Ackbuf,Ackbuf[2]);
		if((CMDType == 0xa1)&&(LstCmd != 0xc2))
			memcpy(LsMoneyAckbuf,Ackbuf,8);
		else
		if((CMDType == 0xa1)&&(LstCmd == 0xc2))
			memcpy(LsStateAckbuf,Ackbuf,8);
		else
		if(CMDType == 0xa4)
			memcpy(LsOutCoinAckbuf,Ackbuf,8);
	}
	else
	{
		if((CMDType == 0xa1)&&(LstCmd != 0xc2))
			Uart0PutStr(LsMoneyAckbuf,8);	
		else
		if((CMDType == 0xa1)&&(LstCmd == 0xc2))
			Uart0PutStr(LsStateAckbuf,8);
		else
		if(CMDType == 0xa4)
			Uart0PutStr(LsOutCoinAckbuf,8);
	}
	return 0;
}
unsigned char IsCmdRecv(unsigned char *CMDType,unsigned char *SubCmd)
{
	uint8_t Recvdata = 0;
	uint8_t CmdIndex=0;

	Recvdata = Uart0GetCh();
	if(Recvdata == 0xEA)
	{
		Uart0RecvCmdBuff[CmdIndex++] = Recvdata;
		RecvCmdTimer = 20;
		while(RecvCmdTimer)
		{
			if(Uart0BuffIsNotEmpty()==1)
			{
				Uart0RecvCmdBuff[CmdIndex++] = Uart0GetCh();
				if(Uart0RecvCmdBuff[2]>8)
					break;
				if(CmdIndex == Uart0RecvCmdBuff[2])
				{
					if(Uart0RecvCmdBuff[CmdIndex-1] == XorCheck((unsigned char *)Uart0RecvCmdBuff,(Uart0RecvCmdBuff[2]-1)))
					{
						lsSn = Uart0RecvCmdBuff[1];
						*CMDType = Uart0RecvCmdBuff[3];
						LstCmd = Uart0RecvCmdBuff[5];
						SubCmd[0] = Uart0RecvCmdBuff[4];
						SubCmd[1] = Uart0RecvCmdBuff[5];
						SubCmd[2] = Uart0RecvCmdBuff[6];
						//Trace("OK...%02x %02x %02x\r\n",Uart0RecvCmdBuff[1],Uart0RecvCmdBuff[7],XorCheck((unsigned char *)Uart0RecvCmdBuff,(Uart0RecvCmdBuff[2]-1)));
						return 1;
					}
					else
						break;	
				}
			}	
		}		
	}
	return 0;
}
예제 #3
0
파일: devproanaly.c 프로젝트: boyayun/soft
/******************************************
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
	}
예제 #4
0
파일: devproanaly.c 프로젝트: boyayun/soft
/******************************************
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();
}
예제 #5
0
/*********************************************************************************************************
** Function name:     	HP_send
** Descriptions:	    EVB通讯
** input parameters:    Chl:选择通道;Head:包头;Sn:序列号;Type:消息类别;Addr:设备地址;Data:数据;
** output parameters:   *ACK:应答包
** Returned value:      1:收到应答;0:未收到应答即通讯失败
*********************************************************************************************************/
uint8 HP_send(uint8 addr,uint8 cmd,uint16 data)
{
	uint8 index = 0,i,ch,len,crc;
	memset((void *)recvbuf,0,sizeof(recvbuf));
	
	sn[addr] = (cmd == HP_PAYOUT) ? sn[addr] + 1: sn[addr]; //更新sn
	
	sendbuf[index++] = 0xED;
	sendbuf[index++] = 0x08; //len
	sendbuf[index++] = sn[addr];
	sendbuf[index++] = cmd;
	sendbuf[index++] = addr;
	sendbuf[index++] = LUINT16(data);
	sendbuf[index++] = HUINT16(data);
	sendbuf[index++] = XorCheck((uint8 *)sendbuf,7);
	
	uart3_clr_buf();
	//发送数据禁止切换任务
	OSSchedLock();
	Uart3PutStr((uint8 *)sendbuf,8);
	OSSchedUnlock();
	
	#ifdef DEBUG_HOPPER
	print_hopper("HP-SEND[%d]:",index);
	for(i = 0;i < index;i++){
		print_hopper("%02x ",sendbuf[i]);
	}
	print_hopper("\r\n");
	#endif
	
	index = 0;
	Timer.hopper_recv_timeout = 100;
	while(Timer.hopper_recv_timeout){ //1000ms内收到ACK,否则超时
		if(Uart3BuffIsNotEmpty() == 1){
			ch = Uart3GetCh();
			if(index == 0){
				if(ch == 0xFD){
					recvbuf[index++] = ch;
				}
			}
			else if(index == 1){
				len = ch;
				recvbuf[index++] = ch;
			}
			else if(index >= (len - 1)){
				recvbuf[index++] = ch;
				crc = XorCheck((uint8 *)recvbuf,len - 1);
				if(crc == ch){
#ifdef DEBUG_HOPPER
					print_hopper("HP-RECV[%d]:",recvbuf[1]);
					for(i = 0;i < recvbuf[1];i++){
						print_hopper("%02x ",recvbuf[i]);
					}
					print_hopper("\r\n");
#endif
					return 1;
				}
			}
			else{
				recvbuf[index++] = ch;
			}
		}
		else{
			msleep(20);
		}
	}
	return 0;
}
예제 #6
0
void Uart0IsrHandler (void)
{
	unsigned char i,ch,crc;
	OSIntEnter();   
	read_in = 0;	
    while((U0IIR & 0x01) == 0) {                         			//判断是否有中断挂起
        switch(U0IIR & 0x0E){ 										//判断中断标志                                     
            case 0x04 : for(i = 0;i < 8;i++){
							read_buf[read_in++] = U0RBR;
						}
					break;
            case 0x0C : while((U0LSR & 0x01) == 0x01){                         
		                	read_buf[read_in++] = U0RBR;
		                }
		                break;
            default	: break;
        }
    }
	
	if(uart0_req == 0){
		for(i = 0;i < read_in;i++){
			ch = read_buf[i];
			if(uart0_in == 0){
				if(ch == HEAD_PC){
					uart0_buf[uart0_in++] = ch;
					Timer.uart0_timeout = 50;
				}
			}
			else if(uart0_in == 1){ 
				uart0_len = ch;
				if(uart0_len >= UART0_BUF_SIZE){
					uart0_in = 0;
				}
				else{
					uart0_buf[uart0_in++] = ch;
				}
			}
			else if(uart0_in >= uart0_len){ 
				uart0_buf[uart0_in++] = ch; 
				crc = XorCheck((unsigned char *)uart0_buf,uart0_len);
				if(crc == ch){
					uart0_req = 1;
				}
				else{
					uart0_req = 0;
				}
				uart0_len = uart0_in;
				uart0_in = 0;
			}
			else{
				uart0_buf[uart0_in++] = ch;
			}
			
			if(uart0_in > 0 && Timer.uart0_timeout == 0){
				uart0_in = 0;
				//uart0_req = 0;
			}
		}
	}
	
    OSIntExit();

}