如果你在 Windows 环境下,使用串口通讯(Serial Communication),你会使用到 CreateFile() 来初始化/打开对应的串口
那么,你需要注意,在使用大于 COM9 的端口时,应该这样写:
unsigned long fdwAccess = GENERIC_READ | GENERIC_WRITE;
CreateFileA(
"\\\\.\\COM10", // address of name of the communications device
fdwAccess, // access (read-write) mode
0, // share mode
NULL, // address of security descriptor
OPEN_EXISTING, // how to create
0, // file attributes
NULL // handle of file with attributes to copy
);
否则你收到的返回值是 INVALID_HANDLE_VALUE,再调用 ::GetLastError() 你会得到错误代码 ERROR_FILE_NOT_FOUND(2)
这语法也适用于端口 COM1 到 COM9,我的建议是,都采用这种语法
引用:
[1] support.microsoft.com HOWTO: Specify Serial Ports Larger than COM9