We use these lines of code to connect to a camera and receive images:
string driverString = Environment.ExpandEnvironmentVariables("%CVB%") + @"\Drivers\GenICam.vin";
var device = DeviceFactory.OpenPort(driverString, 0);
var stream = device.Stream;
if (!stream.IsRunning)
{
stream.Start();
}
using var image = _device.Stream.WaitFor(timeSpan, out var status);
The problem is if we disconnect the camera network and connect it again we get
The device object can´t handle a change of the connection state automatically.
This need to be done within the application.
In our documentation we call this Connection Monitoring. See it here in the GenIcam User Guide: GenICam User Guide | Connection Monitoring
The example code in the GenICam User Guide is based on the old Style C-API.
Here you can see how easy it is to accomplish the handling of the change of the connection state with the CVB.NET API:
if (device.ConnectionState != Stemmer.Cvb.ConnectionState.NotSupported)
{
device.ConnectionStateChanged += (Object o, Stemmer.Cvb.ConnectionStateChangeEventArgs eventArgs) =>
{
if (eventArgs.NewState == Stemmer.Cvb.ConnectionState.Connected)
MessageBox.Show("Device reconnected");
else
MessageBox.Show("Device disconnected");
};
}
Within the Event Handler you can do things you need to do in your application depending on the new connection state.
For Example stop the acquisition after a disconnect and restart the acquisition after a reconnect.
Also try to update to the latest CVB Version. CVB 13.4.4
If you want to use the GevServer on the same machine as your acquisition code (very likely) make sure to use the SocketDriver Option on the GevServer and when configuring the device or discovering it in code also make sure to work on the SocketDriver.
If your code fails with the GevServer as well, we need a small running example of what you are trying to do.
With this we can start debugging further into the problem.
@Fatemeh,
Do you still have the issue that no event is fired when the camera disconnects.
From your example code, i can see that you used port 0 for your tests.
We found out that there is indeed a bug in the GenIcam.vin driver regarding our “Connection Monitoring” mechanism. But not if port 0 is used. This bug is related to port 1 and higher.
This is already fixed by our development team and will be part of the next CVB release.
CVB 13.04.005
We get the right status in ConnectionStateChanged when we get disconnected and connected again, but the device and the stream can not be used in any way because the whole program will crash.
We get an exception if we try to Stop, Abort, TryStop, TryAbort the stream or Dispose the camera device object.
If we try to establish a new connection we get an exception aslo.
Should be also noted that even if we use Common Vision Blox management console and start grabbing (check the grab checkbox) and then disconnect the camera network and reconnect it, the program hangs (not responding mode).
Hi @Fatemeh
The Management Console does not use the Connection Monitoring and can’t detect a disconnected camera. Anyway, it is not normal that the Management Console is not responding in such a situation.
We can’t reproduce your issue. Maybe we could reproduce it, if we get more information. @Chris already recommended updating to the current CVB version. Did you do that?
Did you try it with the GevServer?
Could we get an example application with which we can try to reproduce the issue?
In your code you called the CameraConnection.Dispose() method with the camera_ConnectionStateChanged Event and from your Worker.ExecuteAsync which is monitoring the camera Connection state.
Seems to be a complicated approach for a simple task.
When I do not call the CameraConnection.Dispose() method from the ConenctionStateChangedEvent your test application works with my GevServer Device even when the application detects a reconnect.
And this only works as you load the driver everytime when you establish a connection to the camera after the disconnect as you call Dispose() on the Device.Stream and the Device it self.
This is not necessary. You can stop/abort the stream after a disconnect and start it again when a reconnect was detected. You do not need to reload the driver.
We need this approach because we need the cameras be always up and running even in case of any disconnection - connection
I removed CameraConnection.Dispose() from amera_ConnectionStateChanged Event in my code and still after disconnection my program crashes with this message :
CvbGrabberSample\bin\Debug\net5.0\Isolated.exe (process 10096) exited with code -1073740791.
I tried to dispose stream and device because when I use stream.TryAbort(), stream.Abort(), stream.TryStop() or stream.Stop my program crashes so I thought maybe disposing them will help, which did not.
Could you please share the code you made with GEVServer? As I said I could not use it.
There is no code to be changed for the GevServer. The GevServer Tool is part of our Full CVB installation and you can use the CSGevServer Example to create a server:
%CVB%\Tutorial\GEVServer\CSharp\CSGEVServer\bin\Release\CSGEVServer.exe
I should mention that i changed the target framework to 4.8. Even if I think this should not make a difference at least this is what I changed as well besides removing the call to your Dispose() method.