STM32
直播中

敷衍作笑谈

9年用户 908经验值
擅长:制造/封装 连接器 光电显示 接口/总线/驱动 RF/无线
私信 关注
[问答]

虚拟串口与STM32通信遇到的疑问求解

我想要一个虚拟串口与STM32通信
在其中,我需要从串口获取输入命令,然后实现命令想要做的事情。

我在网上看到很多关于它的例子。 然而,它们都在“CDC_Receive_FS”中获得消息并立即重新发送到PC。 这意味着消息没有出去。 例如,离开“usbd_cdc_if.c”并在“main.c”中发送给PC。

如何在“CDC_Receive_FS”外部收到消息?

如何避免丢失消息?

usbd_cdc_if.c

  • /**
  •   * @brief  Data received over USB OUT endpoint are sent over CDC interface
  •   *         through this function.
  •   *
  •   *         @note
  •   *         This function will block any OUT packet reception on USB endpoint
  •   *         untill exiting this function. If you exit this function before transfer
  •   *         is complete on CDC interface (ie. using DMA controller) it will result
  •   *         in receiving more data while previous ones are still not sent.
  •   *
  •   * @param  Buf: Buffer of data to be received
  •   * @param  Len: Number of data received (in bytes)
  •   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  •   */
  • static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
  • {
  •   /* USER CODE BEGIN 6 */
  •   USBD_CDC_SetRxBuffer( hUsbDeviceFS,  Buf[0]);
  •   USBD_CDC_ReceivePacket( hUsbDeviceFS);

  •   USBD_CDC_SetTxBuffer( hUsbDeviceFS, Buf, Len);
  •   USBD_CDC_TransmitPacket( hUsbDeviceFS);

  •   return (USBD_OK);
  •   /* USER CODE END 6 */
  • }

如何在main.c中使用“UserRxBufferFS”和“UserTxBufferFS”?

  • /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
  •   * @brief Private variables.
  •   * @{
  •   */
  • /* Create buffer for reception and transmission           */
  • /* It's up to user to redefine and/or remove those define */
  • /** Received data over USB are stored in this buffer      */
  • uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];

  • /** Data to send over USB CDC are stored in this buffer   */
  • uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

[/td]
以下内容为评论
[td]我正在使用Blue Pill板。

我在下面找到了例子。它教我成功在外面使用“UserRxBufferFS”和“* Len”。


  • /**
  •   ******************************************************************************
  •   * @file           : main.c
  •   * @brief          : Main program body
  •   ******************************************************************************
  •   * This notice applies to any and all portions of this file
  •   * that are not between comment pairs USER CODE BEGIN and
  •   * USER CODE END. Other portions of this file, whether
  •   * inserted by the user or by software development tools
  •   * are owned by their respective copyright owners.
  •   *
  •   * Copyright (c) 2018 STMicroelectronics International N.V.
  •   * All rights reserved.
  •   *
  •   * Redistribution and use in source and binary forms, with or without
  •   * modification, are permitted, provided that the following conditions are met:
  •   *
  •   * 1. Redistribution of source code must retain the above copyright notice,
  •   *    this list of conditions and the following disclaimer.
  •   * 2. Redistributions in binary form must reproduce the above copyright notice,
  •   *    this list of conditions and the following disclaimer in the documentation
  •   *    and/or other materials provided with the distribution.
  •   * 3. Neither the name of STMicroelectronics nor the names of other
  •   *    contributors to this software may be used to endorse or promote products
  •   *    derived from this software without specific written permission.
  •   * 4. This software, including modifications and/or derivative works of this
  •   *    software, must execute solely and exclusively on microcontroller or
  •   *    microprocessor devices manufactured by or for STMicroelectronics.
  •   * 5. Redistribution and use of this software other than as permitted under
  •   *    this license is void and will automatically terminate your rights under
  •   *    this license.
  •   *
  •   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  •   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  •   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  •   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  •   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  •   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  •   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  •   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  •   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  •   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  •   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  •   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  •   *
  •   ******************************************************************************
  •   */
  • /* Includes ------------------------------------------------------------------*/
  • #include "main.h"
  • #include "stm32f1xx_hal.h"
  • #include "usart.h"
  • #include "usb_device.h"
  • #include "gpio.h"

  • /* USER CODE BEGIN Includes */

  • #include "usb_device.h"
  • #include "usbd_core.h"
  • #include "usbd_desc.h"
  • #include "usbd_cdc.h"
  • #include "usbd_cdc_if.h"
  • /* USER CODE END Includes */

  • /* Private variables ---------------------------------------------------------*/

  • /* USER CODE BEGIN PV */
  • /* Private variables ---------------------------------------------------------*/
  • // http://www.smilefrog.net/?p=763  "USB-CDC (VCP)“
  • extern uint8_t UserRxBufferFS[];  // declare receive data external variable
  • extern uint8_t DataLen;  // declare receive buffer external variable
  • /* USER CODE END PV */

  • /* Private function prototypes -----------------------------------------------*/
  • void SystemClock_Config(void);

  • /* USER CODE BEGIN PFP */
  • /* Private function prototypes -----------------------------------------------*/

  • /* USER CODE END PFP */

  • /* USER CODE BEGIN 0 */

  • /* USER CODE END 0 */

  • /**
  •   * @brief  The application entry point.
  •   *
  •   * @retval None
  •   */
  • int main(void)
  • {
  •   /* USER CODE BEGIN 1 */

  •   /* USER CODE END 1 */

  •   /* MCU Configuration----------------------------------------------------------*/

  •   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  •   HAL_Init();

  •   /* USER CODE BEGIN Init */

  •   /* USER CODE END Init */

  •   /* Configure the system clock */
  •   SystemClock_Config();

  •   /* USER CODE BEGIN SysInit */

  •   /* USER CODE END SysInit */

  •   /* Initialize all configured peripherals */
  •   MX_GPIO_Init();
  •   MX_USART1_UART_Init();
  •   MX_USB_DEVICE_Init();
  •   /* USER CODE BEGIN 2 */

  •   /* USER CODE END 2 */

  •   /* Infinite loop */
  •   /* USER CODE BEGIN WHILE */
  •   while (1)
  •   {

  •   /* USER CODE END WHILE */

  •   /* USER CODE BEGIN 3 */
  •           /* to ensure the code is running */
  •           //HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_12);
  •           //HAL_Delay(1000);

  •           if(DataLen > 0)
  •           {
  •                   while(CDC_Transmit_FS(UserRxBufferFS, DataLen));
  •                   DataLen = 0;
  •           }
  •   }
  •   /* USER CODE END 3 */

  • }

  • /**
  •   * @brief System Clock Configuration
  •   * @retval None
  •   */
  • void SystemClock_Config(void)
  • {

  •   RCC_OscInitTypeDef RCC_OscInitStruct;
  •   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  •   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  •     /**Initializes the CPU, AHB and APB busses clocks
  •     */
  •   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  •   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  •   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  •   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  •   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  •   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  •   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  •   if (HAL_RCC_OscConfig( RCC_OscInitStruct) != HAL_OK)
  •   {
  •     _Error_Handler(__FILE__, __LINE__);
  •   }

  •     /**Initializes the CPU, AHB and APB busses clocks
  •     */
  •   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  •                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  •   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  •   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  •   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  •   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  •   if (HAL_RCC_ClockConfig( RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  •   {
  •     _Error_Handler(__FILE__, __LINE__);
  •   }

  •   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  •   PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  •   if (HAL_RCCEx_PeriphCLKConfig( PeriphClkInit) != HAL_OK)
  •   {
  •     _Error_Handler(__FILE__, __LINE__);
  •   }

  •     /**Configure the Systick interrupt time
  •     */
  •   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  •     /**Configure the Systick
  •     */
  •   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  •   /* SysTick_IRQn interrupt configuration */
  •   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  • }

  • /* USER CODE BEGIN 4 */

  • /* USER CODE END 4 */

  • /**
  •   * @brief  This function is executed in case of error occurrence.
  •   * @param  file: The file name as string.
  •   * @param  line: The line in file as a number.
  •   * @retval None
  •   */
  • void _Error_Handler(char *file, int line)
  • {
  •   /* USER CODE BEGIN Error_Handler_Debug */
  •   /* User can add his own implementation to report the HAL error return state */
  •   while(1)
  •   {
  •   }
  •   /* USER CODE END Error_Handler_Debug */
  • }

  • #ifdef  USE_FULL_ASSERT
  • /**
  •   * @brief  Reports the name of the source file and the source line number
  •   *         where the assert_param error has occurred.
  •   * @param  file: pointer to the source file name
  •   * @param  line: assert_param error line source number
  •   * @retval None
  •   */
  • void assert_failed(uint8_t* file, uint32_t line)
  • {
  •   /* USER CODE BEGIN 6 */
  •   /* User can add his own implementation to report the file name and line number,
  •      tex: printf("Wrong parameters value: file %s on line %drn", file, line) */
  •   /* USER CODE END 6 */
  • }
  • #endif /* USE_FULL_ASSERT */

  • /**
  •   * @}
  •   */

  • /**
  •   * @}
  •   */

  • /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码

  • /**
  •   ******************************************************************************
  •   * @file           : usbd_cdc_if.c///
  •   * @version        : v2.0_Cube
  •   * @brief          : Usb device for Virtual Com Port.
  •   ******************************************************************************
  •   * This notice applies to any and all portions of this file
  •   * that are not between comment pairs USER CODE BEGIN and
  •   * USER CODE END. Other portions of this file, whether
  •   * inserted by the user or by software development tools
  •   * are owned by their respective copyright owners.
  •   *
  •   * Copyright (c) 2018 STMicroelectronics International N.V.
  •   * All rights reserved.
  •   *
  •   * Redistribution and use in source and binary forms, with or without
  •   * modification, are permitted, provided that the following conditions are met:
  •   *
  •   * 1. Redistribution of source code must retain the above copyright notice,
  •   *    this list of conditions and the following disclaimer.
  •   * 2. Redistributions in binary form must reproduce the above copyright notice,
  •   *    this list of conditions and the following disclaimer in the documentation
  •   *    and/or other materials provided with the distribution.
  •   * 3. Neither the name of STMicroelectronics nor the names of other
  •   *    contributors to this software may be used to endorse or promote products
  •   *    derived from this software without specific written permission.
  •   * 4. This software, including modifications and/or derivative works of this
  •   *    software, must execute solely and exclusively on microcontroller or
  •   *    microprocessor devices manufactured by or for STMicroelectronics.
  •   * 5. Redistribution and use of this software other than as permitted under
  •   *    this license is void and will automatically terminate your rights under
  •   *    this license.
  •   *
  •   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  •   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  •   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  •   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  •   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  •   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  •   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  •   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  •   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  •   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  •   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  •   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  •   *
  •   ******************************************************************************
  •   */

  • /* Includes ------------------------------------------------------------------*/
  • #include "usbd_cdc_if.h"

  • /* USER CODE BEGIN INCLUDE */

  • /* USER CODE END INCLUDE */

  • /* Private typedef -----------------------------------------------------------*/
  • /* Private define ------------------------------------------------------------*/
  • /* Private macro -------------------------------------------------------------*/

  • /* USER CODE BEGIN PV */
  • /* Private variables ---------------------------------------------------------*/

  • /* USER CODE END PV */

  • /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  •   * @brief Usb device library.
  •   * @{
  •   */

  • /** @addtogroup USBD_CDC_IF
  •   * @{
  •   */

  • /** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions
  •   * @brief Private types.
  •   * @{
  •   */

  • /* USER CODE BEGIN PRIVATE_TYPES */

  • /* USER CODE END PRIVATE_TYPES */

  • /**
  •   * @}
  •   */

  • /** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines
  •   * @brief Private defines.
  •   * @{
  •   */

  • /* USER CODE BEGIN PRIVATE_DEFINES */
  • /* Define size for the receive and transmit buffer over CDC */
  • /* It's up to user to redefine and/or remove those define */
  • #define APP_RX_DATA_SIZE  64
  • #define APP_TX_DATA_SIZE  64
  • /* USER CODE END PRIVATE_DEFINES */

  • /**
  •   * @}
  •   */

  • /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
  •   * @brief Private macros.
  •   * @{
  •   */

  • /* USER CODE BEGIN PRIVATE_MACRO */

  • /* USER CODE END PRIVATE_MACRO */

  • /**
  •   * @}
  •   */

  • /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
  •   * @brief Private variables.
  •   * @{
  •   */
  • /* Create buffer for reception and transmission           */
  • /* It's up to user to redefine and/or remove those define */
  • /** Received data over USB are stored in this buffer      */
  • uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];

  • /** Data to send over USB CDC are stored in this buffer   */
  • uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

  • /* USER CODE BEGIN PRIVATE_VARIABLES */
  • // http://www.smilefrog.net/?p=763  "USB-CDC (VCP)“
  • uint8_t DataLen; // define receive buffer length variable
  • /* USER CODE END PRIVATE_VARIABLES */

  • /**
  •   * @}
  •   */

  • /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
  •   * @brief Public variables.
  •   * @{
  •   */

  • extern USBD_HandleTypeDef hUsbDeviceFS;

  • /* USER CODE BEGIN EXPORTED_VARIABLES */

  • /* USER CODE END EXPORTED_VARIABLES */

  • /**
  •   * @}
  •   */

  • /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
  •   * @brief Private functions declaration.
  •   * @{
  •   */

  • static int8_t CDC_Init_FS(void);
  • static int8_t CDC_DeInit_FS(void);
  • static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
  • static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);

  • /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */

  • /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */

  • /**
  •   * @}
  •   */

  • USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
  • {
  •   CDC_Init_FS,
  •   CDC_DeInit_FS,
  •   CDC_Control_FS,
  •   CDC_Receive_FS
  • };

  • /* Private functions ---------------------------------------------------------*/
  • /**
  •   * @brief  Initializes the CDC media low layer over the FS USB IP
  •   * @retval USBD_OK if all operations are OK else USBD_FAIL
  •   */
  • static int8_t CDC_Init_FS(void)
  • {
  •   /* USER CODE BEGIN 3 */
  •   /* Set Application Buffers */
  •   USBD_CDC_SetTxBuffer( hUsbDeviceFS, UserTxBufferFS, 0);
  •   USBD_CDC_SetRxBuffer( hUsbDeviceFS, UserRxBufferFS);
  •   return (USBD_OK);
  •   /* USER CODE END 3 */
  • }

  • /**
  •   * @brief  DeInitializes the CDC media low layer
  •   * @retval USBD_OK if all operations are OK else USBD_FAIL
  •   */
  • static int8_t CDC_DeInit_FS(void)
  • {
  •   /* USER CODE BEGIN 4 */
  •   return (USBD_OK);
  •   /* USER CODE END 4 */
  • }

  • /**
  •   * @brief  Manage the CDC class requests
  •   * @param  cmd: Command code
  •   * @param  pbuf: Buffer containing command data (request parameters)
  •   * @param  length: Number of data to be sent (in bytes)
  •   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  •   */
  • static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
  • {
  •   /* USER CODE BEGIN 5 */
  •   switch(cmd)
  •   {
  •     case CDC_SEND_ENCAPSULATED_COMMAND:

  •     break;

  •     case CDC_GET_ENCAPSULATED_RESPONSE:

  •     break;

  •     case CDC_SET_COMM_FEATURE:

  •     break;

  •     case CDC_GET_COMM_FEATURE:

  •     break;

  •     case CDC_CLEAR_COMM_FEATURE:

  •     break;

  •   /*******************************************************************************/
  •   /* Line Coding Structure                                                       */
  •   /*-----------------------------------------------------------------------------*/
  •   /* Offset | Field       | Size | Value  | Description                          */
  •   /* 0      | dwDTERate   |   4  | Number |Data terminal rate, in bits per second*/
  •   /* 4      | bCharFormat |   1  | Number | Stop bits                            */
  •   /*                                        0 - 1 Stop bit                       */
  •   /*                                        1 - 1.5 Stop bits                    */
  •   /*                                        2 - 2 Stop bits                      */
  •   /* 5      | bParityType |  1   | Number | Parity                               */
  •   /*                                        0 - None                             */
  •   /*                                        1 - Odd                              */
  •   /*                                        2 - Even                             */
  •   /*                                        3 - Mark                             */
  •   /*                                        4 - Space                            */
  •   /* 6      | bDataBits  |   1   | Number Data bits (5, 6, 7, 8 or 16).          */
  •   /*******************************************************************************/
  •     case CDC_SET_LINE_CODING:

  •     break;

  •     case CDC_GET_LINE_CODING:

  •     break;

  •     case CDC_SET_CONTROL_LINE_STATE:

  •     break;

  •     case CDC_SEND_BREAK:

  •     break;

  •   default:
  •     break;
  •   }

  •   return (USBD_OK);
  •   /* USER CODE END 5 */
  • }

  • /**
  •   * @brief  Data received over USB OUT endpoint are sent over CDC interface
  •   *         through this function.
  •   *
  •   *         @note
  •   *         This function will block any OUT packet reception on USB endpoint
  •   *         untill exiting this function. If you exit this function before transfer
  •   *         is complete on CDC interface (ie. using DMA controller) it will result
  •   *         in receiving more data while previous ones are still not sent.
  •   *
  •   * @param  Buf: Buffer of data to be received
  •   * @param  Len: Number of data received (in bytes)
  •   * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  •   */
  • static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
  • {
  •   /* USER CODE BEGIN 6 */
  •   USBD_CDC_SetRxBuffer( hUsbDeviceFS,  Buf[0]);
  •   USBD_CDC_ReceivePacket( hUsbDeviceFS);

  •   DataLen = *Len; //assign receive buffer length, don't edit any other!!!

  •   return (USBD_OK);
  •   /* USER CODE END 6 */
  • }

  • /**
  •   * @brief  CDC_Transmit_FS
  •   *         Data to send over USB IN endpoint are sent over CDC interface
  •   *         through this function.
  •   *         @note
  •   *
  •   *
  •   * @param  Buf: Buffer of data to be sent
  •   * @param  Len: Number of data to be sent (in bytes)
  •   * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  •   */
  • uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
  • {
  •   uint8_t result = USBD_OK;
  •   /* USER CODE BEGIN 7 */
  •   USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  •   if (hcdc->TxState != 0){
  •     return USBD_BUSY;
  •   }
  •   USBD_CDC_SetTxBuffer( hUsbDeviceFS, Buf, Len);
  •   result = USBD_CDC_TransmitPacket( hUsbDeviceFS);
  •   /* USER CODE END 7 */
  •   return result;
  • }

  • /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */

  • /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */

  • /**
  •   * @}
  •   */

  • /**
  •   * @}
  •   */

  • /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码

问题1。
为什么使用“while(CDC_Transmit_FS(UserRxBufferFS,DataLen));”?
我尝试只使用“CDC_Transmit_FS(UserRxBufferFS,DataLen)”,它似乎也有用。
它与下一步“DataLen = 0”(重置DataLen)有关吗?

问题2。
只有当我在主循环中添加一些延迟(以模拟另一个任务)时,它会丢失消息或只是回转最后一个字。
如何避免丢失?

请指教!

回帖(1)

王强

2024-4-24 15:05:04
CDC_Transmit_FS(UserRxBufferFS,DataLen);一次不一定能够发送成功。因为你发送的时候USB可能处于BUSY等状态。
所以采用while(CDC_Transmit_FS(UserRxBufferFS,DataLen));不断发送,直到发送成功。
DataLen = 0;原作者借用了这个变量来判断已接收到数据---- if(DataLen > 0)----,此处把它设置为0。但不建议这样做。这存在竞争风险。你的第2个问题有可能和这也相关。
举报

更多回帖

×
20
完善资料,
赚取积分