我的计算机中有两个串行端口。
如何获得串行端口信息?
(我了解Serialport的编码,但我真的很想了解我的计算机信息的Serialport)
您想要有关串行端口的哪些信息?
使用MSComm控件,您可以使用以下函数来确定端口是否存在以及端口是否已在使用中:
Public Enum PortAttr
PortFree = 0
PortInUse = 1
PortUnknown = 2
End Enum
Public Function CheckPort(intPort As Integer) As PortAttr
On Error GoTo ErrorFound
With MSComm1
If .PortOpen Then .PortOpen = False
.CommPort = intPort
.PortOpen = True
CheckPort = PortFree
If .PortOpen = False Then .PortOpen = True
End With 'MSComm1
Exit Function
ErrorFound:
Select Case Err.Number
Case 8002 'port doesnt exist
CheckPort = PortUnknown
Case 8005 'port already in use
CheckPort = PortInUse
Case Else
MsgBox Err.Description, vbCritical, "Error " & CStr(Err.Number) & " on Port " & CStr(intPort)
End Select
On Error GoTo 0
End Function
然后,您可以从1到16循环查看是否存在这些端口(USB转换器可以添加额外的端口)
For intIndex = 1 To 16
Select Case CheckPort(intIndex)
Case PortFree
intFree = intFree + 1
cboPort.AddItem "Com" & CStr(intIndex), intFree 'add the port to the "free" list
cboPort.ItemData(intFree) = intIndex
Case PortInUse
cboPort.AddItem "Com" & CStr(intIndex) 'add the port to the "in use" ist
End Select
Next intIndex
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句