查找我的设备连接的串行端口 [英] Find serial port where my device is connected

查看:69
本文介绍了查找我的设备连接的串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用密码键盘.我需要我的程序在没有用户交互的情况下找到连接密码键盘的端口.

I'm starting to work with a pinpad. I need that my program find the port where the pinpad is connected without user interaction.

我尝试过:

Dim searcher As New ManagementObjectSearcher("root\cimv2","SELECT * FROM Win32_SerialPort")

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

但这只给我COM1"和COM2"作为答案(我的设备连接到COM4)

but this only give me "COM1" and "COM2" as an answer (My device is connected to COM4)

和与

Dim searcher As New ManagementObjectSearcher("root\cimv2", "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0")

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

有了这个,我可以看到我的设备友好名称,但我不知道如何获取端口(我收到诸如HP 打印机"之类的名称)

With this one I can see my device friendly name but I don´t know how to get the port (I receive the names like 'HP printer')

知道如何获得所需的端口吗?

Any idea of how can I get the port that I need?

提前致谢

推荐答案

根据评论,听起来您的设备是一个 USB 设备,它有一个驱动程序,使其看起来是(模拟)串行端口连接设备.在那种情况下,我会使用:

Based on the comments it sounds like your device is a USB device that has a driver that causes it to appear to be (emulates) a serial port attached device. In that case I would use:

My.Computer.Ports.SerialPortNames

My.Computer.Ports.SerialPortNames

枚举和循环所有串行端口.然后,一次尝试打开每一个并向您知道它响应的设备发送命令.大多数设备都有某种心跳或保持活动消息,它们会响应.无论您在哪个端口上获得响应,都是您需要使用的端口.

to enumerate and loop over all serial ports. Then, one at a time try to open each one and send a command to the device that you know it responds to. Most devices have some kind of heartbeat or keep alive message that they will respond to. Whichever port you get a response on is the port you need to use.

这篇关于查找我的设备连接的串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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