cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407, STM32CubeMX, USB FS Device, CDC. How do one check if a VCOM connection are established? Not just if the cable is connected, but that somebody on an attached the host computer, has opened the VCOM port and can accept data?

KGods
Associate II
 
4 REPLIES 4

Hello @KGods​ ,

I recommend you to watch the following training : https://www.youtube.com/watch?v=h9T0RTu9Muc

This may help you.

BeST Regards,

Walid

KGods
Associate II

No, it didn’t help, this was far to simple!

I am looking for a safe way to detect if somebody is connected to the CDC interface, i.e., that a program on the PC has opened the VCOM UART connection. Pls. advice?

Paul1
Lead

Extract from our internal wiki:

How Detection of Open Port was Done

  • The STM32 USB CDC libraries can detect when a USB cable has been plugged in, but they CANNOT detect when a VCP port is opened in the terminal
    • These forum posts ([1] [2] [3] ) show a decent way of doing this using the DTR command that is sent by the terminal program
    • This method poses some problems for our application, so it was not used (problems specifically relating to closing TeraTerm without disconnecting first)
  • Instead, 
  • PortTimer.h
  •  was used to wait a little while for USB by looking at the TxStatus variable in the USB handler
    • TxStatus=1 when transmitting, TxStatus=0 when ready to transmit
  • Because of the way USB works, the host needs to be available to receive data before the VCP will transmit it
    • So TxStatus = 1 forever when there is no port open (the VCP gets stuck waiting to transmit)
    • If TxStatus = 1 for more than 200ms, it is determined that the USB host is not going to respond, and so the data is transmitted only over UART
    • If the USB was determined to be unavailable and then TxStatus goes to 0, it means that the USB host must have came back online, so the timeout is reset and the data is transmitted over both UART and VCP again
    • The timeout is reset every time there is a successful transmit

FYI:

  • We have mirror'd diag on both Hardware UART and VCP on our boards for backwards compatibility
  • ensure VCP is one of first things you configure so you don't miss any of your bootup diagnostic messages.

Paul

KGods
Associate II

The "...the DTR command that is sent by the terminal program..." do the job, thanks!