| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
# Most of this code originally from: |
| 26 |
# http://www.scipy.org/Cookbook/Data_Acquisition_with_NIDAQmx |
| 27 |
|
| 28 |
import logging |
| 29 |
import ctypes |
| 30 |
import numpy as np |
| 31 |
from sys import platform |
| 32 |
|
| 33 |
log = logging.getLogger(__name__) |
| 34 |
log.addHandler(logging.NullHandler()) |
| 35 |
|
| 36 |
try: |
| 37 |
if platform == "win32": |
| 38 |
nidaq = ctypes.windll.nicaiu |
| 39 |
except OSError as err: |
| 40 |
log.info('Failed loading the NI-DAQmx library. ' |
| 41 |
+ 'Check the NI-DAQmx documentation on how to ' |
| 42 |
+ 'install this external dependency. ' |
| 43 |
+ f'OSError: {err}') |
| 44 |
raise |
| 45 |
|
| 46 |
# Data Types |
| 47 |
int32 = ctypes.c_long |
| 48 |
uInt32 = ctypes.c_ulong |
| 49 |
uInt64 = ctypes.c_ulonglong |
| 50 |
float64 = ctypes.c_double |
| 51 |
TaskHandle = uInt32 |
| 52 |
# Constants |
| 53 |
DAQmx_Val_Cfg_Default = int32(-1) |
| 54 |
DAQmx_Val_Volts = 10348 |
| 55 |
DAQmx_Val_Rising = 10280 |
| 56 |
DAQmx_Val_FiniteSamps = 10178 |
| 57 |
DAQmx_Val_GroupByChannel = 1 |
| 58 |
|
| 59 |
|
| 60 |
class DAQmx: |
| 61 |
"""Instrument object for interfacing with NI-DAQmx devices.""" |
| 62 |
|
| 63 |
def __init__(self, name, *args, **kwargs): |
| 64 |
super().__init__() |
| 65 |
self.resourceName = name # NOTE: Device number, e.g. Dev1 or PXI1Slot2 |
| 66 |
self.numChannels = 0 |
| 67 |
self.numSamples = 0 |
| 68 |
self.dataBuffer = 0 |
| 69 |
self.taskHandleAI = TaskHandle(0) |
| 70 |
self.taskHandleAO = TaskHandle(0) |
| 71 |
self.terminated = False |
| 72 |
|
| 73 |
def setup_analog_voltage_in(self, channelList, numSamples, sampleRate=10000, scale=3.0): |
| 74 |
resourceString = "" |
| 75 |
for num, channel in enumerate(channelList): |
| 76 |
if num > 0: |
| 77 |
resourceString += ", " # Add a comma before entries 2 and so on |
| 78 |
resourceString += self.resourceName + "/ai" + str(num) |
| 79 |
self.numChannels = len(channelList) |
| 80 |
self.numSamples = numSamples |
| 81 |
self.taskHandleAI = TaskHandle(0) |
| 82 |
self.dataBuffer = np.zeros((self.numSamples, self.numChannels), dtype=np.float64) |
| 83 |
self.CHK(nidaq.DAQmxCreateTask("", ctypes.byref(self.taskHandleAI))) |
| 84 |
self.CHK(nidaq.DAQmxCreateAIVoltageChan(self.taskHandleAI, resourceString, "", |
| 85 |
DAQmx_Val_Cfg_Default, |
| 86 |
float64(-scale), float64(scale), |
| 87 |
DAQmx_Val_Volts, None)) |
| 88 |
self.CHK(nidaq.DAQmxCfgSampClkTiming(self.taskHandleAI, "", float64(sampleRate), |
| 89 |
DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, |
| 90 |
uInt64(self.numSamples))) |
| 91 |
|
| 92 |
def setup_analog_voltage_out(self, channel=0): |
| 93 |
resourceString = self.resourceName + "/ao" + str(channel) |
| 94 |
self.taskHandleAO = TaskHandle(0) |
| 95 |
self.CHK(nidaq.DAQmxCreateTask("", ctypes.byref(self.taskHandleAO))) |
| 96 |
self.CHK(nidaq.DAQmxCreateAOVoltageChan(self.taskHandleAO, |
| 97 |
resourceString, "", |
| 98 |
float64(-10.0), float64(10.0), |
| 99 |
DAQmx_Val_Volts, None)) |
| 100 |
|
| 101 |
def setup_analog_voltage_out_multiple_channels(self, channelList): |
| 102 |
resourceString = "" |
| 103 |
for num, channel in enumerate(channelList): |
| 104 |
if num > 0: |
| 105 |
resourceString += ", " # Add a comma before entries 2 and so on |
| 106 |
resourceString += self.resourceName + "/ao" + str(num) |
| 107 |
self.taskHandleAO = TaskHandle(0) |
| 108 |
self.CHK(nidaq.DAQmxCreateTask("", ctypes.byref(self.taskHandleAO))) |
| 109 |
self.CHK(nidaq.DAQmxCreateAOVoltageChan(self.taskHandleAO, |
| 110 |
resourceString, "", |
| 111 |
float64(-10.0), float64(10.0), |
| 112 |
DAQmx_Val_Volts, None)) |
| 113 |
|
| 114 |
def write_analog_voltage(self, value): |
| 115 |
timeout = -1.0 |
| 116 |
self.CHK(nidaq.DAQmxWriteAnalogScalarF64(self.taskHandleAO, |
| 117 |
1, # Autostart |
| 118 |
float64(timeout), |
| 119 |
float64(value), |
| 120 |
None)) |
| 121 |
|
| 122 |
def write_analog_voltage_multiple_channels(self, values): |
| 123 |
timeout = -1.0 |
| 124 |
self.CHK(nidaq.DAQmxWriteAnalogF64(self.taskHandleAO, |
| 125 |
1, # Samples per channel |
| 126 |
1, # Autostart |
| 127 |
float64(timeout), |
| 128 |
DAQmx_Val_GroupByChannel, |
| 129 |
(np.array(values)).ctypes.data, |
| 130 |
None, None)) |
| 131 |
|
| 132 |
def acquire(self): |
| 133 |
read = int32() |
| 134 |
self.CHK(nidaq.DAQmxReadAnalogF64(self.taskHandleAI, self.numSamples, float64(10.0), |
| 135 |
DAQmx_Val_GroupByChannel, self.dataBuffer.ctypes.data, |
| 136 |
self.numChannels * self.numSamples, ctypes.byref(read), |
| 137 |
None)) |
| 138 |
return self.dataBuffer.transpose() |
| 139 |
|
| 140 |
def acquire_average(self): |
| 141 |
if not self.terminated: |
| 142 |
avg = np.mean(self.acquire(), axis=1) |
| 143 |
return avg |
| 144 |
else: |
| 145 |
return np.zeros(3) |
| 146 |
|
| 147 |
def stop(self): |
| 148 |
if self.taskHandleAI.value != 0: |
| 149 |
nidaq.DAQmxStopTask(self.taskHandleAI) |
| 150 |
nidaq.DAQmxClearTask(self.taskHandleAI) |
| 151 |
if self.taskHandleAO.value != 0: |
| 152 |
nidaq.DAQmxStopTask(self.taskHandleAO) |
| 153 |
nidaq.DAQmxClearTask(self.taskHandleAO) |
| 154 |
|
| 155 |
def CHK(self, err): |
| 156 |
"""a simple error checking routine""" |
| 157 |
if err < 0: |
| 158 |
buf_size = 100 |
| 159 |
buf = ctypes.create_string_buffer('\000' * buf_size) |
| 160 |
nidaq.DAQmxGetErrorString(err, ctypes.byref(buf), buf_size) |
| 161 |
raise RuntimeError('nidaq call failed with error %d: %s' % (err, repr(buf.value))) |
| 162 |
if err > 0: |
| 163 |
buf_size = 100 |
| 164 |
buf = ctypes.create_string_buffer('\000' * buf_size) |
| 165 |
nidaq.DAQmxGetErrorString(err, ctypes.byref(buf), buf_size) |
| 166 |
raise RuntimeError('nidaq generated warning %d: %s' % (err, repr(buf.value))) |
| 167 |
|
| 168 |
def shutdown(self): |
| 169 |
self.stop() |
| 170 |
self.terminated = True |
| 171 |
super().shutdown() |