nidaqmx co_channels can't write sample

2020-08-07 05:05发布

问题:

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!

回答1:

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)