06-04-2024 03:38 AM
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]]
06-07-2024 09:09 AM
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)