Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Create Digital Output using nidaqmx with Python

Solved!
Go to solution

Hello, I have issues trying to generate a digital output with clock sampling. I am using NI USB-6353 and basically I want to create a custom waveform, made by an array of 1 and 0, with very precise timing (I cannot use counters for a series of reasons).

 

This is pretty much my code:

 

wave = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], dtype=np.uint32)

task = nidaqmx.Task()
task.do_channels.add_do_chan("Dev1/port0/line0")
task.timing.cfg_samp_clk_timing(1000)
writer = DigitalSingleChannelWriter(task.out_stream, auto_start=True)
writer.write_many_sample_port_uint32(wave)

 

My code is perfectly working and I see the waveform on my oscilloscope if I don't use the "task.timing.cfg_samp_clk_timing(1000)" but that's not what I want, I want that samples are written at well defined intervals. If I use the timing configuration I don't see the waveform anymore but strange pulses, and they change if I change the clock timing from 1000 to other values. 

 

What am I missing? What exactly does this instruction: "task.timing.cfg_samp_clk_timing(1000)"? I thought it would have set 1000 samples in 1 second, am I wrong?

 

Please help me. Thanks a lot to anyone who answers.

0 Kudos
Message 1 of 5
(1,567 Views)

I don't do Python, but based on the syntax help found here, here's what I'd expect:

 

The function call task.timing.cfg_samp_clk_timing(1000) appears to explicitly set the sample rate to 1000 samples/sec.  All other timing parameters will use the default values shown at the link.  Of particular note are that the task will be configured for 1000 Finite Samples.  So 1000 samples at 1000 samples/sec means that you can expect the task to generate output for 1 second.  If you were to change *only* the sample rate to 100, you could expect your 1000 samples to take 10 seconds.  If you were to change it to 10000, you could expect them to take only 0.1 seconds.

 

You've also defined a 15 sample buffer that will keep repeating until those 1000 samples are done.  After starting the task, you'll get an initial 5 samples at low state.  After that, you'll keep repeating 5 cycles at high state followed by 10 cycles at low state (due to buffer wraparound) until you reach 1000 samples total.

 

 

-Kevin P

 

 

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 2 of 5
(1,544 Views)

Hello Kevin, thanks for your kind reply.

 

What you say makes sense to me, and it is what I expect, so I tried another code, more specific, creating a waveform of 1000 samples and specifying the sample rate to 1000 and the number of samples to 1000.

 

wave_one = np.ones(500, dtype="uint32")
wave_zero = np.zeros(250, dtype="uint32")
wave = np.append(wave_zero, wave_one)
wave = np.append(wave, wave_zero) 

 

task = nidaqmx.Task()
task.do_channels.add_do_chan("/Dev1/port0/line0")
task.timing.cfg_samp_clk_timing(1000, sample_mode=AcquisitionType.FINITE, samps_per_chan=1000)
writer = DigitalSingleChannelWriter(task.out_stream, auto_start=True)
print(writer.write_many_sample_port_uint32(wave)) 

 

I expected to see a waveform of 1 second with a single pulse in the middle. Instead I can't see any pulse at all! How is it possible?

 

0 Kudos
Message 3 of 5
(1,519 Views)

Well, I have circumscribed the problem better. It seems to be a duration problem. I was using large arrays so at the beginning I was only able to see zeros.

 

Now I am using the following code, a waveform made by progressively larger impulses, using 45 samples: 

 

wave = np.array([0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0], dtype="uint32")


task = nidaqmx.Task()
task.do_channels.add_do_chan("/Dev1/port0/line0")
task.timing.cfg_samp_clk_timing(1000, sample_mode=AcquisitionType.FINITE, samps_per_chan=45)
writer = DigitalSingleChannelWriter(task.out_streamauto_start=True)
print(writer.write_many_sample_port_uint32(wave)) 

 

I was able to realize that the waveform is generated until the sixth impulse (see attachment "clock1000"), then is kind of truncated. Even if the DigitalSingleChannelWriter does not return any error. On the contrary it says that all the 45 samples have been written.

 

If I change the sampling frequency to 10000, I am able to see the complete waveform (see attachment "clock10000"), just because the total duration is lowered!

 

What is happening here?

Download All
0 Kudos
Message 4 of 5
(1,505 Views)
Solution
Accepted by topic author Dome87

Ok, I found out. It was not a clock issue. The problem was the task ended before the writing process on the DigitalWriter was terminated. Thanks anyway.

0 Kudos
Message 5 of 5
(1,489 Views)