Question
Read data from PC to STM32 via USB CDC
Posted on February 24, 2016 at 06:02
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 implementedCDC_Receive_FS
, and in main(), it constants call the functionCDC_Receive_FS
to read data.UserRxBufferFS
is the buffer created by cubemx. In main(), I get the received data via the arrayBuf
. I do receive the input from PC, but the problem is everytime I call the functionCDC_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 arrayBuf
, but if I do this, Ialways 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 */
}