打印
[应用相关]

STM32 USB Virtual_Port_Com虚拟串口通信

[复制链接]
122|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
1.配置STM32CubeMX



默认配置



选择虚拟串口通信



设置堆栈大小

2.修改代码
打开usbd_cdc_if.h 添加如下代码

#define USB_REC_LEN 12
extern uint8_t USB_RX_BUF[USB_REC_LEN];
extern uint16_t USB_RX_STA;
void USB_printf(const char *format,...);
打开 usbd_cdc_if.c

uint8_t USB_RX_BUF[USB_REC_LEN];
uint16_t USB_RX_STA=0;
修改以下代码


/**
  * @brief  Data received over USB OUT endpoint are sent over CDC interface
  *         through this function.
  *
  *         @note
  *         This function will issue a NAK packet on any OUT packet received on
  *         USB endpoint until 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 */
  if(*Len<USB_REC_LEN)
        {
                uint16_t i;
                USB_RX_STA = *Len;
                for(i=0;i<*Len;i++)
                {
                        USB_RX_BUF=Buf;
                }
        }
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  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 */
  uint32_t TimeStart=HAL_GetTick();
  USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
//  if (hcdc->TxState != 0){
//    return USBD_BUSY;
//  }
        while(hcdc->TxState)
        {
                if(HAL_GetTick()-TimeStart>10)
                        return USBD_BUSY;
                else
                        break;
        }
  USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
        TimeStart=HAL_GetTick();
        while(hcdc->TxState)
        {
                if(HAL_GetTick()-TimeStart>10)
                        return USBD_BUSY;
        }
  /* USER CODE END 7 */
  return result;
}

添加函数

#include <stdarg.h>
void USB_printf(const char *format,...)
{
        va_list args;
        uint32_t length;
        va_start(args,format);
        length=vsnprintf((char *)UserTxBufferFS,APP_TX_DATA_SIZE,(char *)format,args);
        va_end(args);
        CDC_Transmit_FS(UserTxBufferFS,length);
}
打开main.c

uint8_t i;
        uint8_t USB_RX_BUF_Change[USB_REC_LEN];
  while (1)
  {
                if(USB_RX_STA!=0)
                        {
                        //USB_printf("USB_RX:");
                        //CDC_Transmit_FS(USB_RX_BUF,USB_RX_STA);
                        for(i=0;i<12;i++)
                        {
                               
                                USB_RX_BUF_Change=USB_RX_BUF;
                                //USB_printf("USB_RX_BUF[%d]=%X\n",i,USB_RX_BUF_Change);
                                USB_printf("%X ",USB_RX_BUF_Change);
                        }
                        //USB_printf("\r\n");
                        USB_RX_STA=0;
                        memset(USB_RX_BUF,0,sizeof(USB_RX_BUF));
                        }
               
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

数据长度为12

打开串口,接收成功。



————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/m0_53197842/article/details/145615284

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

24

主题

50

帖子

0

粉丝