Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Simultaneous Read and Write on a PXIe-6124

Hey. I've been trying to write a NI-DAQmx program in Python that would read input from two analog channels and simultaneously output a continuous sine wave. The acquisitions and output of the continuous sine wave would be triggered by the same trigger, which is one of the analog input channels. This is on a PXIe-6124. The sample rate is at 4 MHz and we need to read 4194304 points.

 

Given, I'm not exactly asking for a code-based answer. I'm just curious if it's actually possible to do this with that card. The specs say 4 MS/s per channel for Analog Input, and 4 MS/s for Analog Output if only one channel. So I don't really see a reason for it to not allow this to work unless I am missing something seemingly obvious.

 

I've tried a few simpler variations of this and have just not been able to get anything going with it regarding simultaneous input and output consistently. I have been able to successfully trigger reading input as well as output some data. However, when the data outputs, it almost immediately stops the moment a read is called. It's almost as if the continuous output of an AnalogSingleChannelWriter just stops the moment another task performs a read. I have tried to make a separate thread that throws the write into a while loop, but that leads to a sine wave with obvious gaps in it like shown here:

 

SDS00036.png

 

The only other way I can think of is to literally generate output data equal to the size of what needs to be read, and then send that to "ao0". But then that gives an onboard device memory overflow.

 

A simpler example including "ai0" as main data in, "ai1" being for trigger", and "ao0" being for continuous output is shown below:

 

 

import nidaqmx
import math
import numpy as np
from nidaqmx.stream_writers import AnalogSingleChannelWriter
from nidaqmx.stream_readers import AnalogMultiChannelReader

# Initialising task
task_in  = nidaqmx.Task()
task_out = nidaqmx.Task()

# Configure task IN
task_in.ai_channels.add_ai_voltage_chan("PXI1Slot2/ai0")
task_in.ai_channels.add_ai_voltage_chan("PXI1Slot2/ai1")
# task_in.ai_channels.add_ai_voltage_chan("PXI1Slot2/ai2") # Loopback of ao0

# Configure task OUT
task_out.ao_channels.add_ao_voltage_chan("PXI1Slot2/ao0")

samples = 4000000

# Set sample rate and source to sync
for task in (task_in, task_out):
	task.timing.cfg_samp_clk_timing(
		rate = samples,
		source = 'OnboardClock',
		sample_mode = nidaqmx.constants.AcquisitionType.CONTINUOUS
	)

#
# INPUT: Assign an analog trigger that fires upon ai1 rising past 1.0
#

task_in.triggers.start_trigger.cfg_anlg_edge_start_trig(
	trigger_source = "PXI1Slot2/ai1",
	trigger_level = 1
)

#
# OUTPUT: Assign a digital trigger that fires the moment aforementioned trigger
#         goes off
#

task_out.triggers.start_trigger.cfg_dig_edge_start_trig(
	trigger_source = "/PXI1Slot2/AnalogComparisonEvent"
)

# Committing task reduces lag between read and write
for task in (task_in, task_out):
	task.control(nidaqmx.constants.TaskMode.TASK_COMMIT)

# Reader/Writer Initialise
reader = AnalogMultiChannelReader (task_in .in_stream )
writer = AnalogSingleChannelWriter(task_out.out_stream, auto_start = False)

# Generate some array of stuff I guess
arr_w = np.linspace(1.0, 5.0, 5000, dtype=np.float64)
arr_r = np.zeros((task_in.in_stream.num_chans, samples), dtype=np.float64)

task_in.start()
task_out.start()

# Write forever (after triggered)
writer.write_many_sample(
	arr_w,
	timeout = nidaqmx.constants.WAIT_INFINITELY
)

while True:
	# Read "samples" amount of points
	reader.read_many_sample(
		arr_r,
		timeout = nidaqmx.constants.WAIT_INFINITELY,
		number_of_samples_per_channel = samples
	)
	print(arr_r[0])

 

 

 

0 Kudos
Message 1 of 2
(788 Views)

I don't really know Python, but think I can follow well enough to suggest 2 things:

 

1. write to your AO task before starting it.  That's the normal order for buffered tasks

2. start your AO task before starting your AI task (so it's already armed and waiting for the Analog Comparison Event before you start the AI task that can cause it to assert)

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 2 of 2
(658 Views)