主要问题是串口有时收到乱码有时收到丢包数据:
关于乱码我们其实很容易想到奇偶校验的问题,而我们平常都是默认为没有奇偶校验。
请看程序:
*USART1defaultconfiguration*/
/*USART1configuredasfollow:
-BaudRate=9600baud
-WordLength=8Bits
-OneStopBit
-ParityOdd
-Hardwareflowcontroldesabled
-Receiveandtransmitenabled
-USARTClockdisabled
-USARTCPOL:Clockisactivelow
-USARTCPHA:Dataiscapturedonthesecondedge
-USARTLastBit:Theclockpulseofthelastdatabitisnotoutputto
theSCLKpin
*/
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_Odd;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Clock=USART_Clock_Disable;
USART_InitStructure.USART_CPOL=USART_CPOL_Low;
USART_InitStructure.USART_CPHA=USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit=USART_LastBit_Disable;
只要改成odd或者程序里作修改即可
问题2:既然是虚拟串口,那么利用pc串口软件两边应该可以正常通信,但是源程序出现的情况是。
真--虚ok
虚--真则严重丢数据
看了一下程序原来捣鬼的是这里:
voidUSB_To_USART_Send_Data(u8*data_buffer,u8Nb_bytes)
{
u32i;
for(i=0;i<Nb_bytes;i++)
{
USART_SendData(USART1,*(data_buffer+i));
}
}
串口发送数据后没有等待串口发送完成
改为下边即可
voidUSB_To_USART_Send_Data(u8*data_buffer,u8Nb_bytes)
{
u32i;
for(i=0;i<Nb_bytes;i++)
{
USART_SendData(USART1,*(data_buffer+i));
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}