如何获取使用 COM 端口的内容 Visual Basic (.NET) [英] How to get what's using the COM Port Visual Basic (.NET)

查看:19
本文介绍了如何获取使用 COM 端口的内容 Visual Basic (.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这个代码

For i As Integer = 0 to My.Computer.Ports.SerialNames.Count - 1
      ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next

示例输出:

COM1
COM2
COM3

这会返回一个 ComboBox 中使用的 COM 端口列表

this returns a list of used COM ports in a ComboBox

现在我想做的是这样的

COM1 <USB Mouse>
COM2 <USB Keyboard>

我想通过 COM 端口获取任何东西.

I want to get whatever it is using the COM port.

希望大家帮忙,谢谢!

推荐答案

在这里我找到了解决您问题的链接:msdn论坛

Here I found a link where your question is solved: Msdn forum

它使用 Windows Management Instrumentation 从端口检索您想要的数据.这样,您将检索端口的全名,包括您想要的部分.

It uses Windows Management Instrumentation to retrieve the data you want from the ports. This way you will retrieve the full name of the port, including the part you want.

代码如下:

' Add reference to System.Management.dll.
Try
    Dim searcher As New ManagementObjectSearcher( _
   "root\cimv2", _
   "SELECT * FROM Win32_SerialPort")

    For Each queryObj As ManagementObject In searcher.Get()
        MsgBox(queryObj("Name"))
    Next

Catch err As ManagementException
    MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try

这篇关于如何获取使用 COM 端口的内容 Visual Basic (.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆