cancel
Showing results for 
Search instead for 
Did you mean: 

How to recognize USB ready

xiewenliang
Associate II

I'm trying to transfer data to the upper computer using virtual port com on STM32G473RET6.

usb.png

But how to recognize USB ready before transfering data?

if (USBD_STATE_CONFIGURED == hUsbDeviceFS.dev_state)
{
   CDC_Transmit_FS(UserTxBufferFS, payload_len);
}

The above code is not rigorous,if (USBD_STATE_CONFIGURED == hUsbDeviceFS.dev_state)Through this code, just knowing that USB has already been initialized.But stm32 MCU doesn't know whether the upper computer has opened the serial port.

How to know the upper computer has opened the serial port?

Thanks for the help!

 

3 REPLIES 3
TDK
Guru

In short, this information is not provided to the peripheral.

When the USB host connects, the device is initialized and it will set the state to configured. This doesn't mean a program has the port open, but the serial port driver is ready to receive information. You can follow the checks within CDC_Transmit_FS to determine when this happens. That's all you get.

If you have control of the terminal program, you can send information to indicate the terminal is open and ready for information.

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist III

As soon as the host starts reading the USB data endpoint (sending IN requests) or writing data (OUT requests)  you can consider the connection ready. Other events (reset, enumeration, control requests for serial port parameters etc.) are helpful to track the progress and state of connection). Some people say that a control request for serial port parameters means that the host application opened the virtual serial port. But this can be activity of the device driver, before an app opens the port.

 

gbm
Lead III

When the connection opens, SetControlLineState request is sent with two least significant bits set to 11. In all my VCOM apps I detect the connection by comparing the previous ControlLineState with the new one. If the state has changed and the new one has these 2 bits set, you may start sending data no sooner than 50 ms after that change - that's my experience with terminal programs like TeraTerm.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice