定义 NI-DAQ 的输入通道时 Python 中的类型错误
typeError in Python when defining a input chanel of NI-DAQ
Python 使用 NI-DAQ 进行数据采集的代码。
已下载 NI 驱动
错误
Traceback (most recent call last):
File "C:\Users\icon\Desktop\DAQ 1.0.py", line 68, in <module>
chan = ctypes.create_string_buffer('Dev1/ai0')
File "C:\Python34\lib\ctypes\__init__.py", line 63, in
create_string_buffer
raise TypeError(init)
TypeError: Dev1/ai0
我是学生。我试图在 Python 中编写程序以从 NI-DAQ 获取数据,但它引发了上述错误。
这是代码
导入了所有需要的库
nidaq = ctypes.windll.nicaiu
int32 = ctypes.c_long
uInt32 = ctypes.c_ulong
uInt64 = ctypes.c_ulonglong
float64 = ctypes.c_double
TaskHandle = uInt32
written = int32()
pointsRead = uInt32()
DAQmx_Val_Volts = 10348
DAQmx_Val_Rising = 10280
DAQmx_Val_Cfg_Default = int32(-1)
DAQmx_Val_ContSamps = 10123
DAQmx_Val_ChanForAllLines = 1
DAQmx_Val_RSE = 10083
DAQmx_Val_Volts = 10348
DAQmx_Val_GroupByScanNumber = 1
DAQmx_Val_FiniteSamps = 10178
DAQmx_Val_GroupByChannel = 0
taskHandle = TaskHandle(0)
min1 = float64(-10.0)
max1 = float64(10.0)
timeout = float64(10.0)
bufferSize = uInt32(10)
pointsToRead = bufferSize
pointsRead = uInt32()
sampleRate = float64(200.0)
samplesPerChan = uInt64(100)
#specifiy the channels
chan = ctypes.create_string_buffer('Dev1/ai0')
clockSource = ctypes.create_string_buffer('OnboardClock')
根据[Python]: ctypes.create_string_buffer(init_or_size, size=None):
init_or_size must be an integer which specifies the size of the array, or a bytes object which will be used to initialize the array items.
因此,对于您的情况,绕过 这个 错误的最简单方法是将 bytes
对象传递给 create_string_buffer
。请注意,传递字符串在 Python3 中不起作用,因为“string”语义已从 更改Python2(有效的地方)。
示例:
>>> import sys, ctypes
>>> print("Python {:s} on {:s}".format(sys.version, sys.platform))
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32
>>> device_str = "Dev1/ai0"
>>>
>>> chan = ctypes.create_string_buffer(device_str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Install\x64\Python\Python.4\Lib\ctypes\__init__.py", line 63, in create_string_buffer
raise TypeError(init)
TypeError: Dev1/ai0
>>>
>>> device_bytes = b"Dev1/ai0" # Use bytes literal directly
>>> chan = ctypes.create_string_buffer(device_bytes)
>>> chan, chan.value
(<ctypes.c_char_Array_9 object at 0x00000000058E0DC8>, b'Dev1/ai0')
>>>
>>> chan = ctypes.create_string_buffer(device_str.encode()) # Use string's encode to convert it to bytes
>>> chan, chan.value
(<ctypes.c_char_Array_9 object at 0x00000000058E0E48>, b'Dev1/ai0')
Python 使用 NI-DAQ 进行数据采集的代码。 已下载 NI 驱动
错误
Traceback (most recent call last):
File "C:\Users\icon\Desktop\DAQ 1.0.py", line 68, in <module>
chan = ctypes.create_string_buffer('Dev1/ai0')
File "C:\Python34\lib\ctypes\__init__.py", line 63, in
create_string_buffer
raise TypeError(init)
TypeError: Dev1/ai0
我是学生。我试图在 Python 中编写程序以从 NI-DAQ 获取数据,但它引发了上述错误。
这是代码
导入了所有需要的库
nidaq = ctypes.windll.nicaiu
int32 = ctypes.c_long
uInt32 = ctypes.c_ulong
uInt64 = ctypes.c_ulonglong
float64 = ctypes.c_double
TaskHandle = uInt32
written = int32()
pointsRead = uInt32()
DAQmx_Val_Volts = 10348
DAQmx_Val_Rising = 10280
DAQmx_Val_Cfg_Default = int32(-1)
DAQmx_Val_ContSamps = 10123
DAQmx_Val_ChanForAllLines = 1
DAQmx_Val_RSE = 10083
DAQmx_Val_Volts = 10348
DAQmx_Val_GroupByScanNumber = 1
DAQmx_Val_FiniteSamps = 10178
DAQmx_Val_GroupByChannel = 0
taskHandle = TaskHandle(0)
min1 = float64(-10.0)
max1 = float64(10.0)
timeout = float64(10.0)
bufferSize = uInt32(10)
pointsToRead = bufferSize
pointsRead = uInt32()
sampleRate = float64(200.0)
samplesPerChan = uInt64(100)
#specifiy the channels
chan = ctypes.create_string_buffer('Dev1/ai0')
clockSource = ctypes.create_string_buffer('OnboardClock')
根据[Python]: ctypes.create_string_buffer(init_or_size, size=None):
init_or_size must be an integer which specifies the size of the array, or a bytes object which will be used to initialize the array items.
因此,对于您的情况,绕过 这个 错误的最简单方法是将 bytes
对象传递给 create_string_buffer
。请注意,传递字符串在 Python3 中不起作用,因为“string”语义已从 更改Python2(有效的地方)。
示例:
>>> import sys, ctypes >>> print("Python {:s} on {:s}".format(sys.version, sys.platform)) Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32 >>> device_str = "Dev1/ai0" >>> >>> chan = ctypes.create_string_buffer(device_str) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\Install\x64\Python\Python.4\Lib\ctypes\__init__.py", line 63, in create_string_buffer raise TypeError(init) TypeError: Dev1/ai0 >>> >>> device_bytes = b"Dev1/ai0" # Use bytes literal directly >>> chan = ctypes.create_string_buffer(device_bytes) >>> chan, chan.value (<ctypes.c_char_Array_9 object at 0x00000000058E0DC8>, b'Dev1/ai0') >>> >>> chan = ctypes.create_string_buffer(device_str.encode()) # Use string's encode to convert it to bytes >>> chan, chan.value (<ctypes.c_char_Array_9 object at 0x00000000058E0E48>, b'Dev1/ai0')