Skip to content

DCAM: can't recover if other process had the camera #101

@xtofl

Description

@xtofl

Description

When opening a Hamamatsu camera with another process (e.g. the HCImageLive app), it is normal that a python app can't create a DCAMCamera instance. When that happens, we want to notify the user, and retry after the other process has released the camera. The DCAMCamera constructor keeps raising the same exception.

>>> from pylablib.devices.DCAM import DCAMCamera, dcamapi4_lib

>>> # OTHER process has camera open

>>> DCAMCamera()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\DCAM.py", line 162, in __init__
    self.open()
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\DCAM.py", line 184, in open
    with libctl.temp_open():
  File "C:\Program Files\Python312\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\utils\load_lib.py", line 290, in temp_open
    init_result,open_result,opid=self.open()
                                 ^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\utils\load_lib.py", line 255, in open
    init_result=self._do_init() # pylint: disable=assignment-from-no-return
                ^^^^^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\DCAM.py", line 24, in _do_init
    self.cameras=self.lib.dcamapi_init()
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <lambda>
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\core\utils\ctypes_wrap.py", line 276, in wrapped_func
    retval=func(*call_args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\dcamapi4_lib.py", line 44, in errchecker
    raise DCAMLibError(func.__name__,code)
pylablib.devices.DCAM.dcamapi4_lib.DCAMLibError: function 'dcamapi_init' raised error -2147483130 (DCAMERR_NOCAMERA)

>>> # closing other software

>>> DCAMCamera()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\DCAM.py", line 162, in __init__
    self.open()
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\DCAM.py", line 184, in open
    with libctl.temp_open():
  File "C:\Program Files\Python312\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\utils\load_lib.py", line 290, in temp_open
    init_result,open_result,opid=self.open()
                                 ^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\utils\load_lib.py", line 255, in open
    init_result=self._do_init() # pylint: disable=assignment-from-no-return
                ^^^^^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\DCAM.py", line 24, in _do_init
    self.cameras=self.lib.dcamapi_init()
                 ^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <lambda>
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\core\utils\ctypes_wrap.py", line 276, in wrapped_func
    retval=func(*call_args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\ProductionPC02Lab012\xtofl\pcr_software\.venv\Lib\site-packages\pylablib\devices\DCAM\dcamapi4_lib.py", line 44, in errchecker
    raise DCAMLibError(func.__name__,code)
pylablib.devices.DCAM.dcamapi4_lib.DCAMLibError: function 'dcamapi_init' raised error -2147483130 (DCAMERR_NOCAMERA)

Environment

  • Windows 10
  • pylablib==1.4.3

Workaround

It is possible to give the driver a bump, and allow reconnecting. Since we're using only 1 camera, that works for us. It could break applications that use multiple cameras.

>>> # ... continued from previous session
>>> dcamapi4_lib.wlib.dcamapi_uninit()
>>> DCAMCamera()
<pylablib.devices.DCAM.DCAM.DCAMCamera object at 0x00000140B70BDA90>

Test code

I wrote a pytest with multiprocessing to verify the behavior; cf. attached file.

test_dcam.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions