AnsweredAssumed Answered

Read data from PC to STM32 via USB CDC

Question asked by mao on Feb 24, 2016
Latest reply on Feb 27, 2016 by mao
I am working on a project. One of the part is exchanging data between PC and MCU(STM32F4) via the USB CDC.
Sending data from STM32 to PC is very easy, but there is difficulty receiving data from PC.

In the file usbd_cdc_if.c, I implemented CDC_Receive_FS , and in main(), it constants call the function CDC_Receive_FS to read data. UserRxBufferFS is the buffer created by cubemx. In main(), I get the received data via the array Buf.

I do receive the input from PC, but the problem is everytime I call the function CDC_Receive_FS I get the same data until new data were received.
This is not the supposed logic I want.
I want to keep the buffer empty until new data were received. Or receive nothing if no new data were received.
Therefore I try to clear the buffer once the data were copied to array Buf, but if I do this, I always receive a empty string in main().

What to do?
 
int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
      
  /* USER CODE BEGIN 6 */
  
    uint8_t status=USBD_CDC_ReceivePacket(hUsbDevice_0);
    if(status==USBD_OK)
     {
            if(UserRxBufferFS[0]!='\0') //there are data received
            {
                    memcpy(Buf,UserRxBufferFS,APP_RX_DATA_SIZE);
        //          memset(UserRxBufferFS,1,APP_RX_DATA_SIZE);
        //       UserRxBufferFS[0]='\0';
            }               
     }
     return status;
       
  /* USER CODE END 6 */ 
}

Outcomes