近几天 用微芯力科的板子 调试usb虚拟串口的程序,发现一些问题以及解决方法。和大家共享

来源:互联网 发布:手谈汉化组 知乎 编辑:程序博客网 时间:2024/06/10 07:30

近几天 用微芯力科的板子 调试usb虚拟串口的程序,发现一些问题以及解决方法。和大家共享
主要问题是  串口有时收到乱码 有时收到丢包数据:
  关于 乱码 我们其实很容易想到 奇偶校验的问题,而我们平常都是默认为没有奇偶校验。
    请看程序:
         * USART1 default configuration */
  /* USART1 configured as follow:
        - BaudRate = 9600 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - Parity Odd
        - Hardware flow control desabled
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the second edge 
        - USART LastBit: The clock pulse of the last data bit is not output to 
                         the SCLK pin
  */
  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 
             虚--真  则严重丢数据

  看了一下程序  原来捣鬼的是这里:
      void USB_To_USART_Send_Data(u8* data_buffer, u8 Nb_bytes)
{
  u32 i;

  for (i = 0; i < Nb_bytes; i++)
  {
    USART_SendData(USART1, *(data_buffer + i));
       }
}
串口发送数据后 没有等待串口发送完成

改为下边即可
   void USB_To_USART_Send_Data(u8* data_buffer, u8 Nb_bytes)
{
  u32 i;

  for (i = 0; i < Nb_bytes; i++)
  {
    USART_SendData(USART1, *(data_buffer + i));
      while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
   
  }
}
 
 
源代码 有需要的 请到 在路上 论坛下载  http://www.stmsky.com/bbs/