Handle GigEVision events in code:

:exclamation:EDIT: You may also want to read this excellent introduction to GEV events with the C-Style API and this example for the CVB++ API :nerd_face:

First of all you should check if the GigEVision events of your camera actually work. For that, you can use the GenICam Browser. In this example I be using a Genie Nano camera (M4020):

grafik

Simply Activate the events you would like to fire and make sure that they work, by having a look at the Logging view of the GenICam Browser:

grafik

For my camera, the “Start of Exposure” event has the ID 40000 and, as the name implies, gets fired for every frame, when the exposure starts.
Now you can read the events from your software (CVB.Net, C#):

var device = DeviceFactory.Open("GenICam.vin");
var nodeMap = device.NodeMaps["Device"];
var eventSelectorNode = nodeMap["EventSelector"] as EnumerationNode;
var notificationNode = nodeMap["EventNotification"] as EnumerationNode;

eventSelectorNode.Value = "ExposureStart";
notificationNode.Value = "On";
device.Notify[1003].Event += INotify1003_Triggered;

After getting nodemap and nodes, we activate the events of the camera, just as we did before in the GenICam Browser. Then, via the INotify interface, the event handler method INotify1003_Triggered can subscribe to the event #1003. This is the ID of the Device event used for the custom events of the camera.

The handler can be implemented as follows:

private void INotify1003_Triggered(object sender, NotifyEventArgs e)
{
  if (e.Value is byte[] data && data.Length >= Marshal.SizeOf(typeof(GevEventData)))
  {
    unsafe
    {
      fixed (byte* pdata = data)
      {
        var eventData = *(GevEventData*)pdata;
        byte[] eventIdBytes = BitConverter.GetBytes(eventData.ID);
        Array.Reverse(eventIdBytes); // endiannes swap
        ushort eventID = BitConverter.ToUInt16(eventIdBytes, 0);
        Console.WriteLine("Event fired: " + eventID);
      }
    }
  }
}

The GevEventData struct hast the following layout:

[StructLayout(LayoutKind.Sequential, Pack = 2)]
struct GevEventData
{
  private readonly UInt16 Reserved;
  public readonly UInt16 ID;
  public readonly UInt64 Timestamp;
}

The ID now indicates which event the camera fired. In this example it’s ID 40000 for exposure start. This is important if you want to react to multiple events!

Cheers
Thomas

1 Like