Skip to main content
KGods
Associate II
July 19, 2021
Question

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?

  • July 19, 2021
  • 4 replies
  • 1678 views

..

This topic has been closed for replies.

4 replies

Walid ZRELLI
Visitor II
July 27, 2021

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
KGodsAuthor
Associate II
July 27, 2021

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
Senior III
July 27, 2021

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
KGodsAuthor
Associate II
July 28, 2021

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