Detecting the presence of an U3 even when in use elsewhere

12 posts / 0 new
Last post
ullix
ullix's picture
Detecting the presence of an U3 even when in use elsewhere

I am using an U3 in Python3 on Ubuntu Linux, and it works well.

However, I am struggling with one issue: I am using several instances of my program in combination with different non-LabJack devices, and on starting the program checks for the presence of these devices.

To determine the presence of the U3 I use the command "if len(LabJackPython.listAll(3)) > 0:". But after I issue "device = u3.U3()", the command returns an empty dictionary, same as it would do if no U3 were connected.

So, I can't tell wether there is no U3 plugged in - because I might have ripped out the USB cable - or wether it is used already in another instance.

Is there a way to get a return from any command like "U3 is connected but in use"?

 

 

LabJack Support
LabJack Support's picture
Currently there is no command

Currently there is no command in LabJackPython and the U3 module that returns if a device is being used used in another process.

If you only have one U3 connected and are opening the first found device ("u3.U3()" is first found), immediately after the failing "u3.U3()" call you can check errno for EBUSY (16). Errno EBUSY is set by the Exodriver's open call when it tries to claim a device but it is busy (libusb error LIBUSB_ERROR_BUSY: if another program or driver has claimed the interface). Something like:

import u3
import ctypes

try:
    d=u3.U3()
except:
    print("Couldn't open my U3.")
    err = ctypes.get_errno()
    if err == 16:
        print("My U3 is busy, and likely opened in another process.")

ullix
ullix's picture
Thank you, it does work.

Thank you, it does work.

It does look a bit cumbersome, though, having to load another module just to get an error number, which should be available in the LabJackPython and u3 modules already?

Most importantly, does it work on Linux, Windows, and Mac in the same way?

LabJack Support
LabJack Support's picture
Most importantly, does it

Most importantly, does it work on Linux, Windows, and Mac in the same way?

No, the method described in my previous email only applies to Linux and Mac as they use errno. The Windows driver, UD, does not use errno but it does have a "LabJackPython.LabJackException: LabJack is already open via USB in another program or process" exception. This error is only available on Windows as its driver provides it. The Exodriver only provides the errno functionality which can only detect if a LabJack is busy.

I can look into detecting EBUSY errno and adding more details to the current open error message on Linux and Mac. The Windows error will remain the same.

ullix
ullix's picture
Sigh. Any simplification and

Sigh. Any simplification and homogenization will be appreicated, thanks.

LabJack Support
LabJack Support's picture
I updated LabJackPython on

I updated LabJackPython on GitHub so the USB open on Linux and macOS will detect an access or claim error, and mention so in the exception message.

As a side note, the errno check in my first post was Linux only compatible. On macOS, errno 13 (EACCES due to libusb error LIBUSB_ERROR_ACCESS) will be set if a device is claimed in another or current process.

ullix
ullix's picture
I installed the latest update

I installed the latest update, but I don't see this as a helpful improvement.

When calling u3.U3() for the second time, I get this exception message: 'Couldn't open device. Please check that the device you are trying to open is connected.' Is that what it is supposed to be?

Informationwise it is not really different from where I started, because I still can't distinguish between "not available because I ripped out the cable inadvertently", or "not available, because already in use".

I first suspected I had installed the update incorrectly and really was still using the old version, so I looked for the usual suspects for version information, like "LabJackPython.__version__" and variants, but all I could find was "LABJACKPYTHON_VERSION = "5-26-2015"", which isn't really helpful to distinguish between modifications all occuring in 2018.

Likewise, no helpful version info for u3 and ei1050.

One more suggestion: the Readme suggests to install LJP with "$ sudo python setup.py install". Well, on my Ubuntu 16.04 the command python is for version Py2, and I now need to use command python3. Although I knew this, I fell to this trap :-/. Some warning in the Readme might be helpful.

So, errno works on Linux and Mac, but Linux uses errno=16, and Mac uses errno=13, and Windows uses something different.

 

LabJack Support
LabJack Support's picture
I updated our LabJackPython

I updated our LabJackPython on GitHub with newer versioning. Versioning is still date based, "1.<YEAR><MONTH>", but is now PEP compliant so pip and others can do versioning with them correctly. The version is 1.201812. Also, I added LabJackPython.__version__. Version numbers are currently only in the LabJackPython module. The ei1050 is part of an example without a version.

As for the open error messages on Linux and macOS, for a u3.U3() command there are these two error messages:

Couldn't open device. Please check that the device you are trying to open is connected.
Couldn't open device. Device access or claim error. Please check that the device is not already open in another or the current process.

Two u3.U3() calls in a row should produce the second error, assuming there is a device connected and the first call was successful. Install the updated LabJackPython. If you are not seeing the "Device access or claim error." message, check if LabJackPython.__version__ 1.201820 is being used.

If after reinstalling and you don't see the new version, try cleaning up the old LabJackPython files in your installations's "site-packages" directory. Remove LabJackPython related egg and egg-info files that are not version 1.201820. If that does resolve it, further remove these files if they are present:

LabJackPython.py
Modbus.py
u12.py
u3.py
u6.py
ue9.py

ullix
ullix's picture
Thanks for your efforts and

Thanks for your efforts and quick answers. I'll test it soon, but something else is confusing: My first post of this thread went into moderation. OK. The others, however, disappeared after posting; I could not even see them even while I was logged on. But they were visible the next day.

My last post has disappeared completely, not even visible now, yet, you seem to have responded to it ???

Anything wrong with forum or my mistake?

ullix
ullix's picture
I downloaded the update as a

I downloaded the update as a zip package and installed with "sudo python3 setup.py install", and all went fine!

Using year and months as version is perfectly ok with me; though given your updating pace you may need at least the day also ;-). And what holds you back from giving such versions also for u3 and ei1050? Gives me more confidence on checking my installation.

So now I simply do:


    try:
        lju3 = u3.U3()
    except Exception as e:
        stre = str(e)
        if "Couldn't open device. Please check that the device you are trying to open is connected." in stre:
            print("No LabJack device found. Is it connected?")

        elif "Couldn't open device. Device access or claim error" in stre:
            print("A LabJack device was found, but it may already be in use by a different program")

        else:
            print("Connecting to LabJack device gave unexpected error: {}".format(stre))

Since there is no (alpha-)numeric error code within the exception data e, the checking is a bit clumsy. But it works!

So, this now works for Linux and Mac, but is still different for Windows?

 

LabJack Support
LabJack Support's picture
In regards to the forum,

In regards to the forum, posts aren't visible until we approve them. We had forgotten to approve your post that we replied to—that's been fixed now. Sorry that it's confusing, but at least it's nothing wrong with how you're using the forum.

In regards to your other questions, we'll answer them soon.

LabJack Support
LabJack Support's picture
Windows has three errors

Windows has three errors which are based on the UD driver error system and error strings:

LabJackPython.LabJackException: LabJack not found
LabJackPython.LabJackException: Device no longer connected
LabJackPython.LabJackException: LabJack is already open via USB in another program or process

With the LabJackException.errorCode set to 1007, 1015 and 1010 respectively. "Device no longer connected" indicates a previously opened device is now disconnected. Note on Windows, its driver does not provide a close call for a single device. LabJackPython.Close() needs to be called and closes all device handles in the process.

Regarding versioning, our LabJackPython interface and its modules have just the one version collectively, which currently is only in the LabJackPython.py module. If we were to have a version in the other modules, it would just be the version from LabJackPython.py which all the other modules use. The last change to detect the device claim / no access was a a function in LabJackPython.py which applies to the U3, U6 and UE9. The ei1050 class is not part of the LabJackPython modules. It is part of example code, and we do not version examples.