-
Notifications
You must be signed in to change notification settings - Fork 0
/
RigolDriver.py
171 lines (154 loc) · 6.3 KB
/
RigolDriver.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
# rigolrdout - Read data and screenshots from Rigol oscilloscopes
# Copyright (C) 2012-2018 Johannes Bauer
#
# This file is part of rigolrdout.
#
# rigolrdout is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; this program is ONLY licensed under
# version 3 of the License, later versions are explicitly excluded.
#
# rigolrdout is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rigolrdout; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Johannes Bauer <JohannesBauer@gmx.de>
import time
import collections
from TMCDataTypes import TMCBool, TMCFloat, TMCRawData
class RigolDriver(object):
_InstrumentParameters = collections.namedtuple("InstrumentParameters", [ "number_channels" ])
_KnownInstruments = {
("RIGOL TECHNOLOGIES", "DS1104Z"): _InstrumentParameters(number_channels = 4),
}
_IdentifyResult = collections.namedtuple("IdentifyResult", [ "vendor", "device", "serial", "fw_version", "instrument_parameters" ])
def __init__(self, connection):
self._conn = connection
self._identification = self._identify()
@property
def identification(self):
return self._identification
def _identify(self):
response = self._conn.command("*IDN?")
(vendor, device, serial, fw_version) = response.split(",")
instrument_id = (vendor, device)
if instrument_id not in self._KnownInstruments:
raise Exception("Instrument '%s' not known currently, add to 'KnownInstruments' dictionary." % (str(instrument_id)))
instrument_parameters = self._KnownInstruments[instrument_id]
return self._IdentifyResult(vendor = vendor, device = device, serial = serial, fw_version = fw_version, instrument_parameters = instrument_parameters)
def data(self):
print(self._conn.command(":MEAS:RIS? CHAN1"))
def run(self):
response = self._conn.command(":RUN")
def stop(self):
response = self._conn.command(":STOP")
def get_acquisition_info(self):
result = {
"acquisition": {
"type": self._conn.command(":ACQ:TYPE?"),
"sample_rate": TMCFloat(self._conn.command(":ACQ:SRAT?")),
"mem_depth": self._conn.command(":ACQ:MDEP?"),
"averages": self._conn.command(":ACQ:AVER?"),
},
"timebase": {
"offset": TMCFloat(self._conn.command(":TIM:OFFS?")),
"scale": TMCFloat(self._conn.command(":TIM:SCAL?")),
"mode": self._conn.command(":TIM:MODE?"),
},
"trigger": {
"mode": self._conn.command(":TRIG:MODE?"),
"coupling": self._conn.command(":TRIG:COUP?"),
"status": self._conn.command(":TRIG:STAT?"),
"sweep": self._conn.command(":TRIG:SWE?"),
"holdoff": TMCFloat(self._conn.command(":TRIG:HOLD?")),
"nreject": TMCBool(self._conn.command(":TRIG:NREJ?")),
"position": int(self._conn.command(":TRIG:POS?")),
"specific": None,
},
}
if result["trigger"]["mode"].lower() == "edge":
result["trigger"]["specific"] = {
"source": self._conn.command(":TRIG:EDG:SOUR?"),
"slope": self._conn.command(":TRIG:EDG:SLOP?"),
"level": TMCFloat(self._conn.command(":TRIG:EDG:LEV?")),
}
return result
def get_channel_info(self, channel_id):
return {
"bw_limit": self._conn.command(":CHAN%d:BWL?" % (channel_id)),
"coupling": self._conn.command(":CHAN%d:COUP?" % (channel_id)),
"inverted": TMCBool(self._conn.command(":CHAN%d:INV?" % (channel_id))),
"offset": TMCFloat(self._conn.command(":CHAN%d:OFFS?" % (channel_id))),
"range": TMCFloat(self._conn.command(":CHAN%d:RANG?" % (channel_id))),
"time_cal": TMCFloat(self._conn.command(":CHAN%d:TCAL?" % (channel_id))),
"scale": TMCFloat(self._conn.command(":CHAN%d:SCAL?" % (channel_id))),
"probe": TMCFloat(self._conn.command(":CHAN%d:PROB?" % (channel_id))),
"unit": self._conn.command(":CHAN%d:UNIT?" % (channel_id)),
"vernier": TMCBool(self._conn.command(":CHAN%d:VERN?" % (channel_id))),
}
def get_waveform(self, channel_id):
self._conn.command(":WAV:SOUR CHAN%d" % (channel_id))
self._conn.command(":WAV:MODE RAW")
self._conn.command(":WAV:FORM BYTE")
preamble = self._conn.command(":WAV:PRE?")
preamble = preamble.split(",")
assert(len(preamble) == 10)
metadata = {
"type": "waveform",
"channel": channel_id,
"format": {
0: "BYTE",
1: "WORD",
2: "ASC",
}[int(preamble[0])],
"waveform_type": {
0: "NORM",
1: "MAX",
2: "RAW",
}[int(preamble[1])],
"points": int(preamble[2]),
"count": int(preamble[3]),
"x_increment": TMCFloat(preamble[4]),
"x_origin": TMCFloat(preamble[5]),
"x_reference": int(preamble[6]),
"y_increment": TMCFloat(preamble[7]),
"y_origin": TMCFloat(preamble[8]),
"y_reference": int(preamble[9]),
}
raw_data = bytearray()
total_bytes = metadata["points"]
bytes_per_batch = 250000
batches = (total_bytes + bytes_per_batch - 1) // bytes_per_batch
for i in range(batches):
start = 1 + (i * bytes_per_batch)
stop = start + bytes_per_batch - 1
stop = min(stop, total_bytes)
self._conn.command(":WAV:STAR %d" % (start))
self._conn.command(":WAV:STOP %d" % (stop))
self._conn.command(":WAV:DATA?", wait_response = False)
data = self._conn.get_tmc_data(timeout = 5.0)
raw_data += data
time.sleep(0.1)
return TMCRawData(data = raw_data, file_format = "bin", metadata = metadata)
def is_channel_enabled(self, channel_id):
return TMCBool(self._conn.command(":CHAN%d:DISP?" % (channel_id)))
def get_enabled_channel_info(self):
channel_info = { }
for channel_id in range(1, self.identification.instrument_parameters.number_channels + 1):
if self.is_channel_enabled(channel_id):
channel_info[channel_id] = self.get_channel_info(channel_id)
return channel_info
def get_display_data(self, img_format = "png"):
assert(img_format in [ "bmp24", "bmp8", "png", "jpeg", "tiff" ])
# color, invert, format
self._conn.command(":DISPLAY:DATA? ON,OFF,%s" % (img_format.upper()), wait_response = False)
return TMCRawData(data = self._conn.get_tmc_data(timeout = 5.0), file_format = img_format, metadata = {
"color": True,
"invert": False,
"type": "hardcopy",
})