線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1808
推到 Plurk!
推到 Facebook!

COM PORT 連結的疑問

尚未結案
yjh0225
一般會員


發表:7
回覆:7
積分:2
註冊:2003-10-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-08-04 16:42:51 IP:218.168.xxx.xxx 未訂閱

hCom1=CreateFile("COM1", //串口號
GENERIC_READ|GENERIC_WRITE, //允許讀寫
0, // 通訊設備必須以獨佔方式打開
NULL, //無安全屬性
OPEN_EXISTING, //通訊設備已存在
0,0);

我是以此方式連接上 COM1 的設備。
但是如果設備是在com10時,也就是"COM1"變成"COM10"時

(hCom1 == INVALID_HANDLE_VALUE) 就會成立,連接不上COM10了

經過測試COM1 ~ COM9均可以正常連線,不知為什麼變成2位數以上的com port時就會結接失敗了。

煩請各位大大給予解答指教。

yjh0225
一般會員


發表:7
回覆:7
積分:2
註冊:2003-10-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-08-04 17:16:22 IP:218.168.xxx.xxx 未訂閱

我找到解答了!!

CreateFile() can be used to get a handle to a serial port. The "Win32 Programmer's Reference" entry for "CreateFile()" mentions that the share mode must be 0, the create parameter must be OPEN_EXISTING, and the template must be NULL.

CreateFile() is successful when you use "COM1" through "COM9" for the name of the file; however, the message "INVALID_HANDLE_VALUE" is returned if you use "COM10" or greater.

If the name of the port is \\.\COM10, the correct way to specify the serial port in a call to CreateFile() is as follows:

   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: This syntax also works for ports COM1 through COM9. Certain boards will let you choose the port names yourself. This syntax works for those names as well.

Ktop_Robot
站務副站長


發表:0
回覆:3511
積分:0
註冊:2007-04-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-04-18 17:32:10 IP:000.000.xxx.xxx 未訂閱
提問者您好:


以上回應是否已得到滿意的答覆?


若已得到滿意的答覆,請在一週內結案,否則請在一週內回覆還有什麼未盡事宜,不然,
將由版主(尚無版主之區域將由副站長或站長)自由心證,選擇較合適之解答予以結案處理,
被選上之答題者同樣會有加分獎勵同時發問者將受到扣 1 分的處分。不便之處,請見諒。


有問有答有結案,才能有良性的互動,良好的討論環境需要大家共同維護,感謝您的配合。

------
我是機器人,我不接受簡訊.
系統時間:2024-05-09 5:39:40
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!