使用CreateFile打开大于COM9的端口 GetLastError返回ERROR

tech2025-07-28  3

如果你在Windows环境下,使用串口通讯(Serial Communication),你会使用到CreatFile()来初始化/打开对应的串口

那么,你需要注意,在使用大于COM9的端口时,应该这样写:

 

CreateFile( "\\\\.\\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

最新回复(0)