急寻解决,vb中串口问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 01:29:58
如何获得当前计算机的可以使用的串口数量?既当前计算机可以使用COM1,COM2,COM3,COM4的哪几个
请给出vb源码或C源码,谢谢!

使用MSCOMM控件可获取当前计算机的可以使用的串口号,
Option Explicit
Dim a As Integer
Private Sub Form_Load()
Me.AutoRedraw = True
On Error GoTo uerror
For a = 1 To 4 'WINXP下可搜索到16
MSComm1.CommPort = a
MSComm1.PortOpen = True
If MSComm1.PortOpen = True Then
Print "可用Com号= "; a
MSComm1.PortOpen = False
Else
End If
Next
Exit Sub
uerror:
Print "出错或占用Com号= "; a
Resume Next
End Sub

' 可用的串口总数保存在注册表中,读一下就行了。

Option Explicit
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueN