pythonnidaqmx

Why is my NIDAQ register_signal_event callback function not getting called?


I'm toggling inputs on an NI-USB6525 and trying to detect the change with a Python script.

I'm able to write the lines and read them successfully after the write, but I am not detecting the changes. I put in some prints to demonstrate I can set an input to True or False and then manually read the input and get the correct result. I need this script to automatically detect the changes.

import sys
import time
import nidaqmx
import logging

def readControlSimulator(niDaqDevice = "Dev1"):

# On NI-USB6501 all IOs are configurable.
# On NI-USB6525 port0 are outputs, port1 are inputs.
INPUT_PORT = 'port1'

    # Function for reading a line
def readLine(lineNumber):
    # Read the corresponding line
    with nidaqmx.Task() as line:
        try:
            line.di_channels.add_di_chan(niDaqDevice + '/' + INPUT_PORT + '/line' + str(lineNumber))
            logging.debug("HPT line {} = {}".format(lineNumber, line.read()))
        except nidaqmx.DaqError as e:
            logging.debug(str(e))

# read command line arguments
for arg in sys.argv[1:]:
    niDaqDevice = arg

logging.basicConfig(format="%(asctime)15s - %(levelname)8s: %(message)s", level=logging.DEBUG)

for line in range(8):
    readLine(line)

# Set up NIDAQ line detection for all lines
with nidaqmx.Task() as task:        
    # Will only work if using a compatible device.
    try:
        logging.debug("SET UP CHANGE DETECTION")
        task.di_channels.add_di_chan(niDaqDevice + '/' + INPUT_PORT + '/line0:7')
        task.timing.cfg_change_detection_timing(rising_edge_chan = niDaqDevice + '/' + INPUT_PORT + '/line0:7',
                                                falling_edge_chan = niDaqDevice + '/' + INPUT_PORT + '/line0:7',
                                                sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS)

        def callback(task_handle=task._handle,
                     signal_type=nidaqmx.constants.Signal.CHANGE_DETECTION_EVENT,
                     callback_data=1):
            logging.debug("task_handle {}".format(task_handle))
            logging.debug("signal_type {}".format(signal_type))
            logging.debug("callback_data {}".format(callback_data))
            logging.debug("CHANGE LINE")
            return 0

        task.register_signal_event(nidaqmx.constants.Signal.CHANGE_DETECTION_EVENT, callback)
        task.start()
    except nidaqmx.DaqError as e:
        print(str(e))

time.sleep(10)
for line in range(8):
    readLine(line)
return

if __name__ == '__main__':
    readControlSimulator()

The code should print "CHANGE LINE" if a line changes, but it doesn't. Here is what I get when I'm toggling the switches while I run the code:

$py src/read_controlSimulator.py
2019-06-25 09:53:58,757 - DEBUG: HPT line 0 = True
2019-06-25 09:53:58,760 - DEBUG: HPT line 1 = True
2019-06-25 09:53:58,766 - DEBUG: HPT line 2 = True
2019-06-25 09:53:58,772 - DEBUG: HPT line 3 = False
2019-06-25 09:53:58,777 - DEBUG: HPT line 4 = False
2019-06-25 09:53:58,781 - DEBUG: HPT line 5 = False
2019-06-25 09:53:58,786 - DEBUG: HPT line 6 = False
2019-06-25 09:53:58,791 - DEBUG: HPT line 7 = False
2019-06-25 09:53:58,792 - DEBUG: SET UP CHANGE DETECTION
2019-06-25 09:54:08,838 - DEBUG: HPT line 0 = False
2019-06-25 09:54:08,842 - DEBUG: HPT line 1 = False
2019-06-25 09:54:08,847 - DEBUG: HPT line 2 = True
2019-06-25 09:54:08,852 - DEBUG: HPT line 3 = True
2019-06-25 09:54:08,856 - DEBUG: HPT line 4 = False
2019-06-25 09:54:08,860 - DEBUG: HPT line 5 = False
2019-06-25 09:54:08,865 - DEBUG: HPT line 6 = False
2019-06-25 09:54:08,871 - DEBUG: HPT line 7 = False

So when I explicitly do reads I get the correct values, however the changes aren't getting detected as I expect...


Solution

  • I haven't use the Python bindings for NI-DAQmx, but

    with nidaqmx.Task() as task:

    looks like it would stop and clear the task immediately after starting it because the program flow leaves the with block and Task.__exit__() executes. When you run it, does that script wait indefinitely or exit quickly?