pypylon icon indicating copy to clipboard operation
pypylon copied to clipboard

Simple way to check if camera is connected

Open YacobBY opened this issue 11 months ago • 1 comments

Describe what you want to implement and what the issue & the steps to reproduce it are:

Hello I am looking for a simple way to check whether the camera is still connected. Using camera.GetDeviceInfo() methods does not appear to return errors when the camera is disconnected.

Is your camera operational in Basler pylon viewer on your platform

Yes

Hardware setup used

X86 pc

Camera(s) used

This code does not run because imports miss. But 'acA1920-25gm'

Runtime information:

python: 3.11.11 (main, Dec  4 2024, 08:55:07) [GCC 11.4.0]
platform: linux/x86_64/5.19.8-xanmod1
pypylon: 4.0.0 / 8.0.0.10

YacobBY avatar Feb 03 '25 11:02 YacobBY

Hi @YacobBY, if your camera should be "open", you can use:

camera.IsCameraDeviceRemoved()

Will be True if your camera is not connected anymore.

Or you access a node that reads a camera feature, the device info are static information on the host after discovering the camera.

So a script can look like:

import pypylon.pylon as py
import time

camera = py.InstantCamera(py.TlFactory.GetInstance().CreateFirstDevice())
camera.Open()

# approach 1 - using native function to check the connection
while not camera.IsCameraDeviceRemoved():
    time.sleep(1)

#-------------------------------------------------------------------

# approach 2 - access any feature that requires a connection
while True:
    try:
        camera.ExposureTime.Value
        time.sleep(1)
    except py.RuntimeException:
        # camera probably disconnected,
        # but definitely not accessible anymore
        break

Best regards

HighImp avatar Feb 03 '25 17:02 HighImp