简体   繁体   中英

Using PyDAQmx to read analog input yields constant values

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM