-2

Python code for Data Aqusition using NI-DAQ. Had downloaded NI-driver

Error

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

I am a student. I was trying to code a program in Python to acquire data from NI-DAQ it raised the above error.

this is the code

imported all libraries required

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')
akshay biradar
  • 61
  • 1
  • 1
  • 6

1 Answers1

0

According to [Python.Docs]: ctypes.create_string_buffer(init_or_size, size=None) (emphasis is mine):

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.

So, for your case, the simplest way to bypass this error would be to pass the bytes object to create_string_buffer. Note that passing a string doesn't work in Python3, because the "string" semantics have changed from Python2 (where it works).

Example:

>>> 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\3.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')
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • I have another problem can you help me? – akshay biradar Feb 01 '18 at 10:35
  • ctypes.ArgumentError: argument 2: : Don't know how to convert parameter 2 – akshay biradar Feb 01 '18 at 10:36
  • Technically, that would qualify for another question. But either way, without the line(s) of code that generates the error it's hard to figure out. Anyway, when adding info please edit the question instead of posting comments. – CristiFati Feb 01 '18 at 10:53
  • I have mentioned the link fpor my code. Go through that and please help me to resolve the problem – akshay biradar Feb 06 '18 at 04:24
  • I said to **add** it not replace it. Now the question has no mention about the previous error. And it will most likely be deleted. the best thing to do is revert the last edit and ask another question. And try to follow the basic ground rules (like posting the minimal code that reproduces the behavior in the question, not as a link - which may go bad). – CristiFati Feb 06 '18 at 06:53
  • Did you post the other question? – CristiFati Feb 06 '18 at 10:18
  • the same error popped up again . but this time it was for the"""onboardclock" – akshay biradar Feb 07 '18 at 10:03
  • Usually, when an error that was fixed occurs in another place, repeating the steps needed to fix the 1st occurrence, would also fix the 2nd. You keep ignoring almost everything that you're advised. I'm not going to post any comments anymore. – CristiFati Feb 07 '18 at 10:45
  • when i do the same action again then a new error comes ctypes.ArgumentError: argument 5: : int too long to convert – akshay biradar Feb 07 '18 at 12:00