Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading in multiple pulse inputs with NI DAQmx in Python

Solved!
Go to solution

Hi everyone,

 

I am trying to read in multiple pulse inputs simultaneously using the nidaqmx library in Python, and I'm having some trouble. My physical setup consists of the following:

DAQ: NI-cDAQ-9178

Module: NI-9411 (Digital Input)

I know there isn't a lot of great documentation for using the nidaqmx library with python but I don't believe I'm attempting anything too complicated and I'm hopeful that someone can identify the error in my ways. For context, I'm essentially trying to use the NI-9411 as a counter for continuous edge detection. I've been able to successfully create a task that reads in pulses from an external signal generator. The problem however comes when I try to create multiple tasks (i.e. read from multiple channels). I have attempted to configure a separate task for each of the four channels and have used the add_ci_count_edges_chan() method to configure each task for edge counting. I am then attempting to continuously read the counts from each channel in a loop using the ci_count property. The result is that Channels 1 and 2 seem to work fine and read their corresponding pulses, however 3, and 4 just seem to be duplicating the read from channel 2. Here is an example of my code:

import nidaqmx
import nidaqmx.system
from nidaqmx.constants import Edge
import time


system = nidaqmx.system.System.local()

 

# ci Channel names where system.devices[2] = cDAQ1Mod2
cDaq = system.devices[2].name
ci_chan1 = system.devices[2].ci_physical_chans[0].name
ci_chan2 = system.devices[2].ci_physical_chans[1].name
ci_chan3 = system.devices[2].ci_physical_chans[2].name
ci_chan4 = system.devices[2].ci_physical_chans[3].name

# Define the input channels and tasks
task1 = nidaqmx.Task("task1")
# Configure the task for edge counting
task1.ci_channels.add_ci_count_edges_chan(counter=ci_chan1,
name_to_assign_to_channel="",
edge=nidaqmx.constants.Edge.RISING,
initial_count=0)

task2 = nidaqmx.Task("task2")
# Configure the task for edge counting
task2.ci_channels.add_ci_count_edges_chan(counter=ci_chan2,
name_to_assign_to_channel="",
edge=nidaqmx.constants.Edge.RISING,
initial_count=0)

task3 = nidaqmx.Task("task3")
# Configure the task for edge counting
task3.ci_channels.add_ci_count_edges_chan(counter=ci_chan3,
name_to_assign_to_channel="",
edge=nidaqmx.constants.Edge.RISING,
initial_count=0)

task4 = nidaqmx.Task("task4")
# Configure the task for edge counting
task4.ci_channels.add_ci_count_edges_chan(counter=ci_chan4,
name_to_assign_to_channel="",
edge=nidaqmx.constants.Edge.RISING,
initial_count=0)

# Start the task
task1.start()
task2.start()
task3.start()
task4.start()
try:
while True:
count1 = task1.ci_channels[0].ci_count
count2 = task2.ci_channels[0].ci_count
count3 = task3.ci_channels[0].ci_count
count4 = task4.ci_channels[0].ci_count
print("Count 1: {}, Count 2: {}, Count 3: {}, Count 4: {}".format(count1, count2, count3, count4))
time.sleep(1)

except KeyboardInterrupt:
pass

finally:
# Stop and clear the tasks
task1.stop()
task1.close()
task2.stop()
task2.close()
task3.stop()
task3.close()
task4.stop()
task4.close()

 

Any help or advice would be greatly appreciated. Thanks in advance!

0 Kudos
Message 1 of 2
(2,020 Views)
Solution
Accepted by topic author Hedrick

It turns out my problem was not with the code but instead my physical setup. I failed to use pull-up/pull-down resistors for my setup and therefore my readings were getting synced to different channels. I don't have a great background in electronics so I apologize if I don't get the technical explanation exactly correct. If I had read the specifications for the 9411, I would've realized that this module doesn't have channel-channel isolation. Regardless, I hope this post can be used as a basic template for reading in multiple pulse signals using daqmx and python.

0 Kudos
Message 2 of 2
(1,982 Views)