alliedvision / VimbaPython

Old Allied Vision Vimba Python API. The successor to this API is VmbPy
BSD 2-Clause "Simplified" License
93 stars 40 forks source link

Getting info on connection status #137

Closed bth5 closed 1 year ago

bth5 commented 1 year ago

I have a script where i use async frame acquisition and software trigger (action commands).

I'm trying to add a functionaly where i check the status of the camera connection. I would like to raise an error if the camera got disconnected somehow.

But so far what i've tried doesn't work: inside:

with vimba as vimba:
        with cam as cam:

cam.is_streaming doesn't change to false I tried with events, but i didn't get that to work either.

Are there a functionality to register and handle a disconnect?

BernardoLuck commented 1 year ago

Hello,

here is an example on how a camera change handler would work:

import vimba

Just used to delay our example a bit

import time

This function is called for every camera event that is detected. The callback must take two

parameters for VimbaPython. The first parameter is the Camera instance that changed, the second

parameter es the type of event that occurred. Events can be Detected, Reachable,

Unreachable, and Missing.

def camera_change_callback(cam, event): print(f'Executed user camera_change_callback: {cam}, {str(event)}')

Get the Vimba singleton to register our callback function and to list our available cameras

vmb = vimba.Vimba.get_instance()

This registers the change handler. To remove the callback, the

vmb.unregister_all_camera_change_handlers and vmb.unregister_camera_change_handler are available.

vmb.register_camera_change_handler(camera_change_callback)

with vmb: while True:

Simple infinite loop, that prints available access modes for all detected cameras. If the

    # access mode for our cameras changes or the number of detected cameras changes, the
    # registered callback will be executed automatically. We do not need to do anything here.
    cams = vmb.get_all_cameras()
    for cam in cams:
        print(f'Camera {cam}: {cam.get_permitted_access_modes()}')
    # Delay execution so we do not spam the output too much.
    time.sleep(1)
bth5 commented 1 year ago

I got it working with inspiration from your code, thank you very much!