| 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 |
# Requires 'instrumental' package: https://github.com/mabuchilab/Instrumental |
| 26 |
|
| 27 |
from instrumental.drivers.daq import ni |
| 28 |
from pymeasure.instruments import Instrument |
| 29 |
|
| 30 |
|
| 31 |
def get_dict_attr(obj, attr): |
| 32 |
for obj in [obj] + obj.__class__.mro(): |
| 33 |
if attr in obj.__dict__: |
| 34 |
return obj.__dict__[attr] |
| 35 |
raise AttributeError |
| 36 |
|
| 37 |
|
| 38 |
class NIDAQ(Instrument): |
| 39 |
''' |
| 40 |
Instrument driver for NIDAQ card. |
| 41 |
''' |
| 42 |
|
| 43 |
def __init__(self, name='Dev1', *args, **kwargs): |
| 44 |
self._daq = ni.NIDAQ(name) |
| 45 |
super().__init__( |
| 46 |
None, |
| 47 |
"NIDAQ", |
| 48 |
includeSCPI=False, |
| 49 |
**kwargs) |
| 50 |
for chan in self._daq.get_AI_channels(): |
| 51 |
self.add_property(chan) |
| 52 |
for chan in self._daq.get_AO_channels(): |
| 53 |
self.add_property(chan, set=True) |
| 54 |
|
| 55 |
def add_property(self, chan, set=False): |
| 56 |
if set: |
| 57 |
def fset(self, value): return self.set_chan(chan, value) |
| 58 |
def fget(self): return getattr(self, '_%s' % chan) |
| 59 |
setattr(self, '_%s' % chan, None) |
| 60 |
setattr(self.__class__, chan, property(fset=fset, fget=fget)) |
| 61 |
else: |
| 62 |
def fget(self): return self.get_chan(chan) |
| 63 |
setattr(self.__class__, chan, property(fget=fget)) |
| 64 |
setattr(self.get, chan, lambda: getattr(self, chan)) |
| 65 |
|
| 66 |
def get_chan(self, chan): |
| 67 |
return getattr(self._daq, chan).read().magnitude |
| 68 |
|
| 69 |
def set_chan(self, chan, value): |
| 70 |
setattr(self, '_%s' % chan, value) |
| 71 |
getattr(self._daq, chan).write('%sV' % value) |