Browse Source

Intermediary commit before push

RoGeorge 7 years ago
parent
commit
34bb2f564a
4 changed files with 209 additions and 57 deletions
  1. 98
    0
      .gitignore
  2. 66
    35
      OscScreenGrabLAN.py
  3. 33
    22
      Rigol_functions.py
  4. 12
    0
      captures/.gitignore

+ 98
- 0
.gitignore View File

1
+# *** START section for manually edited lines
2
+
3
+# ignore debugging log generated by this project
4
+*.py.log
5
+
6
+# *** STOP section for manually edited lines
7
+
8
+
9
+
10
+# Byte-compiled / optimized / DLL files
11
+__pycache__/
12
+*.py[cod]
13
+*$py.class
14
+
15
+# C extensions
16
+*.so
17
+
18
+# Distribution / packaging
19
+.Python
20
+env/
21
+build/
22
+develop-eggs/
23
+dist/
24
+downloads/
25
+eggs/
26
+.eggs/
27
+lib/
28
+lib64/
29
+parts/
30
+sdist/
31
+var/
32
+*.egg-info/
33
+.installed.cfg
34
+*.egg
35
+
36
+# PyInstaller
37
+#  Usually these files are written by a python script from a template
38
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
39
+*.manifest
40
+*.spec
41
+
42
+# Installer logs
43
+pip-log.txt
44
+pip-delete-this-directory.txt
45
+
46
+# Unit test / coverage reports
47
+htmlcov/
48
+.tox/
49
+.coverage
50
+.coverage.*
51
+.cache
52
+nosetests.xml
53
+coverage.xml
54
+*,cover
55
+.hypothesis/
56
+
57
+# Translations
58
+*.mo
59
+*.pot
60
+
61
+# Django stuff:
62
+*.log
63
+local_settings.py
64
+
65
+# Flask stuff:
66
+instance/
67
+.webassets-cache
68
+
69
+# Scrapy stuff:
70
+.scrapy
71
+
72
+# Sphinx documentation
73
+docs/_build/
74
+
75
+# PyBuilder
76
+target/
77
+
78
+# IPython Notebook
79
+.ipynb_checkpoints
80
+
81
+# pyenv
82
+.python-version
83
+
84
+# celery beat schedule file
85
+celerybeat-schedule
86
+
87
+# dotenv
88
+.env
89
+
90
+# virtualenv
91
+venv/
92
+ENV/
93
+
94
+# Spyder project settings
95
+.spyderproject
96
+
97
+# Rope project settings
98
+.ropeproject

+ 66
- 35
OscScreenGrabLAN.py View File

1
 #!/usr/bin/env python
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
 __author__ = 'RoGeorge'
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
 # TODO: Use "waveform:data?" multiple times to extract the whole 12M points
29
 # TODO: Use "waveform:data?" multiple times to extract the whole 12M points
6
           in order to overcome the "Memory lack in waveform reading!" screen message
30
           in order to overcome the "Memory lack in waveform reading!" screen message
15
 # TODO: Add browse and custom filename selection
39
 # TODO: Add browse and custom filename selection
16
 # TODO: Create executable distributions
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
 # Update the next lines for your own default settings:
49
 # Update the next lines for your own default settings:
28
 path_to_save = "captures/"
50
 path_to_save = "captures/"
106
 # The default telnetlib drops 0x00 characters,
128
 # The default telnetlib drops 0x00 characters,
107
 #   so a modified library 'telnetlib_receive_all' is used instead
129
 #   so a modified library 'telnetlib_receive_all' is used instead
108
 tn = Telnet(IP_DS1104Z, port)
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
 # Check if instrument is set to accept LAN commands
133
 # Check if instrument is set to accept LAN commands
113
 if instrument_id == "command error":
134
 if instrument_id == "command error":
137
 
158
 
138
 if file_format in ["png", "bmp"]:
159
 if file_format in ["png", "bmp"]:
139
     # Ask for an oscilloscope display print screen
160
     # Ask for an oscilloscope display print screen
140
-    tn.write("display:data?")
141
     print "Receiving screen capture..."
161
     print "Receiving screen capture..."
142
-    buff = tn.read_until("\n", big_wait)
162
+    buff = command(tn, "display:data?")
143
 
163
 
144
     # Just in case the transfer did not complete in the expected time
164
     # Just in case the transfer did not complete in the expected time
145
     while len(buff) < expected_len:
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
         tmp = tn.read_until("\n", small_wait)
168
         tmp = tn.read_until("\n", small_wait)
147
         if len(tmp) == 0:
169
         if len(tmp) == 0:
148
             break
170
             break
149
         buff += tmp
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
     # Strip TMC Blockheader and terminator bytes
179
     # Strip TMC Blockheader and terminator bytes
152
     buff = buff[TMC_header_len:-terminator_len]
180
     buff = buff[TMC_header_len:-terminator_len]
164
     # Scan for displayed channels
192
     # Scan for displayed channels
165
     channel_list = []
193
     channel_list = []
166
     for channel in ["chan1", "chan2", "chan3", "chan4", "math"]:
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
         # Strip '\n' terminator
197
         # Strip '\n' terminator
171
         response = response[:-1]
198
         response = response[:-1]
182
         print
209
         print
183
 
210
 
184
         # Set WAVE parameters
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
         # Get all possible data
219
         # Get all possible data
196
         buff = ""
220
         buff = ""
199
         # max_chunk is dependent of the wav:mode and the oscilloscope type
223
         # max_chunk is dependent of the wav:mode and the oscilloscope type
200
         # if you get on the oscilloscope screen the error message
224
         # if you get on the oscilloscope screen the error message
201
         # "Memory lack in waveform reading!", then decrease max_chunk value
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
         if max_chunk > depth:
230
         if max_chunk > depth:
204
             max_chunk = depth
231
             max_chunk = depth
205
 
232
 
206
-        n1 = 1.0
233
+        print "max_chunk=", max_chunk
234
+        n1 = 1
207
         n2 = max_chunk
235
         n2 = max_chunk
208
-        data_available = True
209
-
236
+        print "n2=", n2
210
         while data_available:
237
         while data_available:
211
             display_n1 = n1
238
             display_n1 = n1
239
+            print
240
+            print "n1=", n1
241
+            print "n2=", n2
212
             stop_point = is_waveform_from_to(tn, n1, n2)
242
             stop_point = is_waveform_from_to(tn, n1, n2)
243
+            print "stop_point=", stop_point
213
             if stop_point == 0:
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
                 print_running_Python_versions()
245
                 print_running_Python_versions()
246
+                logging.error("ERROR: Stop data point index is Zero while available data is True.")
218
                 sys.exit("ERROR")
247
                 sys.exit("ERROR")
219
             elif stop_point < n1:
248
             elif stop_point < n1:
220
                 break
249
                 break
227
                 n1 = n2 + 1
256
                 n1 = n2 + 1
228
                 n2 += max_chunk
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
             # Just in case the transfer did not complete in the expected time
263
             # Just in case the transfer did not complete in the expected time
237
             while buff_chunks[-1] != "\n":
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
                 tmp = tn.read_until("\n", small_wait)
268
                 tmp = tn.read_until("\n", small_wait)
239
                 if len(tmp) == 0:
269
                 if len(tmp) == 0:
240
                     break
270
                     break
241
                 buff_chunks += tmp
271
                 buff_chunks += tmp
272
+                logging.warning(str(len(tmp)) + " leftover bytes added to 'buff_chunks'.")
242
 
273
 
243
             # Append data chunks
274
             # Append data chunks
244
             # Strip TMC Blockheader and terminator bytes
275
             # Strip TMC Blockheader and terminator bytes
280
     scr_file.write(csv_buff)
311
     scr_file.write(csv_buff)
281
     scr_file.close()
312
     scr_file.close()
282
 
313
 
283
-    print "Saved file:", filename + "." + file_format
314
+    print "Saved file: '", filename + "." + file_format + "'"
284
 
315
 
285
 tn.close()
316
 tn.close()

+ 33
- 22
Rigol_functions.py View File

3
 import time
3
 import time
4
 import pip
4
 import pip
5
 import sys
5
 import sys
6
+import logging
6
 
7
 
7
 
8
 
8
 def print_running_Python_versions():
9
 def print_running_Python_versions():
18
     print
19
     print
19
 
20
 
20
 
21
 
22
+def command(tn, SCPI):
23
+    logging.info("SCPI to be sent: " + SCPI)
24
+    answer_wait_s = 1
25
+    response = ""
26
+    while response != "1\n":
27
+        tn.write("*OPC?")  # previous operation(s) has completed ?
28
+        logging.info("Send SCPI: *OPC?")
29
+        response = tn.read_until("\n", 1)  # wait max 1s for an answer
30
+        logging.info("Received response: " + response)
31
+
32
+    tn.write(SCPI)
33
+    logging.info("Sent SCPI: " + SCPI)
34
+    response = tn.read_until("\n", answer_wait_s)
35
+    logging.info("Received response: " + response)
36
+    return response
37
+
38
+
21
 def get_memory_depth(tn):
39
 def get_memory_depth(tn):
22
     # Define number of horizontal grid divisions for DS1054Z
40
     # Define number of horizontal grid divisions for DS1054Z
23
     h_grid = 12
41
     h_grid = 12
24
 
42
 
25
     # ACQuire:MDEPth
43
     # ACQuire:MDEPth
26
-    tn.write("ACQ:MDEP?")
27
-    mdep = tn.read_until("\n", 1)
44
+    mdep = command(tn, "ACQ:MDEP?")
28
 
45
 
29
     # if mdep is "AUTO"
46
     # if mdep is "AUTO"
30
     if mdep == "AUTO\n":
47
     if mdep == "AUTO\n":
31
         # ACQuire:SRATe
48
         # ACQuire:SRATe
32
-        tn.write("ACQ:SRAT?")
33
-        srate = tn.read_until("\n", 1)
49
+        srate = command(tn, "ACQ:SRAT?")
34
 
50
 
35
         # TIMebase[:MAIN]:SCALe
51
         # TIMebase[:MAIN]:SCALe
36
-        tn.write("TIM:SCAL?")
37
-        scal = tn.read_until("\n", 1)
52
+        scal = command(tn, "TIM:SCAL?")
38
 
53
 
39
         # mdep = h_grid * scal * srate
54
         # mdep = h_grid * scal * srate
40
-        mdep = h_grid * float(scal) * float(srate)
55
+        mdep = h_grid * scal * srate
41
 
56
 
42
     # return mdep
57
     # return mdep
43
-    return float(mdep)
58
+    return int(mdep)
44
 
59
 
45
 
60
 
46
 # return maximum achieved stop point, or 0 for wrong input parameters
61
 # return maximum achieved stop point, or 0 for wrong input parameters
48
 def is_waveform_from_to(tn, n1_d, n2_d):
63
 def is_waveform_from_to(tn, n1_d, n2_d):
49
     # read current
64
     # read current
50
     # WAVeform:STARt
65
     # WAVeform:STARt
51
-    tn.write("WAV:STAR?")
52
-    n1_c = float(tn.read_until("\n", 1))
66
+    n1_c = int(command(tn, "WAV:STAR?"))
53
 
67
 
54
     # WAVeform:STOP
68
     # WAVeform:STOP
55
-    tn.write("WAV:STOP?")
56
-    n2_c = float(tn.read_until("\n", 1))
69
+    n2_c = int(command(tn, "WAV:STOP?"))
57
 
70
 
58
     if (n1_d > n2_d) or (n1_d < 1) or (n2_d < 1):
71
     if (n1_d > n2_d) or (n1_d < 1) or (n2_d < 1):
59
         # wrong parameters
72
         # wrong parameters
61
 
74
 
62
     elif n2_d < n1_c:
75
     elif n2_d < n1_c:
63
         # first set n1_d then set n2_d
76
         # first set n1_d then set n2_d
64
-        tn.write("WAV:STAR " + str(n1_d))
65
-        time.sleep(1)
66
-        tn.write("WAV:STOP " + str(n2_d))
67
-        time.sleep(1)
77
+
78
+        print "a ", "n1_d=", n1_d, "n2_d=", n2_d
79
+        command(tn, "WAV:STAR " + str(n1_d))
80
+        command(tn, "WAV:STOP " + str(n2_d))
68
 
81
 
69
     else:
82
     else:
70
         # first set n2_d then set n1_d
83
         # first set n2_d then set n1_d
71
-        tn.write("WAV:STOP " + str(n2_d))
72
-        time.sleep(1)
73
-        tn.write("WAV:STAR " + str(n1_d))
74
-        time.sleep(1)
84
+        print "b ", "n2_d", n2_d, "n1_d=", n1_d
85
+        command(tn, "WAV:STOP " + str(n2_d))
86
+        command(tn, "WAV:STAR " + str(n1_d))
75
 
87
 
76
     # read achieved n2
88
     # read achieved n2
77
-    tn.write("WAV:STOP?")
78
-    n2_a = float(tn.read_until("\n", 1))
89
+    n2_a = int(command(tn, "WAV:STOP?"))
79
 
90
 
80
     if n2_a < n2_d:
91
     if n2_a < n2_d:
81
         # restore n1_c, n2_c
92
         # restore n1_c, n2_c

+ 12
- 0
captures/.gitignore View File

1
+# *** START section for manually edited lines
2
+
3
+# 'captures' is a mandatory directory for this project,
4
+#     but git can add only files, and can not add an empty directory.
5
+# We want to preserve 'captures' in the repository tree structure,
6
+#     so we will ignore everything except the 'captures/.gitignore' file.
7
+#     This trick will always add the 'captures' directory in the repository,
8
+#     even when there are no other files in 'captures'.
9
+*
10
+!.gitignore
11
+
12
+# *** STOP section for manually edited lines

Loading…
Cancel
Save