用CREATEFILE 打开1-9的串口没问题,但是打开COM10之类的串口号就返回-1了,请问是什么问题呢?
=========================分割线=================================
CreateFile() 可获得到串行端口的句柄。 "Win 32 编程人员参考"项"CreateFile() 提及的共享模式必须是 0 中,创建参数必须是 OPEN_EXISTING,并且该模板必须为 NULL。
文件名中,使用"COM 1"到"COM9 时 CreateFile() 是否成功但是,邮件
INVALID_HANDLE_VALUE
如果您使用"COM10 返回或更高版本。
如果端口的名称为 //./COM10,CreateFile() 调用中指定串行端口,正确方法是,如下所示:
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
);
NOTES: 此语法也适用于通过 COM9 的 COM 1 端口。 某些板将允许您选择端口名称自己。 该语法适用于这些名称。
在MSDN->Device File Names中,明确说了CreateFile的三种参数,亦即设备名称有三种表示方式:
"COMX:",
"/$device/COMX",
"/$bus/PCMCIA_0_0_0"。
其中第一种方式只适合从0~9的设备名,亦即COM10:中10超出了设备命名规范,所以用CreateFile打开设备失败。
使用第二种方式/$device/COM10,作为参数传递给CreateFile,成功打开COM口,通讯正常,搞定。
第三种是用在总线驱动上的,暂时没涉及到
You can use paths longer than MAX_PATH characters by calling the wide (W) version of CreateFile and prepending "//?/" to the path. The "//?/" tells the function to turn off path parsing. This lets you use paths that are nearly 32,000 Unicode characters long. However, each component in the path cannot be more than MAX_PATH characters long. You must use fully-qualified paths with this technique. This also works with UNC names. The "//?/" is ignored as part of the path. For example, "//?/C:/myworld/private" is seen as "C:/myworld/private", and "//?/UNC/tom_1/hotstuff/coolapps" is seen as "//tom_1/hotstuff/coolapps".