Пример #1
0
/*******************************************************************************
* Function Name  : at
* Description    : at +link= ;at  ;at +name
* Input          : none
* Output         : None
* Return         : none
*******************************************************************************/
void at(char *ptr, int timeout)
{   BYTE rcv_ch[30];
    char temp;
    char i;

    Usart_Config(bt_uart,38400);
    BT_ENTER_ATMODE();
    delay_ms(50);

    buff_out(&xtcp_buf,0,buff_dlen(&xtcp_buf));//clear buffer
    memset(rcv_ch,0,sizeof(rcv_ch));
    xtcp_outs("at",2);
    if(ptr)
    {
        xtcp_outs(ptr,strlen(ptr));
    }
    xtcp_outs("\r\n",2);
    delay_ms(timeout);
    temp = buff_dlen(&xtcp_buf);
    buff_out(&xtcp_buf,rcv_ch,temp);
    for(i=0; i<temp; i++)
    {   printf("%c",rcv_ch[i]);
    }

    BT_EXIT_ATMODE();
    Usart_Config(bt_uart,115200);
    delay_ms(100);
}
Пример #2
0
/*******************************************************************************
* Function Name  : bt_disc
* Description    : bt_disc
* Input          : none
* Output         : None
* Return         : none
*******************************************************************************/
void bt_disc(void)
{
    BYTE rcv_ch[30];
    char addr[]="at+disc\r";
    char temp;
    char i;

    Usart_Config(bt_uart,38400);
    BT_ENTER_ATMODE();
    delay_ms(50);

    buff_out(&xtcp_buf,0,buff_dlen(&xtcp_buf));//clear buffer
    memset(rcv_ch,0,sizeof(rcv_ch));
    xtcp_outs(addr,strlen(addr));
    delay_ms(200);
    temp = buff_dlen(&xtcp_buf);
    buff_out(&xtcp_buf,rcv_ch,temp);
    for(i=0; i<temp; i++)
    {   printf("%c",rcv_ch[i]);
    }
    BT_EXIT_ATMODE();
    Usart_Config(bt_uart,115200);
}
Пример #3
0
/* Remove data from buffer, return number of bytes
** If data pointer is null, adjust pointers but don't transfer data */
WORD buff_out(CBUFF *bp, BYTE *data, WORD maxlen)
{	
	WORD out, n, n1, n2;
	out = (WORD)bp->out & (bp->len-1);			/* Mask len to buffer area */
	n  = minw(maxlen, buff_dlen(bp));			/* Get max allowable length */
	n1 = minw(n, (WORD)(bp->len - out));		/* Length up to end of buff */
	n2 = n - n1;								/* Length from start of buff */
	if(n1 && data)								/* If anything to copy.. */
		memcpy(data, &bp->data[out], n1);		/* ..copy up to end of buffer.. */
	if(n2 && data)								/* ..and maybe also.. */
		memcpy(&data[n1], bp->data, n2);		/* ..copy from start of buffer */
	bp->out +=n;								/* Bump O/P pointer */
	if(buff_untriedlen(bp))						/* ..and maybe trial pointer */
		bp->trial = bp->out;
	return(n);
}
Пример #4
0
/* Return length of free space in buffer */
WORD buff_freelen(CBUFF *bp)
{	return (bp->len ? bp->len-1 - buff_dlen(bp) : 0);
}
Пример #5
0
/*******************************************************************************
* Function Name  : pk_poll
* Description    : 提取串口接收队列数据,并进行组包
                    若受到0x55 0xaa,而后面包头校验错误时,只丢掉0x55 0xaa,而不是将整包丢掉
                    若包头校验正确,而业务数据错误,则将全部丢掉
* Input          : none
* Output         : None
* Return         : 返回包长   收到一包数据
                    0          没有收满一包数据
*******************************************************************************/
static U16 pk_poll(void)
{	static U16 bus_len  = 0;//业务数据长度,需要静态
	static U8  state    = 0;//组包状态,(分成四个状态 0 1 2 3->收到SYN1,收到SYN2,包头数据处理,业务数据处理)需要静态
	
	U8  rcv_char = 0;
	U16 pk_len   = 0;

	if(!buff_dlen(&xtcp_buf))		 //buff无数据,直接返回
		return 0;

	switch (state){
		case 0://检测同步字段SYN1,若检测到,则转向state 1
			buff_out(&xtcp_buf, &rcv_char, 1);
			if( rcv_char == SYN1 )
			{	state = 1;
			}
			break;
		case 1://检测同步字段SYN2,若检测到,则转向state 2
			buff_out(&xtcp_buf, &rcv_char, 1);
			if( (rcv_char == SYN2) )
			{	state = 2;
				pk_buf.hdr.sync[0] = 0x55;
				pk_buf.hdr.sync[1] = 0xAA;
				pk_time_out   = PK_RCV_TIMEOUT;
			}		
			break;
		case 2://找到同步字段,接收包头,若包头正确,且有业务数据,转向state3
			if( buff_dlen(&xtcp_buf) >= (sizeof(xys_tcp_hdr)-2) )//收到至少一个包头,采用数据块出队,增加效率
			{	buff_try( &xtcp_buf, ((U8 *)(&pk_buf))+2, sizeof(xys_tcp_hdr)-2 );//包头的55AA已经处理
				if( pk_buf.hdr.crc == crc_cal(&pk_buf, HDR_SIZE-1) )	//包头CRC校验
	//			if(1)		//不管包头CRC校验
				{
					//包头采用buff_try出队,若包头校验错误,只丢掉包头的0x55 0xaa,程序将继续从0x55 aa后开始寻找
				    //包头同步字段
					buff_out( &xtcp_buf, (BYTE *)0, sizeof(xys_tcp_hdr)-2 ); //若包头校验正确 采用buff_out出队

					bus_len = pk_buf.hdr.len[0] + pk_buf.hdr.len[1] * 256;//1字节的校验
					if(bus_len > 0)
					{	if(bus_len <= OP_CODE_LEN)
						{	state = 3;			//有业务数据,数据两小于定义的最大长度 转向state3接收业务数据
						
						}
						else
						{	state = 0;}//有数据,但数据长度超过定义的程度,丢弃该包
					}
					else
					{	state  = 0;	//包结束,转向state0
						pk_len = sizeof(xys_tcp_hdr);	//无业务数据,返回包头长度(该包可能是ACK包)
					}
				}
				else
				{	state = 0;//若校验错误,丢弃该包,转向state0
				}
				
			}
			break;
		case 3://接收业务数据,若业务数据校验错误,丢弃该包,并转向state0
			if( buff_dlen(&xtcp_buf) >= bus_len )
			{	buff_out( &xtcp_buf, ((U8 *)(&pk_buf))+HDR_SIZE, bus_len );
//				if( pk_buf.business[bus_len-1] == SumCheck((U8 *)&pk_buf.business, bus_len-1) )	 //业务数据和校验
				if(1)	//不进行业务数据和校验
				{	pk_len = bus_len + sizeof(xys_tcp_hdr);
				} 
				state = 0;
			}
			break;
		default:
			break;
	}
	return pk_len;
}