Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Multichannel analog output Python NiDaqmx

Solved!
Go to solution

Hi,

 

I am trying to generate two analog output signals using the write function of the NiDaqmx package for python. However when passing a 2D array as output array I get the following error:

 

"

nidaqmx.errors.DaqError: Write cannot be performed because the NumPy array passed into this function is not shaped correctly. You must pass in a NumPy array of the correct number of dimensions based on the write method you use.

 

No. of dimensions of NumPy Array provided: 2

No. of dimensions of NumPy Array required: 1

"

 

In the example below outputX and outputY are two equally sized 1 dimensional arrays. I tried multiple ways of generating a 2D array but none of them seems to solve the error. If I compare the data type of this array it is the same as the one they use in the 

 

I already looked at the documentation but still haven't found a solution yet.

 

Help is much appreciated,

 

With kind regards,

 

Lex

 

 

outputData = np.append(outputX, outputY)
    outputData = outputData.reshape((2,outputX.size))
    outputData = np.transpose(outputData)
    print(outputData.shape)
    outputChannels = ao_chan_x + ", " + ao_chan_y
    with nidaqmx.Task() as writeTask, nidaqmx.Task() as readTask:  
            writeTask.ao_channels.add_ao_voltage_chan(outputChannels)
            readTask.ai_channels.add_ai_voltage_chan(ai_chan)
            
            writeTask.timing.cfg_samp_clk_timing(rate = writingRate,
                                                 sample_mode = nidaqmx.constants.AcquisitionType.FINITE,
                                                 samps_per_chan = outputX.size)
            readTask.timing.cfg_samp_clk_timing(rate = readingRate,
                                                sample_mode = nidaqmx.constants.AcquisitionType.FINITE,
                                                samps_per_chan = inputData.size)              
            writeTask.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source = '/Dev2/ai/StartTrigger') #Setting the trigger on the analog input
            
            reader = AnalogSingleChannelReader(readTask.in_stream)
            writer = AnalogSingleChannelWriter(writeTask.out_stream)
            print("The channels linked to WriteTask are: ")
            for i in writeTask.ao_channels:
                print(i)
                
            print("Starting writing and reading using", outputX.size, "and", inputData.size)
            estT = outputX.size/writingRate
            print("Estimated time: ", estT)
            writer.write_many_sample(outputData)
            
            writeTask.start()
            print("Start reading/writing")
            reader.read_many_sample(inputData, timeout = estT+5)   
            
            writeTask.wait_until_done(timeout = estT+5)
            readTask.wait_until_done(timeout = estT+5)
            print("Done with data")

 

0 Kudos
Message 1 of 2
(5,572 Views)
Solution
Accepted by topic author LHD

I don't know python, but I made note of the following 2 lines:

 

writer = AnalogSingleChannelWriter(writeTask.out_stream)
writer.write_many_sample(outputData)

 

When writing "many samples" to a "single channel" task, a 1D array would be appropriate.   It appears that elsewhere in the code you've constructed a 2D array *and* configured the task to contain 2 channels.   I suspect you need to construct a different "writer" object, maybe it's named something more like "AnalogMultiChannelWriter"?   Then *that* kind of writer would be prepared to accept the 2D array you've (appropriately) constructed to feed multiple samples to a 2 channel task.

 

 

-Kevin P

 

 

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 2 of 2
(5,550 Views)