如何获取Desktop的串行端口信息



我的计算机中有两个串行端口。

如何获取串行端口信息?

(我知道串行端口的编码,但我真的很想知道我的计算机信息的串行端口)

  • 有可能得到我电脑串行端口的信息吗
  • 我在vb.net中编码
  • 在vb.net中是可能的吗
  • 如果可能的话,请告诉我怎么做

您希望获得有关串行端口的哪些信息?

使用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

使用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响应数据。关闭串行端口。

要阅读本文的其余部分,您可以转到此处如何使用Visual Basic.NET 访问串行和并行端口

最新更新