Using PyDAQmx to read analog input yields constant values

2.6k views Asked by At

Hello fellow stackers,

Today I discovered a Python module called PyDAQmx, which I intend to use to read out the analog data channels from a National Instruments USB-6221 data acquisition box. I have used this box in conjunction with LabView, but I need to stream the data from LabView into a Python script, which is sort of a hassle. Installing the PyDAQmx module went like a charm, and the example code ran right away without any errors. However, when I inspect the collected data vector obtained from channel 1, I see one-thousand times 10.61799802, which is not exactly the output I was expecting. Investigating other channels gave further and further decreasing output, starting from around +10 V on channels 0, 1 and 2, down to -10 V on the highest numbered channels. Channels in between seem to yield just electronic noise.

I suspect I did not correctly set-up the code, since LabView does give me the correct values, but I have no clue where to look for (I have never worked with the DAQmx library before). Here is my code:

import numpy as np
from PyDAQmx import *

N_samples = 100
log_rate = 100.0

taskHandle = TaskHandle()
read = int32()
data = np.zeros((N_samples,), dtype=np.float64)

DAQmxCreateTask("", byref(taskHandle))
# I have an LVDT plugged into channel ai1 with range +/-10V
DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai1", "LVDT", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, None)
DAQmxCfgSampClkTiming(taskHandle, "", log_rate, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, N_samples)

DAQmxStartTask(taskHandle)
DAQmxReadAnalogF64(taskHandle, N_samples, 10.0, DAQmx_Val_GroupByChannel, data, N_samples, byref(read), None)

print data

Hopefully there is someone out there who can give me a hand with this. I can add more details on request. Thanks!

1

There are 1 answers

2
MPA On BEST ANSWER

I found the source the problem and fixed it by changing:

DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai1", "LVDT", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, None)

to

DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai1", "LVDT", DAQmx_Val_RSE, -10.0, 10.0, DAQmx_Val_Volts, None)