ni / nidaqmx-python

A Python API for interacting with NI-DAQmx
Other
444 stars 159 forks source link

`Task.write` does not handle certain inputs as expected #126

Open stafak opened 3 years ago

stafak commented 3 years ago

Hi!

Running this code, I expect to write the signal to line 2, but instead I get a strange error message about mismatching number of channels (even though there is no mismatch):

import nidaqmx

taskLen = 100
signalLine2 = [True] * taskLen
signals = [signalLine2]

with nidaqmx.Task('mytask') as task:
    task.do_channels.add_do_chan('Dev1/port0/line2')
    task.write(signals, auto_start=True)
    task.wait_until_done()

print('done')
nidaqmx.errors.DaqError: Write cannot be performed, because the number of channels in the data does not match the number of channels in the task.

When writing, supply data for all channels in the task. Alternatively, modify the task to contain the same number of channels as the data written.

Number of Channels in Task: 1
Number of Channels in Data: 1

Task Name: mytask

I'm aware that it's possible to avoid this by simply using signalLine2 as the input instead of the 2-dimensional signals variable, but sometimes you can end up in this situation when creating a varying number of signals in a loop.


Similarly, an error is raised when mixing Python lists and numpy arrays in the input:

import nidaqmx
import numpy as np

taskLen = 100
signalLine2 = np.ones(taskLen, dtype=bool)
signalLine3 = np.ones(taskLen, dtype=bool)
signals = [signalLine2, signalLine3]

with nidaqmx.Task('mytask') as task:
    task.do_channels.add_do_chan('Dev1/port0/line2')
    task.do_channels.add_do_chan('Dev1/port0/line3')
    task.write(signals, auto_start=True)
    task.wait_until_done()

print('done')
nidaqmx.errors.DaqError: Write failed, because this write method only accepts boolean samples when there is one digital line per channel in a task.

Requested sample type: <class 'numpy.ndarray'>

Task Name: mytask
stafak commented 3 years ago

I opened a pull request that resolves this: #127