Browse Source

Add waveform data capture/export to CSV capability

RoGeorge 9 years ago
parent
commit
0efdeb9f7c
1 changed files with 153 additions and 48 deletions
  1. 153
    48
      OscScreenGrabLAN.py

+ 153
- 48
OscScreenGrabLAN.py View File

@@ -1,8 +1,7 @@
1 1
 #!/usr/bin/env python
2 2
 __author__ = 'RoGeorge'
3 3
 #
4
-# TODO: Add command line parameters for IP, file path and file format
5
-# TODO: Add data capture/export capabilities
4
+# TODO: Add command line parameters file path
6 5
 # TODO: Add GUI
7 6
 # TODO: Add browse and custom filename selection
8 7
 # TODO: Create executable distributions
@@ -37,30 +36,43 @@ serial = 2
37 36
 # Check parameters
38 37
 script_name = os.path.basename(sys.argv[0])
39 38
 
40
-# Print usage
41
-print
42
-print "Usage:"
43
-print "    " + script_name + " [oscilloscope_IP [save_path [png | bmp]]]"
44
-print
45
-print "Usage examples:"
46
-print "    " + script_name
47
-print "    " + script_name + " 192.168.1.3"
48
-print "    " + script_name + " 192.168.1.3 my_place_for_osc_captures"
49
-print "    " + script_name + " 192.168.1.3 my_place_for_osc_captures BMP"
50
-print
51
-print "This program capture the image displayed"
52
-print "    by a Rigol DS1000Z series oscilloscope, then save it on the computer"
53
-print "    as a PNG or BMP file with a timestamp in the file name."
54
-print
55
-print "    The program is using LXI protocol, so the computer"
56
-print "    must have LAN connection with the oscilloscope."
57
-print "    USB and/or GPIB connections are not used by this software."
58
-print
59
-print "    No VISA, IVI or Rigol drivers are needed."
60
-print
61 39
 
62
-# Create/check if 'path' exists
40
+def print_help():
41
+    # Print usage
42
+    print
43
+    print "Usage:"
44
+    print "    " + script_name + " png|bmp|csv [oscilloscope_IP [save_path]]"
45
+    print
46
+    print "Usage examples:"
47
+    print "    " + script_name + " png"
48
+    print
49
+    print "The following usage cases are not yet implemented:"
50
+    print "    " + script_name + " csv 192.168.1.3"
51
+    print "    " + script_name + " bmp 192.168.1.3 my_place_for_osc_bmp_captures"
52
+    print
53
+    print "This program captures either the waveform or the whole screen"
54
+    print "    of a Rigol DS1000Z series oscilloscope, then save it on the computer"
55
+    print "    as a CSV, PNG or BMP file with a timestamp in the file name."
56
+    print
57
+    print "    The program is using LXI protocol, so the computer"
58
+    print "    must have LAN connection with the oscilloscope."
59
+    print "    USB and/or GPIB connections are not used by this software."
60
+    print
61
+    print "    No VISA, IVI or Rigol drivers are needed."
62
+    print
63
+
64
+
65
+if len(sys.argv) <= 1:
66
+    print_help()
67
+    sys.exit("Warning - wrong command line parameters.")
68
+elif sys.argv[1].lower() not in ["png", "bmp", "csv"]:
69
+    print_help()
70
+    print "This file type is not supported: ", sys.argv[1]
71
+    sys.exit("ERROR")
72
+
73
+file_format = sys.argv[1].lower()
63 74
 
75
+# Create/check if 'path' exists
64 76
 
65 77
 # Check network response (ping)
66 78
 if platform.system() == "Windows":
@@ -103,29 +115,122 @@ print instrument_id
103 115
 timestamp = time.strftime("%Y-%m-%d_%H.%M.%S", time.localtime())
104 116
 filename = path_to_save + id_fields[model] + "_" + id_fields[serial] + "_" + timestamp
105 117
 
106
-# Ask for an oscilloscope display print screen
107
-tn.write("display:data?")
108
-print "Receiving..."
109
-buff = tn.read_until("\n", big_wait)
110
-
111
-# Just in case the transfer did not complete in the expected time
112
-while len(buff) < expected_len:
113
-    tmp = tn.read_until("\n", small_wait)
114
-    if len(tmp) == 0:
115
-        break
116
-    buff += tmp
117
-
118
-# Strip TMC Blockheader and terminator bytes
119
-buff = buff[TMC_header_len:-terminator_len]
120
-
121
-# Save as PNG
122
-im = Image.open(StringIO.StringIO(buff))
123
-im.save(filename + ".png", "PNG")
124
-print "Saved file:", filename + ".png"
125
-
126
-# Save as BMP
127
-# scr_file = open(filename + ".bmp", "wb")
128
-# scr_file.write(buff)
129
-# scr_file.close()
118
+if file_format in ["png", "bmp"]:
119
+    # Ask for an oscilloscope display print screen
120
+    tn.write("display:data?")
121
+    print "Receiving..."
122
+    buff = tn.read_until("\n", big_wait)
123
+
124
+    # Just in case the transfer did not complete in the expected time
125
+    while len(buff) < expected_len:
126
+        tmp = tn.read_until("\n", small_wait)
127
+        if len(tmp) == 0:
128
+            break
129
+        buff += tmp
130
+
131
+    # Strip TMC Blockheader and terminator bytes
132
+    buff = buff[TMC_header_len:-terminator_len]
133
+
134
+    # Save as PNG or BMP according to file_format
135
+    im = Image.open(StringIO.StringIO(buff))
136
+    im.save(filename + "." + file_format, file_format)
137
+    print "Saved file:", filename + "." + file_format
138
+
139
+elif file_format == "csv":
140
+    print "csv mode"
141
+    # Put osc in STOP mode
142
+    # tn.write("stop")
143
+    # response = tn.read_until("\n", 1)
144
+
145
+    # Scan for displayed channels
146
+    channel_list = []
147
+    for channel in ["chan1", "chan2", "chan3", "chan4", "math"]:
148
+        tn.write(channel + ":display?")
149
+        response = tn.read_until("\n", 1)
150
+        # Strip '\n' terminator
151
+        response = response[:-1]
152
+        if response == '1':
153
+            channel_list += [channel]
154
+
155
+    print channel_list
156
+
157
+    csv_buff = ""
158
+
159
+    # for each active channel
160
+    for channel in channel_list:
161
+        # Read WAVE parameters
162
+
163
+        # Set WAVE parameters
164
+        tn.write("waveform:source " + channel)
165
+        response = tn.read_until("\n", 1)
166
+
167
+        # Maximum - only displayed data when osc. in RUN mode, or full memory data when STOPed
168
+        tn.write("waveform:mode maximum")
169
+        response = tn.read_until("\n", 1)
170
+
171
+        tn.write("waveform:format ASCII")
172
+        response = tn.read_until("\n", 1)
173
+
174
+        # Get data
175
+        tn.write("waveform:data?")
176
+
177
+        print "Receiving..."
178
+        buff = tn.read_until("\n", big_wait)
179
+
180
+        # Just in case the transfer did not complete in the expected time
181
+        while buff[-1] != "\n":
182
+            tmp = tn.read_until("\n", small_wait)
183
+            if len(tmp) == 0:
184
+                break
185
+            buff += tmp
186
+
187
+        # Append each value to csv_buff
188
+
189
+        # Strip headers (TMC and points number)
190
+        TMC_header = buff[:TMC_header_len]
191
+        data_points = float(TMC_header[2:])
192
+        # Strip TMC Blockheader and terminator bytes
193
+        buff = buff[TMC_header_len:-1]
194
+
195
+        # Process data
196
+        print buff
197
+        print
198
+        print "data_points =", data_points, "from", channel
199
+
200
+        buff_list = buff.split(",")
201
+        buff_rows = len(buff_list)
202
+
203
+        print buff_list
204
+
205
+        # Put red data into csv_buff
206
+        csv_buff_list = csv_buff.split(os.linesep)
207
+        csv_rows = len(csv_buff_list)
208
+
209
+        current_row = 0
210
+        if csv_buff == "":
211
+            csv_first_column = True
212
+            csv_buff = str(channel) + os.linesep
213
+        else:
214
+            csv_first_column = False
215
+            csv_buff = str(csv_buff_list[current_row]) + "," + str(channel) + os.linesep
216
+
217
+        for point in buff_list:
218
+            current_row += 1
219
+            if csv_first_column:
220
+                csv_buff += str(point) + os.linesep
221
+            else:
222
+                if current_row < csv_rows:
223
+                    csv_buff += str(csv_buff_list[current_row]) + "," + str(point) + os.linesep
224
+                else:
225
+                    csv_buff += "," + str(point) + os.linesep
226
+
227
+        print csv_buff
228
+
229
+    # Save data as CSV
230
+    scr_file = open(filename + "." + file_format, "wb")
231
+    scr_file.write(csv_buff)
232
+    scr_file.close()
233
+
234
+    print "Saved file:", filename + "." + file_format
130 235
 
131 236
 tn.close()

Loading…
Cancel
Save