连接到C#中的特定USB设备 [英] connect to specific USB device in C#

查看:156
本文介绍了连接到C#中的特定USB设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个系统通过USB连接到PC.我正在编写具有连接"按钮的软件,该按钮允许用户连接到该特定的USB端口.我的解决方法是
1.扫描所有可用的COM端口
2.向每个COM端口发送一条特定的消息,如果系统回显匹配的另一条特定消息,则软件将连接到该COM端口.

但是,问题是某些COM端口已经打开并正在使用中.因此,当软件尝试打开该COM端口(不是我的USB)时,将出现错误.因此,我想扫描所有具有特定名称的USB端口(我的USB端口名称:TUSB3410设备,在设备管理"下).因此,现在只需将作业连接到该特定的USB设备名称即可.

如何获取所有USB设备名称?

谢谢:)

Hi guys,
I have a system connect to PC through USB. I am writing the software that has "Connect" button which allows user to connect to that specific USB port. My solution is
1. Scan all available COM ports
2. Send a specific message to each COM port and if the system echo back another specific message that match, the software will connect to that COM port.

However, the problem is some COM ports are already open and in use. So when the software try to OPEN that COM port (which is not my USB), it will be an error. So I would like to scan all the USB ports, with each specific name (my USB port names: TUSB3410 Device, under Device Management). So the job now just connect to that specific USB device name.

How can I get all the USB device name?

Thank you, :)

推荐答案



您是在谈论USB还是COM端口仿真器?听起来您正在谈论将USB连接为COM端口仿真器的USB.

在这种情况下,您可以使用以下方法获取COM端口设备的名称列表.

SerialPort.GetPortNames();

然后可以遍历列表,并使用forloop语句找到要查找的列表.

希望对您有所帮助.

Dave:)
Hi,

Are you talking about USB or COM port emulator? It sounds like you are talking about a USB that connets as a COM port emulator.

If that is the case, you can use the following method to get the list of names of com port devices.

SerialPort.GetPortNames();

you can then iterate through the list and find the one you are looking for using a forloop statement.

Hope it helps.

Dave :)


是的,我说的是USB作为COM端口连接.是的,我做了一个SerialPort.GetPortNames()以获取COMx名称,并连接到该特定的USB.但是我正面临这个问题,比方说,我连接USB的PC具有以下COM(在设备管理器中):

1. COM1:通信端口
2. COM3:Logitech鼠标(例如)
3. COM4:其他USB到COM端口的设备
5. COM5:TUSB3410设备(我的USB)

我遍历循环,在每个循环中我都这样做:
1.将当前端口分配给Port_temp
2.检查Port_temp是否已经打开?如果它是开放的,那不是一个.另一方面,它正在使用中.
3.如果未打开,请尝试打开Port_temp
4.发送消息
5.接收来自MCU的消息
6.如果消息与某些预定义消息匹配,请检查来自MCU的消息.将Port_temp分配给SerialPort并连接到这些端口.

完成循环后,我成功连接到COM5.但是,当我将软件运行到另一台计算机(假设它们在上面具有相同的列表端口)时,遇到了可以打开COM3的问题.我不知道为什么会出现问题,因为在第3步打开端口之前,我检查了第2步是否打开了端口.

这就是乳清我想获取USB设备名称的原因,即通讯端口,TUSB3410设备等因此,我可以使用唯一名称连接到特定端口.或者获得VID/PID也很好.无论如何,我可以获得有关每个COM端口的那些信息吗?

问候
Yes, I am talking about a USB that connects as a COM port. And yes, I do a SerialPort.GetPortNames() to get the COMx name, and connect to that specific USB. But I am facing the problem, let''s say the PC I connect the USB has the following COM (in device manager):

1. COM1: Communication Port
2. COM3: Logitech mouse (for example)
3. COM4: other USB to COM port device
5. COM5: TUSB3410 device (my USB)

I iterate the loop, in each loop I do:
1. assign the current port to a Port_temp
2. check if Port_temp is already open? If it is open, it is not the one. It is on the other hand is in use.
3. if it is not open, try open Port_temp
4. send a message
5. receive a message from the MCU
6. check the message from the MCU, if the message matches with some predefined message. Assign the Port_temp to SerialPort and connect to those.

I was successful to connect to the COM5 after doing the loop. However, when I ran the software to other computer(assume they have the same list port above) , it faced the problem that it can open COM3. I do not know why there is a problem, because before opening a port on step 3, I checked if the port was opened or not on step 2.

So that is the reason whey I would like to get the device name of the USB,i.e. Communication Port, TUSB3410 device,etc. So I can connect to specific port with the unique name. Or get the VID/PID is good too. Are there anyway I can get those information about each COM port?

Regards,




其实我用try catch语句解决了这个问题.因此,如果软件无法尝试打开一个端口,它将尝试另一个端口,而不会出现任何问题.但是,我想知道如何使用C#获取有关USB(COM)端口的更多信息,例如VID/PID,设备名称等.这应该非常有趣.

谢谢您的帮助,
问候,
Anh
Hi,

Actually I solved the problem with try catch statement. So if the software can not try to open 1 port, it will try another port without any problems. However, I would like to know how to use C# to get more information about the USB (COM) port, e.g, VID/PID, Device Name, etc. That''s should be very interesting to know.

Thank you for helping me in this,
Regards,
Anh


这篇关于连接到C#中的特定USB设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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