unsigned int USART1_Rx(unsigned char *buffers, unsigned int length)
{	
	int size = USART1_RxAvailable();
	int first_read;	
	SUSART_Buffers_RX *ptr=&RXBuffers;	
	
	if(!size)
		return 0;
		
	if(length >= size)
		length = size;
	
	if((size+ptr->PtrCons) >= NUM_RX_BUFFERS)
	{
		//buffer wrap around
		first_read = NUM_RX_BUFFERS - ptr->PtrCons;
		//copy from current point to end of buffer
		memcpy(buffers, &ptr->Buffers[ptr->PtrCons], first_read);
		//continue data copy from buffer starting
		memcpy(&buffers[first_read], &ptr->Buffers[0], length-first_read);
		ptr->PtrCons = length-first_read;
	}
	else
	{		
		memcpy(buffers, &ptr->Buffers[ptr->PtrCons], size);
		ptr->PtrCons += size;
	}
		
	//reading read length
	return length;
}
Beispiel #2
0
/*******************************************************************************
* 函数名:主函数
* 功  能:	
* 参  数:
* 返  回:	
*******************************************************************************/
int main(void)
{	
	SystemInit();
	Delay_init(72);
	NVIC_Configuration();
	USART1_Init(9600);
	while(1)
	{	
		//Delayms(100);
		//UART1_PrintChar(0x55);
		//UART1_PrintBuff(buf, 6);
		//UART1_PrintString("string...");
		len = USART1_RxAvailable();
		if(  len > 0 ) 
		{
			tx_len = USART1_ReadRxBUF(buf, len);
			USART1_PrintBuff(buf, tx_len);
		}
	}
}