genicam / harvesters

Image Acquisition Library for GenICam-based Machine Vision System
Apache License 2.0
501 stars 86 forks source link

How to close camera? #362

Open MasIgor opened 1 year ago

MasIgor commented 1 year ago

Hi all!

I use this code to write some parameters to the camera, start it, do its thing, and then stop it:

        self.ia.remote_device.node_map.ExposureTime.value = 300
        self.ia.remote_device.node_map.Gain.value = 100
        self.ia.remote_device.node_map.BalanceRatioSelector.value = "Red"
        self.ia.remote_device.node_map.BalanceRatio.value = 1.35
        self.ia.remote_device.node_map.BalanceRatioSelector.value = "Green"
        self.ia.remote_device.node_map.BalanceRatio.value = 1
        self.ia.remote_device.node_map.BalanceRatioSelector.value = "Blue"
        self.ia.remote_device.node_map.BalanceRatio.value = 2.1
        self.ia.remote_device.node_map.TriggerMode.value = 'On'
        self.ia.remote_device.node_map.PixelFormat.value = 'BayerRG8'
        self.ia.start()

        print("camera is waiting")

        with self.ia.try_fetch(timeout=1) as buffer:
            print("camera triggered")

            ##... do some stuff here..

        self.ia.stop()

so far everything is working as expected. but when something happens (for example, the trigger does not arrive within the 1 second, or any other exception is thrown, then self.ia.stop() is not called anymore and the camera remains "live". in the next cycle then I get the error message -1009 stating that it is not possible to write the parameters. is there a way to force a stop after some time, or if the trigger does not arrive within 1 second?

The error:

GenTL exception: Parameter not valid or out of range. (Message from the source: Could not write data to 0x20000000(4 bytes). Status: GEV_STATUS_LOCAL_PROBLEM. ) (ID: -1009)

The parameter is not out of range. its just that the camera is in use and the data cannot be written.

thank you!

MathijsNL commented 1 year ago

From the tutorial: https://harvesters.readthedocs.io/en/latest/TUTORIAL.html

from genicam.gentl import TimeoutException

try:
    buffer = ia.fetch(timeout=3)
except TimeoutException as e:
    # oops, it's timed out but it may be reasonable
    # depending on the context.

I think this might solve the problem.