Impact Acquire SDK Python
GenICamCallbackOnEvent.py

The GenICamCallbackOnEvent program is a simple example which illustrates how GenICam™ events can be used to inform an application about a certain event via a callback.

How it works:
  1. Open the device by calling
    pDev.open()
  2. Enable GenICam™ events.
  3. Attach a custom callback to the ExposureEndTimestamp property that gets called whenever a property is modified.
  4. Start the image acquisition in order to cause the callbacks to get triggered.

The full explanation regarding the callback usage can be found at the chapter Callbacks Triggered By GenICam™ Events .

Source code
1import os, platform, sys
2# import all the stuff from our SDK into the current scope
3from mvIMPACT import acquire
4# import all the helper functions for the examples using our SDK such as 'conditionalSetProperty' into the current scope
5# If you want to use this module in your code feel free to do so but make sure the 'Common' folder resides in a sub-folder of your project then
6from mvIMPACT.Common import exampleHelper
7
8#------------------------------------------------------------------
9# Event Callback implementation
10#------------------------------------------------------------------
11class EventCallback(acquire.ComponentCallback):
12 def __init__(self, pUserData):
13 acquire.ComponentCallback.__init__(self)
14 self.pUserData_ = pUserData
15
16 def execute(self, c, pUserData):
17 try:
18 # re-generating the object/data previously attached to the callback object. This could now be used to call a certain member function e.g. to update a class instance about this event!
19 ec = self.pUserData_
20 if( c.isProp ):
21 p = acquire.Property(c.hObj())
22 print("Component " + c.name() + " has changed. Its current value: " + p.readS() + "us. FrameID is: " + ec.eventExposureEndFrameID.readS())
23 except Exception as e:
24 print("An exception has been raised by code that is not supposed to raise one: '" + str(e) + "'! If this is NOT handled here the application will crash as this Python exception instance will be returned back into the native code that fired the callback!")
25
26devMgr = acquire.DeviceManager()
27pDev = exampleHelper.getDeviceFromUserInput(devMgr)
28if pDev == None:
29 exampleHelper.requestENTERFromUser()
30 sys.exit(-1)
31pDev.open()
32
33print("Please enter the number of buffers to capture followed by [ENTER]: ", end='')
34framesToCapture = exampleHelper.getNumberFromUser()
35if framesToCapture < 1:
36 print("Invalid input! Please capture at least one image")
37 sys.exit(-1)
38
39# The mvDisplay library is only available on Windows systems for now
40isDisplayModuleAvailable = platform.system() == "Windows"
41if isDisplayModuleAvailable:
42 display = acquire.ImageDisplayWindow("A window created from Python")
43else:
44 print("The display library of this SDK is not available on this('" + platform.system() + "') system. Consider using the PIL(Python Image Library) and numpy(Numerical Python) packages instead. Have a look at the source code of the ContinuousCapture example to get an idea how.")
45
46fi = acquire.FunctionInterface(pDev)
47
48while fi.imageRequestSingle() == acquire.DMR_NO_ERROR:
49 print("Buffer queued")
50pPreviousRequest = None
51
52# enable GenICam events
53ec = acquire.EventControl(pDev)
54ec.eventSelector.writeS("ExposureEnd")
55ec.eventNotification.writeS("On")
56
57# register a callback to eventExposureEndTimestamp
58eventCallback = EventCallback(ec)
59eventCallback.registerComponent(ec.eventExposureEndTimestamp)
60
61exampleHelper.manuallyStartAcquisitionIfNeeded(pDev, fi)
62for i in range(framesToCapture):
63 requestNr = fi.imageRequestWaitFor(10000)
64 if fi.isRequestNrValid(requestNr):
65 pRequest = fi.getRequest(requestNr)
66 if pRequest.isOK:
67 if isDisplayModuleAvailable:
68 display.GetImageDisplay().SetImage(pRequest)
69 display.GetImageDisplay().Update()
70 if pPreviousRequest != None:
71 pPreviousRequest.unlock()
72 pPreviousRequest = pRequest
73 fi.imageRequestSingle()
74 else:
75 # Please note that slow systems or interface technologies in combination with high resolution sensors
76 # might need more time to transmit an image than the timeout value which has been passed to imageRequestWaitFor().
77 # If this is the case simply wait multiple times OR increase the timeout(not recommended as usually not necessary
78 # and potentially makes the capture thread less responsive) and rebuild this application.
79 # Once the device is configured for triggered image acquisition and the timeout elapsed before
80 # the device has been triggered this might happen as well.
81 # The return code would be -2119(DEV_WAIT_FOR_REQUEST_FAILED) in that case, the documentation will provide
82 # additional information under TDMR_ERROR in the interface reference.
83 # If waiting with an infinite timeout(-1) it will be necessary to call 'imageRequestReset' from another thread
84 # to force 'imageRequestWaitFor' to return when no data is coming from the device/can be captured.
85 print("imageRequestWaitFor failed (" + str(requestNr) + ", " + acquire.ImpactAcquireException.getErrorCodeAsString(requestNr) + ")")
86exampleHelper.manuallyStopAcquisitionIfNeeded(pDev, fi)
87eventCallback.unregisterComponent( ec.eventExposureEndTimestamp)
88exampleHelper.requestENTERFromUser()
Definition Common/__init__.py:1