Python and NI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to stop HW-timed task using external trigger

Hello, I am new using nidaqmx and I have the following question:

I am using PCIe-6320, and I want to make a hardware-timed acquisition task which stops when an external trigger is received. The code looks like this:

 

import numpy as np
import nidaqmx
from nidaqmx.constants import TerminalConfiguration, AcquisitionType

# I_PIN is the trigger for the HW-Timed analog input (A_SIG_PIN) 

# Definition of ports
A_SIG_PIN = "Dev1/ai0" #analog input
I_PIN = "/Dev1/PFI0" #trigger signal
#Note: "/" comes first because it's terminal
T6_PIN = "Dev1/port2/line4" #End of acquisition

# min pulse width for digital filter
min_pulse_width = 5.12e-6

def Init_Analog_Read(signal, trig):
    # Initialization of I-triggered analog read task
    task = nidaqmx.Task()
    task.ai_channels.add_ai_voltage_chan(signal,
       terminal_config=TerminalConfiguration.DIFFERENTIAL)
    task.timing.cfg_samp_clk_timing(10000,source=trig,
            sample_mode=AcquisitionType.HW_TIMED_SINGLE_POINT)
    task.triggers.start_trigger.cfg_dig_edge_start_trig(trig)
    # Digital filtering on both the clock and start triggers
    task.timing.samp_clk_dig_fltr_enable = True
    task.timing.samp_clk_dig_fltr_min_pulse_width = min_pulse_width

    task.triggers.start_trigger.dig_edge_dig_fltr_enable = True
    task.triggers.start_trigger.dig_edge_dig_fltr_min_pulse_width = min_pulse_width
    task.start()
    return task

def Init_Dig_Read(signal):
    task = nidaqmx.Task()
    task.di_channels.add_di_chan(signal)
    task.di_channels.di_dig_fltr_enable = True
    task.di_channels.di_dig_fltr_min_pulse_width = min_pulse_width
    task.start()
    return task

if __name__ == "__main__":
    task_read = Init_Analog_Read(A_SIG_PIN,I_PIN)
    task_monitor_T6 = Init_Dig_Read(T6_PIN)

    while True:
        state = task_monitor_T6.read()
        if state == True:
            print("###T6 has arrived###")
            task_monitor_T6.stop()
            task_read.stop()
            break

        v_value = task_read.read(1000)
        #export values to a file

    print("End of code")

 

It seems that the program remains at the line

v_value = task_read.read(1000)

waiting for the 1000 samples instead of receiving the trigger to stop acquisition (here: T6_PIN).

I have also even tried using threads and explicitely abborting the task on the main thread, but this doesn't seem the proper way to do so, since I it may happen that there are still values in the buffer.

Apparently, if I use event flags when using threads also does not solve the problem because the program still remains in the line v_value = task_read.read(1000)

 

0 Kudos
Message 1 of 1
(1,053 Views)