forked from nicedwarf/python-ivi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyvisa.py
131 lines (109 loc) · 4.33 KB
/
pyvisa.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
"""
Python Interchangeable Virtual Instrument Library
Copyright (c) 2014-2016 Alex Forencich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import io
import sys
from distutils.version import StrictVersion
try:
import pyvisa as visa
try:
# New style PyVISA
visa_rm = visa.ResourceManager()
visa_instrument_opener = visa_rm.open_resource
except AttributeError:
# Old style PyVISA
visa_instrument_opener = visa.instrument
except ImportError:
# PyVISA not installed, pass it up
raise ImportError
except:
# any other error
e = sys.exc_info()[1]
sys.stderr.write("python-ivi: PyVISA is installed, but could not be loaded (%s: %s)\n" %
(e.__class__.__name__, e.args[0]))
raise ImportError
class PyVisaInstrument:
"PyVisa wrapper instrument interface client"
def __init__(self, resource, *args, **kwargs):
if type(resource) is str:
self.instrument = visa_instrument_opener(resource, *args, **kwargs)
# For compatibility with new style PyVISA
if not hasattr(self.instrument, 'trigger'):
self.instrument.trigger = self.instrument.assert_trigger
else:
self.instrument = resource
self.buffer = io.BytesIO()
def write_raw(self, data):
"Write binary data to instrument"
self.instrument.write_raw(data)
def read_raw(self, num=-1):
"Read binary data from instrument"
# PyVISA only supports reading entire buffer
#return self.instrument.read_raw()
data = self.buffer.read(num)
if len(data) == 0:
self.buffer = io.BytesIO(self.instrument.read_raw())
data = self.buffer.read(num)
return data
def ask_raw(self, data, num=-1):
"Write then read binary data"
self.write_raw(data)
return self.read_raw(num)
def write(self, message, encoding = 'utf-8'):
"Write string to instrument"
if type(message) is tuple or type(message) is list:
# recursive call for a list of commands
for message_i in message:
self.write(message_i, encoding)
return
self.write_raw(str(message).encode(encoding))
def read(self, num=-1, encoding = 'utf-8'):
"Read string from instrument"
return self.read_raw(num).decode(encoding).rstrip('\r\n')
def ask(self, message, num=-1, encoding = 'utf-8'):
"Write then read string"
if type(message) is tuple or type(message) is list:
# recursive call for a list of commands
val = list()
for message_i in message:
val.append(self.ask(message_i, num, encoding))
return val
self.write(message, encoding)
return self.read(num, encoding)
def read_stb(self):
"Read status byte"
raise NotImplementedError()
def trigger(self):
"Send trigger command"
self.instrument.trigger()
def clear(self):
"Send clear command"
raise NotImplementedError()
def remote(self):
"Send remote command"
raise NotImplementedError()
def local(self):
"Send local command"
raise NotImplementedError()
def lock(self):
"Send lock command"
raise NotImplementedError()
def unlock(self):
"Send unlock command"
raise NotImplementedError()