我的电脑中有两个串口。
如何获取序列端口信息?
(我知道串口编码,但我真的想知道我的计算机信息的串口)
答案 0 :(得分:1)
您希望了解有关串口的哪些信息?
使用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
答案 1 :(得分:-1)
使用Visual Basic .NET中的MSComm控件访问串行端口
由于不存在用于访问连接到计算机的通信资源的Microsoft .NET Framework类,因此可以使用Microsoft Visual Basic 6.0中的MSComm控件。 MSComm控件通过串行端口启用数据传输和接收,为您的应用程序提供串行通信。要使用调制解调器实现基本串行通信,请按照下列步骤操作: 启动Microsoft Visual Studio .NET。 在“文件”菜单上,指向“新建”,然后单击“项目”。 在“项目类型”下,单击“Visual Basic项目”。 在模板下,单击控制台应用程序 在“名称”框中,键入MyConsoleApplication,然后单击“确定”。
默认情况下,会创建Module1.vb。 右键单击MyConsoleApplication项目,然后单击“添加引用”。 单击COM选项卡,单击组件名称下的Microsoft Comm Control 6.0,单击选择,然后单击确定。
注意若要使用MSComm控件,必须在安装了Microsoft Visual Studio .NET的同一台计算机上安装Microsoft Visual Basic 6.0的相关COM组件。
有关在Visual Studio .NET中使用Visual Basic 6.0控件时许可证问题的详细信息,请单击下面的文章编号,以查看Microsoft知识库中相应的文章: 318597在Visual Studio .NET中使用Visual Basic 6.0控件时出错 使用以下代码示例替换Module1.vb中的代码。
Imports MSCommLib
Module Module1
Sub Main()
'New a MSComm control
Dim MSComm1 As MSComm
MSComm1 = New MSComm
' Buffer to hold input string.
Dim Buffer As String
' Use the COM1 serial port.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read the whole buffer when Input is used.
MSComm1.InputLen = 0
' Open the serial port.
MSComm1.PortOpen = True
Console.WriteLine("Open the serial port.")
' Tell the control to make the Input property return text data.
MSComm1.InputMode() = InputModeConstants.comInputModeText
'Clear the receive buffer.
MSComm1.InBufferCount() = 0
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr(13)
Console.WriteLine("Send the attention command to the modem.")
Console.WriteLine("Wait for the data to come back to the serial port...")
' Make sure that the modem responds with "OK".
' Wait for the data to come back to the serial port.
Do
Buffer = Buffer & MSComm1.Input
Loop Until InStr(Buffer, "OK" & vbCrLf)
' Read the "OK" response data in the serial port.
' Close the serial port.
Console.WriteLine("Read the OK response data in the serial port.")
MSComm1.PortOpen = False
Console.WriteLine("Close the serial port.")
End Sub
End Module
按CRTL + F5构建并运行此项目。您将收到以下输出消息: 打开串口。 将注意命令发送到调制解调器。 等待数据返回串口... 读取串口中的OK响应数据。 关闭串口。
要阅读其余内容,您可以访问How to access serial and parallel ports by using Visual Basic .NET