坑人的官方固件库…
软硬件版本
- Windows 1909 或 2004 Preview
- STM32F407VET6
- STM32CubeMX 5.4.0 / 5.6.0 / 5.6.1
- USB库 v1.0_Cube
症状描述
- Windows可检测到虚拟串口,无感叹号(时序设置正确)
- Windows API 中,
- CreateFileA 正常
- SetCommState 失败, GetLastError返回56
- PuTTY无法连接串口,TeraTerm正常
产生原因
单片机无法响应行编码(Line Coding)信息
解决方法
在 usbd_cdc_if.c中添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19/*******************************************************************************/
/* Line Coding Structure */
/*-----------------------------------------------------------------------------*/
/* Offset | Field | Size | Value | Description */
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
/* 4 | bCharFormat | 1 | Number | Stop bits */
/* 0 - 1 Stop bit */
/* 1 - 1.5 Stop bits */
/* 2 - 2 Stop bits */
/* 5 | bParityType | 1 | Number | Parity */
/* 0 - None */
/* 1 - Odd */
/* 2 - Even */
/* 3 - Mark */
/* 4 - Space */
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
static uint8_t lineCoding[7] // 115200bps, 1stop, no parity, 8bit
= { 0x00, 0xC2, 0x01, 0x00, 0x00, 0x00, 0x08 };调整
CDC_Control_FS
函数,添加如下两行:1
2
3
4
5
6
7
8
9
10
11
12
13static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) {
/* USER CODE BEGIN 5 */
switch(cmd) {
...
case CDC_SET_LINE_CODING:
memcpy(lineCoding, pbuf, sizeof(lineCoding)); // This line
break;
case CDC_GET_LINE_CODING:
memcpy(pbuf, lineCoding, sizeof(lineCoding)); // This line
break;
...
}
参考链接
- What is issue with STM32 Virtual Com Port? I can not open it - Stackoverflow
- Unable to configure serial port Error for USB CDC - st.com
后记&牢骚
据说这两个地方原来是有的,但是ST官方把它们删掉了…
我再也不相信官方库了 /(ㄒoㄒ)/~~