Looking to determing possible causes of error 2 - Can't open SUB device

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
jrager
Posts: 6
Joined: Fri Oct 13, 2023 12:38 pm

Looking to determing possible causes of error 2 - Can't open SUB device

Post by jrager »

Hi all,

Wondering if anyone can tell me all of the conditions that would trigger the DLL to report an error 2 - Can't open SUB device.

Some background: I'm developing some code that runs two threads - one thread polls connected devices checking to see if a device has been connected/disconnected and the other thread handles requests for operations on a specific device. Thread #1 runs sub_find_devices every 500ms collecting device references and compares that with the previous device references. If a new device is discovered, it's opened and made available to thread #2. If a device has been removed, it's closed & disposed and no longer available to thread #2.

I've seen some interesting behavior when my Windows PC is left alone and begins to enter power save mode. sub_find_devices begins to return new, random device references for device(s) that have remained physically connected. I'm assuming it has something to do with the USB being put into low/no-power mode. Since my code would see that as a "new" device, it would try to open it and of course fail with an error 2 since it's already open under the original device reference. My challenge is the only way I know of to tell that it really IS an already-connected device is to check it's serial number against open devices' serial number, which requires me to open...you can see my Catch-22.

So my latest theory is that if I try to open it and it fails with an error 2, perhaps I can assume that it's already open and can ignore this "new" device reference. But to do that I need to know whether this is the only condition that triggers an error 2. Since I don't know how the DLL is written, I'm wondering if someone who was involved in writing it can tell me.

Advice on alternatives is welcome, but can't be to disable power save mode in settings on the PC or in some other way force power save mode off while the program is running, etc. - I've already considered that but because this software will go to customers that would be a big red flag. I'm looking for a way to gracefully handle the situation.

Thank you!
John

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Looking to determing possible causes of error 2 - Can't open SUB device

Post by serg »

Hi John,

The source code of the sub20.dll can be found in the lib folder of the Linux SUB-20 package which can be downloaded on the SUB-20 home page http://www.xdimax.com/sub20/sub20.html, look for SUB-20-snap-130926.tgz. Besides the libsub.c, the sub20.dll project contains a resource file to store the file version and a def file to declare export functions. The sub20.dll uses libusb-1.0 library to access USB devices. On Windows libusb library calls native WinUsb API. The error 2 is an errno error code which means that the device doesn't exist.
The SUB-20 serial number is an USB string descriptor, so I guess you could detect attached SUB-20 devices by enumerating attached USB devices and reading their device and string descriptors by calling native OS USB APIs. This way you should be able to maintain the most updated SUB-20 device list without the need to open the devices with sub_open call
Below is the SUB-20 USB device descriptor

Code: Select all

bLength                  : 0x12 (18 bytes)
bDescriptorType          : 0x01 (Device Qualifier Descriptor)
bcdUSB                   : 0x200 (USB Version 2.00)
bDeviceClass             : 0xFF (Vendor Specific)
bDeviceSubClass          : 0x00
bDeviceProtocol          : 0x00
bMaxPacketSize0          : 0x40 (64 bytes)
idVendor                 : 0x04D8
idProduct                : 0xFFC3
bcdDevice                : 0x01
iManufacturer            : 0x01
 Language 0x0409         : "XDIMAX"
iProduct                 : 0x02
 Language 0x0409         : "SUB-20"
iSerialNumber            : 0x03
 Language 0x0409         : "2DAD"
bNumConfigurations       : 0x01

Post Reply