-
Notifications
You must be signed in to change notification settings - Fork 0
/
NIDAQmx.py
274 lines (231 loc) · 8.45 KB
/
NIDAQmx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# (c) 2020, ETH Zurich, Power Electronic Systems Laboratory, T. Guillod
import ctypes
import numpy
class NIDAQmx():
"""
This Python class controls the "NI-6215" USB DAQ:
- connect to the device
- read and write
- analog and digital signals
This class:
- uses the "nicaiu.dll"
- run on "MS Windows" but adaptation to Linux should be possible
- should also be easy to adapt to other NI DAQ cards
- was tested with Python 2.7 but should run with Python 3.x
This class is meant as a lightweight code to be used as a "code snippet" and not as a full package.
For more functionalities, you can use libraries like "NI-DAQmx Python".
"""
def __init__(self, daq_name):
"""
Constructor of the NIDAQmx class.
"""
# assign the name and get the DLL (MS windows only)
self.daq_name = daq_name
self.NIDAQmx = ctypes.windll.nicaiu
# number of channels for the DAQ NI-6215
self.n = dict()
self.n["analog_read"] = 16
self.n["analog_write"] = 2
self.n["digital_read"] = 4
self.n["digital_write"] = 4
# dict with the internal data of the DAQ NI-6215
self.DAQmx_Val_RSE = 10083
self.DAQmx_Val_Volts = 10348
self.DAQmx_Val_GroupByChannel = 0
self.DAQmx_Val_ChanPerLine = 0
self.DAQmx_V_max = 10.0
self.DAQmx_delay = 10.0
# dict for the read/write tasks
self.task = dict()
def open(self):
"""
Make the connection to the device. Reset the device.
"""
# check the status and reset the device
self._check_device()
self.NIDAQmx.DAQmxResetDevice(self.daq_name)
# counter for the task number
self.task = dict()
c = 0
# analog read task
self.task["analog_read"] = ctypes.c_ulong(c)
self._daq_check(self.NIDAQmx.DAQmxCreateTask("", ctypes.byref(self.task["analog_read"])))
self._daq_check(
self.NIDAQmx.DAQmxCreateAIVoltageChan(
self.task["analog_read"],
self.daq_name + "/ai0:" + str(self.n["analog_read"] - 1),
"",
self.DAQmx_Val_RSE,
ctypes.c_double(-self.DAQmx_V_max), ctypes.c_double(self.DAQmx_V_max),
self.DAQmx_Val_Volts,
None
)
)
# digital read task
self.task["digital_read"] = ctypes.c_ulong(c)
self._daq_check(self.NIDAQmx.DAQmxCreateTask("", ctypes.byref(self.task["digital_read"])))
self._daq_check(
self.NIDAQmx.DAQmxCreateDIChan(
self.task["digital_read"],
self.daq_name + "/port0/line0:" + str(self.n["digital_read"] - 1),
"",
self.DAQmx_Val_ChanPerLine
)
)
# analog write tasks
for i in range(self.n["analog_write"]):
self.task["analog_write_" + str(i)] = ctypes.c_ulong(c)
c += 1
self._daq_check(self.NIDAQmx.DAQmxCreateTask("", ctypes.byref(self.task["analog_write_" + str(i)])))
self._daq_check(
self.NIDAQmx.DAQmxCreateAOVoltageChan(
self.task["analog_write_" + str(i)], self.daq_name + "/ao" + str(i),
"",
ctypes.c_double(-self.DAQmx_V_max),
ctypes.c_double(self.DAQmx_V_max),
self.DAQmx_Val_Volts,
None
)
)
# digital write tasks
for i in range(self.n["digital_write"]):
self.task["digital_write_" + str(i)] = ctypes.c_ulong(c)
c += 1
self._daq_check(self.NIDAQmx.DAQmxCreateTask("", ctypes.byref(self.task["digital_write_" + str(i)])))
self._daq_check(
self.NIDAQmx.DAQmxCreateDOChan(
self.task["digital_write_" + str(i)],
self.daq_name + "/port1/line" + str(i),
"",
self.DAQmx_Val_GroupByChannel
)
)
# start the tasks
for name in self.task:
self._daq_check(self.NIDAQmx.DAQmxStartTask(self.task[name]))
def close(self):
"""
Close the connection to the device.
"""
# stop the tasks
for name in self.task:
self.NIDAQmx.DAQmxStopTask(self.task[name])
self.NIDAQmx.DAQmxClearTask(self.task[name])
# reset the tasks and the device
self.task = dict()
self.NIDAQmx.DAQmxResetDevice(self.daq_name)
def _daq_check(self, err):
"""
Check the return code of the DAQ.
"""
if err < 0:
buf_size = 1024
buf = ctypes.create_string_buffer('\000' * buf_size)
self.NIDAQmx.DAQmxGetErrorString(err, ctypes.byref(buf), buf_size)
raise RuntimeError('NIDAQmx call failed with error %d: %s' % (err, repr(buf.value)))
def _check_device(self):
"""
Check if the DAQ is found.
"""
buf_size = 1024
buf = ctypes.create_string_buffer('\000' * buf_size)
self._daq_check(self.NIDAQmx.DAQmxGetSysDevNames(ctypes.byref(buf), buf_size))
name_tmp = ''
namelist = []
for ch in buf:
if ch in '\000 \t\n':
name_tmp = name_tmp.rstrip(',')
if len(name_tmp) > 0:
namelist.append(name_tmp)
name_tmp = ''
if ch == '\000':
break
else:
name_tmp += ch
assert self.daq_name in namelist, "invalid device"
def analog_read(self, n):
"""
Read a analog channel.
"""
data = numpy.zeros((self.n["analog_read"],), dtype=numpy.float64)
self._daq_check(
self.NIDAQmx.DAQmxReadAnalogF64(
self.task["analog_read"],
-1,
ctypes.c_double(self.DAQmx_delay),
self.DAQmx_Val_GroupByChannel, data.ctypes.data,
self.n["analog_read"],
ctypes.byref(ctypes.c_long()),
None
)
)
data = data.tolist()
if isinstance(n, list):
data = [data[i] for i in n]
elif isinstance(n, int):
data = data[n]
else:
raise RuntimeError("invalid channel")
return data
def digital_read(self, n):
"""
Read one (or many) digital channel.
"""
data = numpy.zeros((self.n["digital_read"],), dtype=numpy.uint8)
self._daq_check(
self.NIDAQmx.DAQmxReadDigitalLines(
self.task["digital_read"],
-1,
ctypes.c_double(self.DAQmx_delay),
self.DAQmx_Val_GroupByChannel,
data.ctypes.data,
self.n["digital_read"],
ctypes.byref(ctypes.c_long()),
ctypes.byref(ctypes.c_long()),
None
)
)
data = data.astype('bool')
data = data.tolist()
if isinstance(n, list):
data = [data[i] for i in n]
elif isinstance(n, int):
data = data[n]
else:
raise RuntimeError("invalid channel")
return data
def analog_write(self, n, data):
"""
Write (or many) analog channel.
"""
assert n < self.n["analog_write"], "invalid channel"
data = max(min(data, self.DAQmx_V_max), -self.DAQmx_V_max)
self._daq_check(
self.NIDAQmx.DAQmxWriteAnalogF64(
self.task["analog_write_" + str(n)],
1,
0,
ctypes.c_double(self.DAQmx_delay),
self.DAQmx_Val_GroupByChannel,
ctypes.byref(ctypes.c_double(data)),
ctypes.byref(ctypes.c_long()),
None
)
)
def digital_write(self, n, data):
"""
Write a digital channel.
"""
assert n < self.n["digital_write"], "invalid channel"
self._daq_check(
self.NIDAQmx.DAQmxWriteDigitalLines(
self.task["digital_write_" + str(n)],
1,
0,
ctypes.c_double(self.DAQmx_delay),
self.DAQmx_Val_GroupByChannel,
ctypes.byref(ctypes.c_ushort(data)),
ctypes.byref(ctypes.c_long()),
None
)
)