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.
In command line, on linux, try the
lsusb
– user5299
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
– Gomiero
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.
– Rodrigo Guiotti
if you are looking for penUsb or disks try
lsblk
– JJoao