如何获取桌面的SerialPort信息?

问题描述:

我的电脑里有两个串口。如何获取桌面的SerialPort信息?

如何获取串行端口信息?

(我知道的SerialPort的编码,但我真的想知道我的计算机信息的SerialPort)

  • 是有可能得到关于我的电脑的SerialPort的信息?
  • 我正在编vb.net
  • 是可能的vb.net?
  • 如果可能的话,请告诉我该怎么做。
+1

你想知道关于串口?你想知道它是什么号码吗? – smushi 2014-11-06 07:05:32

+0

编号我想知道关于串口信息的波特率,奇偶校验,数据位,握手信息。当然,我也需要一个serialport Number。 – JasonMin 2014-11-12 08:14:13

+0

你必须自己设置这些参数,你没有从系统中读取它们,但是你将它们设置为你需要它们的地方 – Hrqls 2014-11-12 14:16:41

?使用

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 
+0

对不起,我在VS2013使用Vb.Net。但是,我无法在VS2013中找到MSComm。它是免费的组件? – JasonMin 2014-11-12 08:20:58

+0

我不知道。从未在vb.net中使用过mscomm ...我编辑了你的问题,将其标记为vb.net,并在其中添加了一个编辑说明,说明你正在使用vb.net而不是vb6 – Hrqls 2014-11-12 14:15:49

使用Visual Basic .NET中的MSComm控件访问串行端口

因为没有Microsoft .NET Framework类存在访问连接到计算机的通信资源,你可以使用MSComm控件在Microsoft Visual Basic 6.0中。 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中使用Visual Basic 6.0控件时出现错误Studio .NET 用以下代码示例替换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你想有关于串行端口,信息