How to identify a USB device?

6

I have two types of devices (A and B) that communicate serially via USB and can be connected to the PC using linux.

Is there a way to identify which device is connected without having to talk to it? Since each would be handled by a very different protocol?

    
asked by anonymous 25.02.2016 / 14:19

2 answers

2

I'll answer here how I ended up solving the problem before getting to talk to each device.

1) libusb

This library may be a good solution, but I ended up not testing because I could not keep her license as it currently stands. I also found a lib called RS232 which has examples of various types of different methods for Linux and Windows that I recommend to anyone who is learning, it worked until today (05/30/2016) even in Windows 10 and Linux. p>

This second has a quieter license, but I still did not use it.

2) lsusb

It is possible with this approach to know if there has been a state change in USB devices along that path. To actually connect later, you may need another solution.

I started to make a system call to see if I had changed the amount of USB devices and then I tried to connect or disconnect:

# lsusb | wc -l
The problem with this approach is that fast connection and disconnection from one device to another was left undetected, since I looked at this once per second and it was possible to switch from one to the other in that range, but it is an acceptable solution for testing . I would end up doing a diff of the current lsusb result with the result of the previous time, but I did not get there because I interrupted this approach first.

It's also important to note that I tried to find something that would differ from the devices, but lsusb -v and all it can provide information was not useful to me. And yes, this is the most useful solution available in Linux.

How do I use the same type of USB-Serial cable for the two types of devices I'm trying to connect to and all of them have responded the same thing: Prolific PL2303, I could not identify the device without trying to communicate. If the devices are different by the lsusb command, I believe it is a good solution.

3) dmesg

I have looked at ways to look at dmesg only, and it is possible and probably better and more secure than lsusb to know that there has been a change to USB devices if you need to change it later (/ dev / ttyUSB0, for example). Here it is possible for it to show which device was connected and if it does not show, it is probably a driver problem.

I understand that it is a possible solution even for many to use the combination (lsusb and dmesg solve well if it is possible to differentiate the devices by lsusb). But on my system these messages are disabled to speed boot time as much as possible, so you could not use that solution either.

4) Device List

I ended up by skipping lsusb / dmesg by creating a path list of USB devices that can be connected to my system:

char comports[MAXIMUM_COMPORTS][16] = {  "/dev/ttyUSB0", "/dev/ttyUSB1","/dev/ttyUSB2","/dev/ttyUSB3","/dev/ttyUSB4","/dev/ttyUSB5" };

What I do is check if any of these are created on the system and if so, I try to communicate with it using the available communication protocols. Your list may be a lot simpler, maybe just / dev / ttyUSB0 and / dev / ttyUSB1 should work for you.

If / dev / ttyUSB0 exists, I try to open it and communicate as if it were the device 1. If it fails, I close it and try to open it and see it communicates as if it were the device 2. If both fail, I try all devices up to 3 times again on the same port and then I give up.

If you communicate, mark this device as active and connected until a disconnection occurs.

The way to check every device I used was the ct's own stat method.

    
30.05.2016 / 19:23
1

Every device model has a unique device ID, which is a combination of a manufacturer ID (2 bytes) and model ID (plus 2 bytes). The manufacturer ID is assigned by the manufacturer of the USB standard to the manufacturer, and the model ID by the manufacturer itself.

As mentioned @Amadeus, you can see the connected devices and their device IDs with lsusb (part of the usbutils package). With the -v option it displays detailed data for the associated driver, p. ex. path (s) in the / dev folder, which should be sufficient to associate the ID with the pipe you are going to open.

In your program, use the libusb API to list and identify devices.

libusb: Device handling and enumeration

    
10.03.2016 / 16:12