Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading multiple distinct samples from a single channel line on python

Hello. I am new to NI. I have 4 channels and I am writing 2 values per channel and trying to find a way to read those 2 values for every channel on PYTHON. But instead, I am only reading the first element in the list-in-a-list for every channel. Hence the 2nd element isn't output'd. May I know how should fix this?


On a separate question, how should I conduct a test to check the quickest time for the machine to read/write values? I would like verify against the specification sheet (I have a NI USB 6289)

Code:

```

import nidaqmx
from nidaqmx.constants import TerminalConfiguration

# Create a task for the analog output channels
task= nidaqmx.Task()
task.ao_channels.add_ao_voltage_chan("Dev1/ao0", "")
task.ao_channels.add_ao_voltage_chan("Dev1/ao1", "")
task.ao_channels.add_ao_voltage_chan("Dev1/ao2", "")
task.ao_channels.add_ao_voltage_chan("Dev1/ao3", "")
task.start()

nested_list = [[2,3], [4,5], [6,7],[10,8]]
task.write(nested_list,2)

# Create a separate task for the analog input channels
task1= nidaqmx.Task()
# Note the writting style. It is important.
task1.ai_channels.add_ai_voltage_chan("Dev1/ai0","",terminal_config=TerminalConfiguration.RSE)
task1.ai_channels.add_ai_voltage_chan("Dev1/ai1", "",terminal_config=TerminalConfiguration.RSE)
task1.ai_channels.add_ai_voltage_chan("Dev1/ai2", "",TerminalConfiguration.RSE,-10,10)
task1.ai_channels.add_ai_voltage_chan("Dev1/ai3", "",terminal_config=TerminalConfiguration.RSE,
                                      min_val=-10, max_val=10)
task1.start()

value1 = task1.read(number_of_samples_per_channel=1)
print(value1)

task.stop()
task.close()
task1.stop()
task1.close()
```

Output: [[2.99981258877711], [4.999941758700331], [7.000392949796307], [8.000182112088945]]

0 Kudos
Message 1 of 2
(298 Views)
Hello!

The device cannot write 2 values to the same channel at the same time. You need to write the values, then read the values, and repeat.

For instance, you will need to write the first 4 values to the 4 AO channels, then read the values. After that, write the last 4 values to the 4 AO channels and then read again. The output will be 2 lists of your read values- you can combine these into one if you want. If you use the “with” statement, you don’t need to stop and close the tasks.

For code timing, you can check here.

import nidaqmx
from nidaqmx.constants import TerminalConfiguration

# write output voltage to each channel
# you can't combine multiple writes in one task

with nidaqmx.Task() as task:  
    task.ao_channels.add_ao_voltage_chan("Dev1/ao0")
    task.write(2)
with nidaqmx.Task() as task: 
    task.ao_channels.add_ao_voltage_chan("Dev1/ao1")
    task.write(3)
with nidaqmx.Task() as task:  
    task.ao_channels.add_ao_voltage_chan("Dev1/ao3")
    task.write(4)
with nidaqmx.Task() as task: 
    task.ao_channels.add_ao_voltage_chan("Dev1/ao4")
    task.write(5)

# read AI channels - outputs to list1
# you can combine multiple reads in one task 

with nidaqmx.Task() as task:
    task.ai_channels.add_ai_voltage_chan("Dev1/ai0",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    task.ai_channels.add_ai_voltage_chan("Dev1/ai1",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    task.ai_channels.add_ai_voltage_chan("Dev1/ai2",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    task.ai_channels.add_ai_voltage_chan("Dev1/ai3",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    list1 = task.read(number_of_samples_per_channel=1)

# repeat with remaining values 

with nidaqmx.Task() as task:  
    task.ao_channels.add_ao_voltage_chan("Dev1/ao0")
    task.write(6)
with nidaqmx.Task() as task: 
    task.ao_channels.add_ao_voltage_chan("Dev1/ao1")
    task.write(7)
with nidaqmx.Task() as task:  
    task.ao_channels.add_ao_voltage_chan("Dev1/ao3")
    task.write(8)
with nidaqmx.Task() as task: 
    task.ao_channels.add_ao_voltage_chan("Dev1/ao4")
    task.write(9)

# list2 is new values 

with nidaqmx.Task() as task:
    task.ai_channels.add_ai_voltage_chan("Dev1/ai0",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    task.ai_channels.add_ai_voltage_chan("Dev1/ai1",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    task.ai_channels.add_ai_voltage_chan("Dev1/ai2",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    task.ai_channels.add_ai_voltage_chan("Dev1/ai3",terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10)
    list2 = task.read(number_of_samples_per_channel=1)

print(list1, list2) ​
 
0 Kudos
Message 2 of 2
(272 Views)