串口com10以上CreateFile打开成功的方法

在做一个串口通讯,遇到了这个问题,com1到com9正常打开,com10以上就不行了,用getlasterror查看,错误位找不到指定文件,后查看MSDN,解释如下:

Windows NT: 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". 

当路径长度大于MAX_PATH 后,应加上"\\?\",个人猜想,可能打开串口的话只能支持com1到com9,大于4个字符就不行了,故com10以上应添加"\\?\",即

CreateFile("\\\\?\\COM10", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

这样就能成功打开。以上方法所有com1~9也是可以的,所以建议就用这种方法吧。

后在网上看到说加"\\.\",试了一下也可以,和"\\?\"一样,我现在就在想“?”到底是什么意思,是不是换成其他符号也可以呢,后来试了,只有换成“."可以,关于这个问题还请高手解答。


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值