Search code examples
pythonlabviewnidaqmx

nidaqmx: Access an existing task


I am using the nidaqmx-python library for acquiring data. Is it possible to access an existing task, which is already defined in the NI MAX?


Solution

  • My solution, thanks to the tip from @nekomatic, is:

    import nidaqmx
    
    system = nidaqmx.system.System.local()  # load local system
    
    task_names = system.tasks.task_names  # returns a list of task names
    
    task = system.tasks[0]  # selected the first task
    loaded_task = task.load()  # load the task
    
    sent_samples = []  # list for saving acquired data
    
    with loaded_task:
        loaded_task.timing.cfg_samp_clk_timing(
            rate=2560, 
            sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS, 
            samps_per_chan=1000)
    
        def callback(task_handle, every_n_samples_event_type,
                     number_of_samples, callback_data):
            """
            Callback function/
            """
            print('Every N Samples callback invoked.')
    
            samples = loaded_task.read(number_of_samples_per_channel=2560)
            sent_samples.extend(samples)
    
            return 0
    
        loaded_task.register_every_n_samples_acquired_into_buffer_event(
            200, callback)
    
        loaded_task.start()
    
        input('Running task. Press Enter to stop.\n')