|
@@ -1,6 +1,30 @@
|
1
|
1
|
#!/usr/bin/env python
|
|
2
|
+
|
|
3
|
+from telnetlib_receive_all import Telnet
|
|
4
|
+from Rigol_functions import *
|
|
5
|
+import time
|
|
6
|
+from PIL import Image
|
|
7
|
+import StringIO
|
|
8
|
+import sys
|
|
9
|
+import os
|
|
10
|
+import platform
|
|
11
|
+import logging
|
|
12
|
+
|
|
13
|
+__version__ = 'v1.0.0'
|
2
|
14
|
__author__ = 'RoGeorge'
|
|
15
|
+
|
|
16
|
+#
|
|
17
|
+# TODO: Replace the fixed delay between commands with *OPC? (Operation Complete) query
|
|
18
|
+# TODO: Add debug mode
|
|
19
|
+# TODO: Add debug switch
|
|
20
|
+# TODO: Add Python and modules version
|
|
21
|
+# TODO: Add script version
|
|
22
|
+# TODO: Add message for csv data points: mdep (all) or 1200 (screen), depending on RUN/STOP state, MATH and WAV:MODE
|
|
23
|
+# TODO: Clarify info, warning, error, debug and print messages
|
|
24
|
+# TODO: Remove debugging print lines
|
|
25
|
+# TODO: Add .gitignore
|
3
|
26
|
#
|
|
27
|
+
|
4
|
28
|
"""
|
5
|
29
|
# TODO: Use "waveform:data?" multiple times to extract the whole 12M points
|
6
|
30
|
in order to overcome the "Memory lack in waveform reading!" screen message
|
|
@@ -15,14 +39,12 @@ __author__ = 'RoGeorge'
|
15
|
39
|
# TODO: Add browse and custom filename selection
|
16
|
40
|
# TODO: Create executable distributions
|
17
|
41
|
#
|
18
|
|
-from telnetlib_receive_all import Telnet
|
19
|
|
-from Rigol_functions import *
|
20
|
|
-import time
|
21
|
|
-from PIL import Image
|
22
|
|
-import StringIO
|
23
|
|
-import sys
|
24
|
|
-import os
|
25
|
|
-import platform
|
|
42
|
+
|
|
43
|
+# Set the desired logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
|
44
|
+logging.basicConfig(level=logging.INFO,
|
|
45
|
+ format='%(asctime)s - %(levelname)s - %(message)s',
|
|
46
|
+ filename=os.path.basename(sys.argv[0]) + '.log')
|
|
47
|
+logging.info("Log message: INFO level set.")
|
26
|
48
|
|
27
|
49
|
# Update the next lines for your own default settings:
|
28
|
50
|
path_to_save = "captures/"
|
|
@@ -106,8 +128,7 @@ if response != 0:
|
106
|
128
|
# The default telnetlib drops 0x00 characters,
|
107
|
129
|
# so a modified library 'telnetlib_receive_all' is used instead
|
108
|
130
|
tn = Telnet(IP_DS1104Z, port)
|
109
|
|
-tn.write("*idn?") # ask for instrument ID
|
110
|
|
-instrument_id = tn.read_until("\n", 1)
|
|
131
|
+instrument_id = command(tn, "*idn?") # ask for instrument ID
|
111
|
132
|
|
112
|
133
|
# Check if instrument is set to accept LAN commands
|
113
|
134
|
if instrument_id == "command error":
|
|
@@ -137,16 +158,23 @@ filename = path_to_save + id_fields[model] + "_" + id_fields[serial] + "_" + tim
|
137
|
158
|
|
138
|
159
|
if file_format in ["png", "bmp"]:
|
139
|
160
|
# Ask for an oscilloscope display print screen
|
140
|
|
- tn.write("display:data?")
|
141
|
161
|
print "Receiving screen capture..."
|
142
|
|
- buff = tn.read_until("\n", big_wait)
|
|
162
|
+ buff = command(tn, "display:data?")
|
143
|
163
|
|
144
|
164
|
# Just in case the transfer did not complete in the expected time
|
145
|
165
|
while len(buff) < expected_len:
|
|
166
|
+ logging.warning("Received LESS data then expected! (" +
|
|
167
|
+ str(len(buff)) + " out of " + str(expected_len) + " expected raw BMP bytes.)")
|
146
|
168
|
tmp = tn.read_until("\n", small_wait)
|
147
|
169
|
if len(tmp) == 0:
|
148
|
170
|
break
|
149
|
171
|
buff += tmp
|
|
172
|
+ logging.warning(str(len(tmp)) + " leftover bytes added to 'buff'.")
|
|
173
|
+
|
|
174
|
+ if len(buff) < expected_len:
|
|
175
|
+ logging.error("Received LESS data then expected! (" +
|
|
176
|
+ str(len(buff)) + " out of " + str(expected_len) + " expected raw BMP bytes.)")
|
|
177
|
+ sys.exit("ERROR")
|
150
|
178
|
|
151
|
179
|
# Strip TMC Blockheader and terminator bytes
|
152
|
180
|
buff = buff[TMC_header_len:-terminator_len]
|
|
@@ -164,8 +192,7 @@ elif file_format == "csv":
|
164
|
192
|
# Scan for displayed channels
|
165
|
193
|
channel_list = []
|
166
|
194
|
for channel in ["chan1", "chan2", "chan3", "chan4", "math"]:
|
167
|
|
- tn.write(channel + ":display?")
|
168
|
|
- response = tn.read_until("\n", 1)
|
|
195
|
+ response = command(tn, channel + ":display?")
|
169
|
196
|
|
170
|
197
|
# Strip '\n' terminator
|
171
|
198
|
response = response[:-1]
|
|
@@ -182,15 +209,12 @@ elif file_format == "csv":
|
182
|
209
|
print
|
183
|
210
|
|
184
|
211
|
# Set WAVE parameters
|
185
|
|
- tn.write("waveform:source " + channel)
|
186
|
|
- time.sleep(1)
|
|
212
|
+ command(tn, "waveform:source " + channel)
|
|
213
|
+ command(tn, "waveform:form asc")
|
187
|
214
|
|
188
|
|
- tn.write("waveform:form asc")
|
189
|
|
- time.sleep(1)
|
190
|
|
-
|
191
|
|
- # Maximum - only displayed data when osc. in RUN mode, or full memory data when STOPed
|
192
|
|
- tn.write("waveform:mode max")
|
193
|
|
- time.sleep(1)
|
|
215
|
+ # Maximum = only displayed data when osc. in RUN mode, or full memory data when STOPed
|
|
216
|
+ # Always ONLY displayed data (1200 points) if MATH channel is selected
|
|
217
|
+ command(tn, "waveform:mode max")
|
194
|
218
|
|
195
|
219
|
# Get all possible data
|
196
|
220
|
buff = ""
|
|
@@ -199,22 +223,27 @@ elif file_format == "csv":
|
199
|
223
|
# max_chunk is dependent of the wav:mode and the oscilloscope type
|
200
|
224
|
# if you get on the oscilloscope screen the error message
|
201
|
225
|
# "Memory lack in waveform reading!", then decrease max_chunk value
|
202
|
|
- max_chunk = 100000.0 # tested for DS1104Z
|
|
226
|
+ max_chunk = 100000 # tested for DS1104Z
|
|
227
|
+ print "max_chunk=", max_chunk
|
|
228
|
+ print "depth=", depth
|
|
229
|
+ print "max_chunk > depth:", max_chunk > depth
|
203
|
230
|
if max_chunk > depth:
|
204
|
231
|
max_chunk = depth
|
205
|
232
|
|
206
|
|
- n1 = 1.0
|
|
233
|
+ print "max_chunk=", max_chunk
|
|
234
|
+ n1 = 1
|
207
|
235
|
n2 = max_chunk
|
208
|
|
- data_available = True
|
209
|
|
-
|
|
236
|
+ print "n2=", n2
|
210
|
237
|
while data_available:
|
211
|
238
|
display_n1 = n1
|
|
239
|
+ print
|
|
240
|
+ print "n1=", n1
|
|
241
|
+ print "n2=", n2
|
212
|
242
|
stop_point = is_waveform_from_to(tn, n1, n2)
|
|
243
|
+ print "stop_point=", stop_point
|
213
|
244
|
if stop_point == 0:
|
214
|
|
- data_available = False
|
215
|
|
- print "ERROR: Stop data point index lower then start data point index"
|
216
|
|
- print
|
217
|
245
|
print_running_Python_versions()
|
|
246
|
+ logging.error("ERROR: Stop data point index is Zero while available data is True.")
|
218
|
247
|
sys.exit("ERROR")
|
219
|
248
|
elif stop_point < n1:
|
220
|
249
|
break
|
|
@@ -227,18 +256,20 @@ elif file_format == "csv":
|
227
|
256
|
n1 = n2 + 1
|
228
|
257
|
n2 += max_chunk
|
229
|
258
|
|
230
|
|
- tn.write("waveform:data?")
|
231
|
|
-
|
232
|
|
- print "Data from channel " + str(channel) + ", points " +\
|
233
|
|
- str(int(display_n1)) + "-" + str(int(stop_point)) + ": Receiving..."
|
234
|
|
- buff_chunks = tn.read_until("\n", big_wait)
|
|
259
|
+ print "Data from channel '" + str(channel) + "', points " +\
|
|
260
|
+ str(display_n1) + "-" + str(stop_point) + ": Receiving..."
|
|
261
|
+ buff_chunks = command(tn, "waveform:data?")
|
235
|
262
|
|
236
|
263
|
# Just in case the transfer did not complete in the expected time
|
237
|
264
|
while buff_chunks[-1] != "\n":
|
|
265
|
+ logging.warning("The data transfer did not complete in the expected time of " +
|
|
266
|
+ str(small_wait) + " second(s).")
|
|
267
|
+
|
238
|
268
|
tmp = tn.read_until("\n", small_wait)
|
239
|
269
|
if len(tmp) == 0:
|
240
|
270
|
break
|
241
|
271
|
buff_chunks += tmp
|
|
272
|
+ logging.warning(str(len(tmp)) + " leftover bytes added to 'buff_chunks'.")
|
242
|
273
|
|
243
|
274
|
# Append data chunks
|
244
|
275
|
# Strip TMC Blockheader and terminator bytes
|
|
@@ -280,6 +311,6 @@ elif file_format == "csv":
|
280
|
311
|
scr_file.write(csv_buff)
|
281
|
312
|
scr_file.close()
|
282
|
313
|
|
283
|
|
- print "Saved file:", filename + "." + file_format
|
|
314
|
+ print "Saved file: '", filename + "." + file_format + "'"
|
284
|
315
|
|
285
|
316
|
tn.close()
|