cancel
Showing results for 
Search instead for 
Did you mean: 

USB CDC class: How to determine if VCP has been opened?

tpoms9
Associate II
Posted on September 25, 2014 at 08:45

Hello!

I'm using the USB CDC class implementation provided by STM32CubeMX 4.3.0 (firmware library v1.3.0).

Is it possible to check on the microcontroller if the virtual COM port has been opened? I want to transmit a version string via USB, but this would only be reasonable if the port is opened, otherwise the user will not receive the information.

Regards,

Thomas

#stm32 #cdc #usb #vcp
6 REPLIES 6
chen
Associate II
Posted on September 25, 2014 at 16:56

Hi

I am not familiar with the Cube implementation, I am still using the old USB OTG driver.

I think they are similar (identical all bar naming of variables and functions)

Some where in the guts of the driver, it knows if the USB is connected because it goes through a USB connected IRQ. I am affraid you will have to dig around the code to find it.

sdim
Associate III
Posted on September 26, 2014 at 09:36

If you want to know if the device is configured:

if (pdev->dev_state == USBD_STATE_CONFIGURED) ...

If you want to know if you have a CDC device and it is connected, use a global variable inside the HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) procedure.

If USBD_LL_DevConnected status is USBD_OK you should change the value of the global variable to 1 (or whatever you prefer).

You should write the code to update this global variable into every related procedure (HAL_PCD_DisconnectCallback , HAL_PCD_SuspendCallback, HAL_PCD_ResumeCallback).

srdjan
Associate II
Posted on September 26, 2014 at 10:55

Here is the solution that I tested and it is working fine:

http://stackoverflow.com/questions/5338875/detecting-open-pc-com-port-from-usb-virtual-com-port-device

Posted on September 26, 2014 at 12:46

Hi Thomas,

When you open the COM port, you get a call to ComPort_Config(). You can use it to get notification for opening COM port.

I've tested it with Hercule on Windows7 and it may depend on OS (but it was called twice !).

Regards,

Heisenberg.

Pieter Conradie
Associate III
Posted on May 23, 2018 at 10:11

Sorry for posting to old thread, but for others searching for a solution,  this is what I did:

static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)

{

...

  case CDC_SET_CONTROL_LINE_STATE:

      {

          USBD_SetupReqTypedef * req = (USBD_SetupReqTypedef *)pbuf;

          if(req->wValue &0x0001 != 0)

          {

              // DTR is set...

          }

      }

    break;

...

}
David Jiang
Associate

That's solve my problem, thanks.