除了從1循環到32並嘗試打開它們之外,是否有可靠的方法來獲得系統上的COM端口?如何枚舉計算機上的可用COM端口?
2
A
回答
2
這是1〜255最快的,你可以做到這一點是使用QueryDosDevice
這樣
Option Explicit
'--- for CreateFile
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const OPEN_EXISTING As Long = 3
Private Const INVALID_HANDLE_VALUE As Long = -1
'--- error codes
Private Const ERROR_ACCESS_DENIED As Long = 5&
Private Const ERROR_GEN_FAILURE As Long = 31&
Private Const ERROR_SHARING_VIOLATION As Long = 32&
Private Const ERROR_SEM_TIMEOUT As Long = 121&
Private Declare Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (ByVal lpDeviceName As Long, ByVal lpTargetPath As String, ByVal ucchMax As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Function PrintError(sFunc As String)
Debug.Print sFunc; ": "; Error
End Function
Public Function IsNT() As Boolean
IsNT = True
End Function
Public Function EnumSerialPorts() As Variant
Const FUNC_NAME As String = "EnumSerialPorts"
Dim sBuffer As String
Dim lIdx As Long
Dim hFile As Long
Dim vRet As Variant
Dim lCount As Long
On Error GoTo EH
ReDim vRet(0 To 255) As Variant
If IsNT Then
sBuffer = String$(100000, 1)
Call QueryDosDevice(0, sBuffer, Len(sBuffer))
sBuffer = Chr$(0) & sBuffer
For lIdx = 1 To 255
If InStr(1, sBuffer, Chr$(0) & "COM" & lIdx & Chr$(0), vbTextCompare) > 0 Then
vRet(lCount) = "COM" & lIdx
lCount = lCount + 1
End If
Next
Else
For lIdx = 1 To 255
hFile = CreateFile("COM" & lIdx, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0)
If hFile = INVALID_HANDLE_VALUE Then
Select Case Err.LastDllError
Case ERROR_ACCESS_DENIED, ERROR_GEN_FAILURE, ERROR_SHARING_VIOLATION, ERROR_SEM_TIMEOUT
hFile = 0
End Select
Else
Call CloseHandle(hFile)
hFile = 0
End If
If hFile = 0 Then
vRet(lCount) = "COM" & lIdx
lCount = lCount + 1
End If
Next
End If
If lCount = 0 Then
EnumSerialPorts = Split(vbNullString)
Else
ReDim Preserve vRet(0 To lCount - 1) As Variant
EnumSerialPorts = vRet
End If
Exit Function
EH:
PrintError FUNC_NAME
Resume Next
End Function
的片段回落到CreateFile
在Windows 9x。爲簡潔起見,函數被stubbed。
3
我相信在現代Windows環境下,你可以在註冊表中找到它們,下面的鍵HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
。我不確定指定註冊表項的正確方法。不過,我只有在Windows XP上測試過它。
3
看看這篇文章從蘭迪·伯奇的網站:CreateFile: Determine Available COM Ports
還有使用MSComm控件的方法:ConfigurePort: Determine Available COM Ports with the MSCOMM Control
代碼有點太長了,我在這裏發佈,但鏈接有一切你需要。
相關問題
- 1. 使用RegQueryInfoKey()枚舉COM端口
- 2. 枚舉計算機內存
- 3. pyserial枚舉端口
- 4. 計算枚舉值
- 5. 枚舉做計算
- 6. 如何枚舉可用的代理服務器IP及端口
- 7. 枚舉帶驅動程序的COM端口
- 8. 如何枚舉使用VBA在計算機上設置的DSN列表?
- 9. 如何枚舉計算機所在子網的所有ip
- 10. 如何正確登錄我的Applet以訪問計算機的COM端口?
- 11. RegEx端口枚舉驗證
- 12. 將Silverlight連接到本地計算機COM端口
- 13. 容器如何枚舉網絡上可用的主機?
- 14. 枚舉並做計算
- 15. 如何連接手機與COM端口
- 16. 用Jquery計算和枚舉LI的
- 17. 在COM IDL中如何從COM依賴關係引用枚舉?
- 18. 如何枚舉端點url?
- 19. C#Com枚舉和VB6
- 20. Xml序列化c#枚舉只適用於某些計算機
- 21. 可擴展枚舉計劃
- 22. 如何枚舉窗口LPT端口及其I/O範圍?
- 23. 如何在我的計算機上阻止端口23上的數據包?
- 24. 計算機上同一端口上的Udp套接字通信
- 25. RXTX如何從COM端口
- 26. 如何解鎖COM端口
- 27. 枚舉的接口
- 28. 新的COM端口可用事件
- 29. 列出可用的COM端口
- 30. 發送方計算機如何知道接收方計算機上自定義進程的端口號?
例如...? – 2011-03-12 04:11:30
在.NET中這很容易,你有什麼理由爲什麼使用VB6? – Arafangion 2011-03-12 04:19:40
@Arafangion遺留應用程序。 – AngryHacker 2011-03-12 04:41:26