串口com10以上CreateFile開啟成功的方法

來源:互聯網
上載者:User

在做一個串口通訊,遇到了這個問題,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也是可以的,所以建議就用這種方法吧。

後在網上看到說加"\\.\",試了一下也可以,和"\\?\"一樣,我現在就在想“?”到底是什麼意思,是不是換成其他符號也可以呢,後來試了,只有換成“."可以,關於這個問題還請高手解答。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.