-
Notifications
You must be signed in to change notification settings - Fork 0
/
RIGOL_DS1104Z.py
executable file
·212 lines (163 loc) · 7.03 KB
/
RIGOL_DS1104Z.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
204
205
206
207
208
209
210
211
212
import visa as vs
import sys
from time import sleep
import numpy as np
import dashboard_functions as dbf
"""
{} - optional parameters
| - seperate multiple parameters
[] - Contents in [] can be omitted
<> - content must be replaced with an effective value
"""
class RIGOL_DS1104Z():
#define class object attributes
description='class for using the RIGOL DS1104Z scope'
def __init__(self,scopeName='',scope='', memDepth = 600000, sampleRate = 1, numSamples = 0):
self.scopeName = scopeName
self.scope = scope
def get_USB_port(self):
#we try 10 attempts at opening the resource.
attempts = 10
while attempts > 0:
try:
#instantiate ResourceManager from pyVISA
rm = vs.ResourceManager('@py')
#return a list of resources to an array
available=rm.list_resources()
#usually the oscilloscope will be the 0th element of the available array
self.scopeName=available[1]
# self.scopeName = "/dev/ttyAMA0::INSTR"
print(self.scopeName)
#open the oscilloscope, timeout of 5sec,
self.scope=rm.open_resource(self.scopeName, timeout=10000, chunk_size=1024000)
attempts = 0
print('Scope initialized')
except:
print("attempts: " + str(attempts))
attempts = attempts - 1
def initialize_scope(self,format_type = 'ASC', mode = 'RAW', memDepth = 1200000, channel = 1):
#get the USB port and name of scope. Open the scope to transmission
self.get_USB_port()
#set the format
self.stop()
self.wave_format_set(format_type)
#set the mode
self.wave_mode_set(mode)
#we need to initialize the channels before setting the memory depth
#otherwise if someone has on all 4 channels, but we want to sample 1 channel
#very high, the scope will limit our memory depth because it thinks we're using
# more than 1 channel
self.initialize_channel(channel)
#to adjust the # of points recorded (depth) we need to have the scope in
# Run mode and then change the depth
self.run()
#set the memory depth
self.acquire_depth_set(memDepth)
self.stop()
#set the termination
self.scope.read_termination='\n'
def initialize_channel(self,channel = [1]):
analog_channels = [1,2,3,4] #make a list of all the analog channels
for i in channel:
self.channel_display_on(i)
if int(i) in analog_channels:
analog_channels.remove(int(i)) #remove the active analog channel form the list
for i in analog_channels:
self.channel_display_off(i)
def deinitialize_channel(self, channel):
self.channel_display_off()
self.wave_source_get()
''' COMMAND SYSTEM'''
def autoscale(self):
self.scope.write(':AUT')
def clear(self):
self.scope.write(':CLE')
def run(self):
self.scope.write(':RUN')
def stop(self):
self.scope.write(':STOP')
def single(self):
self.scope.write(':SING')
'''ACQUIRE COMMANDS '''
def acquire_averages_get(self):
return self.scope.query(':ACQ:AVER?')
def acquire_depth_get(self):
return self.scope.query(':ACQ:MDEP?')
def acquire_depth_set(self,num):
self.scope.write(':ACQ:MDEP '+ str(int(num)))
def acquire_type_get(self):
return self.scope.query(':ACQ:TYPE?')
#NORMal, AVERages, PEAK, HRESolution
def acquire_type_set(self,mode):
self.scope.write(':ACQ:TYPE '+str(mode))
def acquire_srate_get(self):
return float(self.scope.query(':ACQ:SRATe?'))
def channel_coupling_get(self,channel):
return self.scope.write(':CHAN' + channel + ':COUP?')
def channel_coupling_set(self,channel):
return self.scope.write(':CHAN' + channel + ':COUP?')
def channel_display_get(self,channel):
return self.scope.write(':CHAN'+str(channel)+':DISP?')
#
def channel_display_on(self,channel):
self.scope.write(':CHAN'+str(channel)+':DISP ON')
def channel_display_off(self,channel):
self.scope.write(':CHAN'+str(channel)+':DISP OFF')
#get the channel scale
def chan_scale_get(self, channel):
return self.scope.query(':CHAN'+channel+':SCAL?')
def time_scale_get(self):
return self.scope.query(':TIM:MAIN:SCALE?')
def trigger_status(self):
return self.scope.query(':TRIG:STAT?')
'''DISPLAY COMMANDS'''
#get the channel scale
def display_data_get(self):
return self.scope.query(':DISP:DATA?')
def wave_mode_set(self,chan):
self.scope.write(':WAV:MODE '+str(chan))
def wave_mode_get(self):
return self.scope.query('WAV:MODE?')
def wave_format_set(self, frmt):
self.scope.write(':WAV:FORM '+str(frmt))
def wave_format_get(self):
return self.scope.query('WAV:FORM?')
def wave_source_set(self,chan):
self.scope.write(':WAV:SOUR CHAN'+str(chan))
def wave_source_get(self):
return self.scope.query('WAV:SOUR?')
def wave_start_point(self,num):
self.scope.write(':WAV:STAR '+str(num))
def wave_stop_point(self,num):
self.scope.write(':WAV:STOP '+str(num))
def single_channel_data(self, start=1, stop= 125000, decimals=2):
#set the start point
self.wave_start_point(start)
#set the stop point
self.wave_stop_point(stop)
#get the raw data
raw = np.array(self.scope.query_ascii_values(':WAV:DATA?', converter='s'))
#check for data type
try:
values = raw[1:].astype(float)
print(values)
values = np.around(values, decimals = decimals)
except ValueError as e:
print("got it :-) ", e)
return values
def channel_data_return(self, channel):
self.stop()
self.wave_source_set(channel)
memDepth = self.acquire_depth_get()
print("Memory Depth: " + str(memDepth))
queryFormat = self.wave_format_get()
numQueriesNeeded, maxReadsPerQuery = dbf.calc_query_req(str(queryFormat), memDepth)
channelData = np.empty([1])
for i in range(numQueriesNeeded):
start = (i * maxReadsPerQuery) + 1
stop = start + maxReadsPerQuery
if stop > int(memDepth):
stop = int(memDepth)
newData = self.single_channel_data(start = start, stop = stop)
channelData = np.concatenate((channelData,newData), axis = 0)
return channelData