-
Notifications
You must be signed in to change notification settings - Fork 3
/
rigol_ds1000z.py
204 lines (153 loc) · 5.75 KB
/
rigol_ds1000z.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
""" Rigol oscilloscope using Python
Post Tenebras Lab the geneva(Hackerspace)
"""
__author__ = 'Sebastien Chassot'
__author_email__ = '[email protected]'
__version__ = "1.0.1"
__copyright__ = ""
__maintainer__ = "Sebastien Chassot"
__licence__ = "GPL"
__status__ = ""
import sys
import os
import ds1074z_cmd
import matplotlib.pyplot as pyplot
class DS1000z:
""" Class to control a Rigol DS1000 series oscilloscope
"""
nb_of_channel = range(1, 5) # those scopes have 4 channel
def __init__(self, device):
""" constructor
:param device: device (/dev/usbtcmX)
"""
self.cmd = ds1074z_cmd.DS1074zCommands(device)
self.name = self.cmd.get_name()
print(self.name+"...scope initialized")
self.channel = []
for chan in self.nb_of_channel: # Create each channel state
self.channel.append(Channel(chan.__str__(), self.cmd))
self.channel[-1].update_state()
self.measures = [] # Acquisitions are placed in this list
def write(self, command):
""" send an action command (no return from the scope)
:param command: command to be send (ex. :CHAN3:DISP OFF)
"""
self.cmd.write(command)
def read(self, command, length=4096):
""" send a cmd to the scope and return it
:param command: command to be send (ex. :ACQuire:TYPE?)
:param length: buffer length
:return: value returned by the scope
"""
return self.cmd.read(command, length)
def acquire(self, channel_lst):
""" append new acquisition(s) to measure list
:return: id of the measure (self.measure[id])
"""
lst = []
for chan in self.nb_of_channel:
c = Channel(chan, self.cmd)
m = Acquisition(c)
if chan in channel_lst:
m.get_data()
lst.append(m)
self.measures.append(lst)
return self.measures.__len__() # the measure id
def get_rate(self):
return self.cmd.get_rate()
class Channel:
PROBE_RATIO = 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000
def __init__(self, channel, cmd, probe_ratio=PROBE_RATIO[9]):
self.channel = channel.__str__()
self.cmd = cmd
self.display = self.get_display()
self.probe_ratio = probe_ratio
if probe_ratio is not self.PROBE_RATIO[9]:
self.cmd.set_probe_ratio(probe_ratio, channel)
self.volt_scale = self.get_volt_scale()
self.volt_offset = self.get_volt_offset()
self.time_scale = self.get_time_scale()
self.time_offset = self.get_time_offset()
def update_state(self):
""" update all field from the actual scope state
"""
self.display = self.get_display()
self.probe_ratio = self.get_probe_ratio()
self.volt_scale = self.get_volt_scale()
self.volt_offset = self.get_volt_offset()
self.time_scale = self.get_time_scale()
self.time_offset = self.get_time_offset()
def restore_state(self):
""" put the scope in this saved state
"""
self.set_display()
self.set_probe_ratio(self.probe_ratio)
self.set_volt_scale(self.volt_scale)
self.set_volt_offset(self.volt_offset)
self.set_time_scale(self.time_scale)
self.set_time_offset(self.time_offset)
def get_data(self):
return self.cmd.get_data(self)
def get_display(self):
return self.cmd.get_display(self.channel)
def set_display(self):
self.cmd.set_display(self.display, self.channel)
def get_probe_ratio(self):
return self.cmd.get_probe_ratio(self.channel)
def set_probe_ratio(self, value):
self.cmd.set_probe_ratio(value, self.channel)
def get_volt_scale(self):
return self.cmd.get_volt_scale(self.channel)
def set_volt_scale(self, value):
self.cmd.set_volt_scale(value, self.channel)
def get_volt_offset(self):
return self.cmd.get_volt_offset(self.channel)
def set_volt_offset(self, value):
self.cmd.set_volt_offset(value, self.channel)
def get_time_scale(self):
return self.cmd.get_time_scale()
def set_time_scale(self, value):
self.cmd.set_time_scale(value)
def get_time_offset(self):
return self.cmd.get_time_offset()
def set_time_offset(self, value):
self.cmd.set_time_offset(value)
class Acquisition:
""" acquire a plot from oscilloscope
:param channel: channel number
:param plot: display the plot True/False
:param filename: save image as
"""
def __init__(self, channel):
# self.cmd = cmd
self.title = "Channel "
self.channel = channel
self.channel.update_state()
self.unit = ["S", "mS", "uS", "nS"]
self.data = []
self.time = []
def get_data(self):
self.channel.restore_state()
self.data, self.time, self.unit = self.channel.get_data()
def plot(self, plot=True):
""" pretty print data (plot)
"""
p = pyplot
p.plot(self.time, self.data)
p.title(self.title+self.channel.channel)
p.ylim((-4*self.channel.volt_scale)-self.channel.volt_offset,(4*self.channel.volt_scale)-self.channel.volt_offset)
p.ylabel("Voltage "+self.channel.volt_scale.__str__()+" (V)")
p.xlabel("Time (" + self.unit[0] + ")")
p.xlim(self.time[0], self.time[-1])
if plot:
pyplot.show()
return p
def save_plot(self, filename):
""" save the plot in a file
:param filename:
"""
self.plot(plot=False)
try:
pyplot.savefig(filename.__str__())
except FileNotFoundError as e:
os.write(sys.stderr, e)