Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reopen connection after process termination #812

Open
Kukuevm opened this issue Jan 21, 2025 · 3 comments
Open

Reopen connection after process termination #812

Kukuevm opened this issue Jan 21, 2025 · 3 comments

Comments

@Kukuevm
Copy link

Kukuevm commented Jan 21, 2025

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

I am looking for a way to reopen connection when the program is closed or crashes without closing the camera session.

The following code establishes the connection every second run if the previous one ended up with an error "The device is controlled by another application. "0xE1018006""

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)

However, I would like to make the program to do a few re-connection attempts like shown below:

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())

status = False
while True:
    try:
        if status == False:
            camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly) 
            status = True
    except Exception:
        time.sleep(4) # I assumed that after 3 seconds the connection should be closed by the camera but it is not
        continue

But the code above always fails to exception.

There seems to be a solution for C++ called PylonGigEConnectionGuard to resolve this particular issue but I cannot find any alternatives for Python.

Is your camera operational in Basler pylon viewer on your platform

Yes

Hardware setup used

Windows 10 x64

Camera(s) used

BaslerCamera a2A 1920-51gcPRO (40429581)
p=vg4p_imx392c_pro/s=r/v=2.4.2/i=8208.11/h=02e36ab

Runtime information:

python: 3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 15:03:56) [MSC v.1929 64 bit (AMD64)]
platform: win32/AMD64/10
pypylon: 4.0.0 / 8.0.0.10
@thiesmoeller
Copy link
Collaborator

The PylonGigEConnectionGuard is only used for debugging.

Because when you are stopped in a breakpoint, the application can't service the GigEVision Heartbeat anymore, and the camera would break the connection on it's own, because it falsely assumes that the application is crashed
This is where the PylonGigEConnectionGuard takes over then.

What you describe is more complex to solve, because the state of your application in the interaction with the camera has to be recovered.

The error you show comes from the fact, that if you kill the application ( without proper shutdown) the camera has still an open connection to an application
The timeout for this is configured here: https://docs.baslerweb.com/transport-layer-parameters#heartbeat-timeout

So you'll get this error when you try to reopen the connection before the heartbeat-timeout has passed.

As a recommendation for this scenario:
take care to have proper exception handling in your application, that allows to properly close the camera connection, even in the case of errors.
This will allow you to directly reopen it, after application restart

@Kukuevm
Copy link
Author

Kukuevm commented Feb 8, 2025

The Heartbeat Timeout is set to 3 seconds but the script still cannot re-connect after this time when a "try" cycle is running.
The only options to open the connection again are either to power cycle the camera or re-run the script with the following two lines twice.

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)

What I cannot get is why re-running these two lines in a "try" cycle (even with a 5-second timeout) does not reset the connection but re-running the script does.

Another interesting thing is if the script is killed by a task manager, the connection can be opened right away even though it has not been properly closed. So the problem occurs only when the script crashes.

@thiesmoeller
Copy link
Collaborator

It is the inverse that you have to do.
The heartbeat timeout defines the time the camera will keep a connection open without getting any requests from your application.

If it works with Closing and "killing" your application: then the destructors of the instantcamera are still properly called and the connection is properly closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants