0

I try to use Ni-Daq to generate pulse. the example provided by the nidaqmx is like as follows:

    import nidaqmx
from nidaqmx.types import CtrTime


with nidaqmx.Task() as task:
    task.co_channels.add_co_pulse_chan_time("Dev1/ctr0")

    sample = CtrTime(high_time=0.001, low_time=0.002)

    print('1 Channel 1 Sample Write: ')
    print(task.write(sample))

But after I run this script, it generate some errors as shown below:

raise DaqError(error_buffer.value.decode("utf-8"), error_code) DaqError: The task is not buffered or has no channels. If the task is not buffered, use the scalar version of this function. If the task has no channels, add one to the task. Task Name: _unnamedTask<0>

Status Code: -201395

what cause the problem? how to fix that?

Thank you very much!

Forrest
  • 51
  • 1
  • 9

1 Answers1

0

The Python examples for NI-DAQmx are tuned for NI's X Series devices, the 63xx models. The 62xx models are M Series devices, and their counters are more picky about how you program them. In short: the pulse specification must be given when the channel is created, and cannot be given later. X Series devices do not have this restriction.

Configuring the shape of the pulse

Instead of

task.co_channels.add_co_pulse_chan_time("Dev1/ctr0")

try

# https://github.com/ni/nidaqmx-python/blob/master/nidaqmx/_task_modules/co_channel_collection.py#L160
task.co_channels.add_co_pulse_chan_time(
    counter="Dev1/ctr0",
    low_time=0.002,
    high_time=0.001)

And because you specified the pulse in this way, you don't need to write() to the channel anymore. When you want to start the pulse train, just use

task.start()

Configuring the length of the pulse train

When you generate a pulse train, you can tell the driver to emit a finite number of pulses or a continuous square wave.

Before you start() the task, use cfg_implicit_timing(). This snippet generates 1200 pulses:

# https://github.com/ni/nidaqmx-python/blob/master/nidaqmx/_task_modules/timing.py#L2878
pulse_count = 1200;
task.cfg_implicit_timing(
    sample_mode=AcquisitionType.FINITE,
    samps_per_chan=pulse_count)
Joe Friedrichsen
  • 1,976
  • 14
  • 14
  • Thank you very much for the answer. I will try soon. In this case, is it possible to define how many pulse generated? – Forrest Apr 23 '18 at 17:16
  • Sure, I've updated my answer to include that aspect as well. – Joe Friedrichsen Apr 23 '18 at 17:31
  • Hi Joe, the code works. Thank you very much. now. how to do a loop of this pulse train? `for i in range(5): task.start()` – Forrest Apr 24 '18 at 04:47
  • the code 'for i in range(5): task.start()' try to generate pulse_count 5 times, but this code doesn't work. – Forrest Apr 24 '18 at 04:54
  • NI calls the behavior you're describing "retriggerable". You can either (1) create, configure, run, and clear the task each iteration; or (2) use an external signal, configure the start trigger on that pin, and enable its retriggerable property `task.triggers.start_trigger.cfg_dig_edge_start_trig("/Dev1/PFI9"); task.triggers.start_trigger.retriggerable = True;` – Joe Friedrichsen Apr 24 '18 at 14:54
  • Kinda tacky, but if this works for you, would you mind accepting this answer? – Joe Friedrichsen May 03 '18 at 18:53