I was writing a wrapper for Python mcculw and would like to call an instance method within an event callback. However, the python kernel dies whenever the instance method is called within the callback function. Part of the codes are listed below:
class aiBase:
def __init__(self, daqid, lowCh, highCh, **kwarg):
...
@ul.ULEventCallback
def endOfScanFcn(board_num, event_type, event_data, cvar):
#def endOfScanFcn(self, board_num, event_type, event_data, cvar): # doesn't work
'''
Call self.stop() after the scan finishes under non-continuous or foreground modes
'''
print('endOfScanFcn() is called')
self = ctypes.cast(cvar, ctypes.py_object).value
print('ctypes casting is done')
self.stop() # TODO - crash here
#self.stop(self) # doesn't work
print('Printed after returning from self.stop() at endOfScanFcn()')
def start(self): # AI start
...
# Set up event callback
ul.enable_event(self.daqid, EventType.ON_END_OF_AI_SCAN, 0, self.endOfScanFcn, ctypes.py_object(self))
...
I understand that I don't have to call self.stop() to do the cleanup for foreground acquisition since I can put everything in the "finally:" clause of "try-except-finally" instead. However, I just use this as an example to learn the correct way to pass instance to and call instance method within the event callback function.
The full test jupyter notebook is attached for your reference.
Question
TMWang
Hi,
I was writing a wrapper for Python mcculw and would like to call an instance method within an event callback. However, the python kernel dies whenever the instance method is called within the callback function. Part of the codes are listed below:
class aiBase:
def __init__(self, daqid, lowCh, highCh, **kwarg):
...
@ul.ULEventCallback
def endOfScanFcn(board_num, event_type, event_data, cvar):
#def endOfScanFcn(self, board_num, event_type, event_data, cvar): # doesn't work
'''
Call self.stop() after the scan finishes under non-continuous or foreground modes
'''
print('endOfScanFcn() is called')
self = ctypes.cast(cvar, ctypes.py_object).value
print('ctypes casting is done')
self.stop() # TODO - crash here
#self.stop(self) # doesn't work
print('Printed after returning from self.stop() at endOfScanFcn()')
def start(self): # AI start
...
# Set up event callback
ul.enable_event(self.daqid, EventType.ON_END_OF_AI_SCAN, 0, self.endOfScanFcn, ctypes.py_object(self))
...
def stop(self): # AI stop
print('self.stop() executed')
self.data = ctypes.cast(self.buffer, ctypes.POINTER(ctypes.c_ushort))
ul.disable_event(self.daqid, EventType.ALL_EVENT_TYPES)
ul.win_buf_free(self.buffer)
daq = aiBase(0,0,1)
daq.start()
I understand that I don't have to call self.stop() to do the cleanup for foreground acquisition since I can put everything in the "finally:" clause of "try-except-finally" instead. However, I just use this as an example to learn the correct way to pass instance to and call instance method within the event callback function.
The full test jupyter notebook is attached for your reference.
Thank you,
TM.
System info:
Hardware - USB-1608GX-2AO
OS: Win10
Python: 3.10.14
Jupyterlab: 3.5.3
mcculw: 1.0.0
test_eventCallback.ipynb
Link to comment
Share on other sites
0 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now