I've experienced random and unexplainable problems with CDC communication over USB, while using CubeMX-generated code.
The PC was always able to enumerate the COM port, but sometimes it was unable to open it, (Windows reports a "wrong parameter" error).
After a thorough research and the use of an USB sniffer, I think I've identified the problem.
The issue comes from a wrong answer to the PC's request GET_CODING_LINE.
When the PC issues this request, it expects a payload containing line speed in bps, parity, stop bits and databits.
CDC_Control_FS() in usbd_cdc_if.c should fill the payload with correct data, but in facts its body is empty. There a switch listing the various CDC messages, but the case statements do nothing.
case CDC_SET_LINE_CODING:break;case CDC_GET_LINE_CODING:
break;(...and so on...)
For this reason, the payload contains random data that, in most cases, prevent the communication to work.
Here is an example of a request that the sniffer captured.
The body of the function is classified as "user code", but leaving it empty is misleading.
I wasted a lot of time to track down this problem.
I understand that CDC code is provided as an example, yet there is not even a simple #warning to help developers avoiding this pitfall.
I think this should be fixed as soon as possible.