How to identify a USB device?

Asked

Viewed 1,377 times

6

I have two types of devices (A and B) that communicate serial through 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 one would be treated by a very different protocol?

  • In command line, on linux, try the lsusb

  • I haven’t tried it, but here are three suggestions that might help: Linux USB, for C/C++: libusb, and in Java: jUSB: Java USB

  • Thanks for the comments... I’ll take a look. lsusb pointed me to a linux folder where I can get the information and libusb has plenty of potential, I will also check.

  • if you are looking for penUsb or disks try lsblk

2 answers

2


I will answer here how I ended up solving the problem before I get 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 holds. I also found a lib called RS232 that has examples of several different methods for Linux and Windows that I recommend to those who are learning, it worked until today (05/30/2016) even in Windows 10 and Linux.

This second has a quieter license, but even so I ended up not using.

2) lsusb

It is possible with this approach to know if there has been a change of status in the USB devices by this way. To actually connect later, you’ll need another solution.

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

# lsusb | wc -l

The problem with this approach is that the quick connection and disconnection from one device to another was left undetected, since I looked at it once a second and it was possible to swap one by another in this interval, but it is an acceptable solution for testing. I would end up making a diff of the actual result of lsusb with the result of the previous instant, but I didn’t get there by interrupting before this approach.

It is also important to note that I tried to find something that differentiated the devices, but the lsusb -v and everything it can provide information was not useful to me. And yes, this is the most useful solution available on Linux.

As I use the same type of USB-Serial cable for the two types of devices I am trying to connect and all answered 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 analyzed ways to look only at dmesg and it is possible and probably better and more guaranteed than lsusb to know that there has been a change in Usbs devices if you need its path after (for example/dev/ttyUSB0). Here it is possible that it shows 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 up boot time as much as possible, so we could not use this solution either.

4) List of devices

I ended up bypassing the lsusb/dmesg 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 in the system and if they are, I try to communicate with them using the available communication protocols. Your list may be much simpler, maybe only /dev/ttyUSB0 and /dev/ttyUSB1 solve for you.

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

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

The way to check each device I used was the stat method of c itself.

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 USB standard manager 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 presents detailed data of the associated driver, e. g. path(s) in the /dev folder, which should be sufficient to associate the ID to the pipe you will open.

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

libusb: Device Handling and enumeration

Browser other questions tagged

You are not signed in. Login or sign up in order to post.