Browse Source

Test 24 million points extraction to CSV in small chunks.

 This is just a draft of the idea - code optimisation strongly needed.
RoGeorge 9 years ago
parent
commit
bf861fa24c
2 changed files with 135 additions and 22 deletions
  1. 64
    22
      OscScreenGrabLAN.py
  2. 71
    0
      Rigol_functions.py

+ 64
- 22
OscScreenGrabLAN.py View File

@@ -1,14 +1,22 @@
1 1
 #!/usr/bin/env python
2 2
 __author__ = 'RoGeorge'
3 3
 #
4
+"""
5
+# 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
7
+"""
8
+# TODO: Detect if the osc is in RUN or in STOP mode (looking at the length of data extracted)
9
+# TODO: Investigate scaling. Sometimes 3.0e-008 instead of expected 3.0e-000
4 10
 # TODO: Add timestamp and mark the trigger point as t0
5 11
 # TODO: Use channels label instead of chan1, chan2, chan3, chan4, math
6 12
 # TODO: Add command line parameters file path
13
+# TODO: Speed-up the transfer, try to replace Telnet with direct TCP
7 14
 # TODO: Add GUI
8 15
 # TODO: Add browse and custom filename selection
9 16
 # TODO: Create executable distributions
10 17
 #
11
-import telnetlib_receive_all
18
+from telnetlib_receive_all import Telnet
19
+from Rigol_functions import *
12 20
 import time
13 21
 import Image
14 22
 import StringIO
@@ -95,7 +103,7 @@ if response != 0:
95 103
 # Open a modified telnet session
96 104
 # The default telnetlib drops 0x00 characters,
97 105
 #   so a modified library 'telnetlib_receive_all' is used instead
98
-tn = telnetlib_receive_all.Telnet(IP_DS1104Z, port)
106
+tn = Telnet(IP_DS1104Z, port)
99 107
 tn.write("*idn?")  # ask for instrument ID
100 108
 instrument_id = tn.read_until("\n", 1)
101 109
 
@@ -161,42 +169,76 @@ elif file_format == "csv":
161 169
     print "Active channels on the display:", channel_list
162 170
 
163 171
     csv_buff = ""
172
+    depth = get_memory_depth(tn)
164 173
 
165 174
     # for each active channel
166 175
     for channel in channel_list:
167
-        # Read WAVE parameters
176
+        print
168 177
 
169 178
         # Set WAVE parameters
170 179
         tn.write("waveform:source " + channel)
171 180
         time.sleep(0.2)
172 181
 
173
-        # Maximum - only displayed data when osc. in RUN mode, or full memory data when STOPed
174
-        tn.write("waveform:mode normal")
182
+        tn.write("waveform:form asc")
175 183
         time.sleep(0.2)
176 184
 
177
-        tn.write("waveform:format ASCII")
185
+        # Maximum - only displayed data when osc. in RUN mode, or full memory data when STOPed
186
+        tn.write("waveform:mode max")
178 187
         time.sleep(0.2)
179 188
 
180
-        # Get data
181
-        tn.write("waveform:data?")
189
+        # Get all possible data
190
+        buff = ""
191
+        data_available = True
192
+
193
+        # max_chunk is dependent of the wav:mode and the oscilloscope type
194
+        # if you get on the oscilloscope screen the error message
195
+        # "Memory lack in waveform reading!", then decrease max_chunk value
196
+        max_chunk = 100000.0  # tested for DS1104Z
197
+        if max_chunk > depth:
198
+            max_chunk = depth
199
+
200
+        n1 = 1.0
201
+        n2 = max_chunk
202
+        data_available = True
203
+
204
+        while data_available:
205
+            display_n1 = n1
206
+            stop_point = is_waveform_from_to(tn, n1, n2)
207
+            if stop_point == 0:
208
+                data_available = False
209
+                print "ERROR: Stop data point index lower then start data point index"
210
+                sys.exit("ERROR")
211
+            elif stop_point < n1:
212
+                break
213
+            elif stop_point < n2:
214
+                n2 = stop_point
215
+                is_waveform_from_to(tn, n1, n2)
216
+                data_available = False
217
+            else:
218
+                data_available = True
219
+                n1 = n2 + 1
220
+                n2 += max_chunk
182 221
 
183
-        print "Receiving data for channel " + str(channel) + "..."
184
-        buff = tn.read_until("\n", big_wait)
222
+            tn.write("waveform:data?")
185 223
 
186
-        # Just in case the transfer did not complete in the expected time
187
-        while buff[-1] != "\n":
188
-            tmp = tn.read_until("\n", small_wait)
189
-            if len(tmp) == 0:
190
-                break
191
-            buff += tmp
224
+            print "Data from channel " + str(channel) + ", points " +\
225
+                  str(int(display_n1)) + "-" + str(int(stop_point)) + ": Receiving..."
226
+            buff_chunks = tn.read_until("\n", big_wait)
192 227
 
193
-        # Append each value to csv_buff
228
+            # Just in case the transfer did not complete in the expected time
229
+            while buff_chunks[-1] != "\n":
230
+                tmp = tn.read_until("\n", small_wait)
231
+                if len(tmp) == 0:
232
+                    break
233
+                buff_chunks += tmp
234
+
235
+            # Append data chunks
236
+            # Strip TMC Blockheader and terminator bytes
237
+            buff += buff_chunks[TMC_header_len:-1] + ","
194 238
 
195
-        # Strip headers (TMC and points number)
196
-        TMC_header = buff[:TMC_header_len]
197
-        data_points = float(TMC_header[2:])
198
-        # Strip TMC Blockheader and terminator bytes
199
-        buff = buff[TMC_header_len:-1]
239
+        buff = buff[:-1]
240
+
241
+        # Append each value to csv_buff
200 242
 
201 243
         # Process data
202 244
 

+ 71
- 0
Rigol_functions.py View File

@@ -0,0 +1,71 @@
1
+__author__ = 'RoGeorge'
2
+
3
+import time
4
+
5
+def get_memory_depth(tn):
6
+	# Define number of horizontal grid divisions for DS1054Z
7
+	h_grid = 12
8
+
9
+	# ACQuire:MDEPth
10
+	tn.write("ACQ:MDEP?")
11
+	mdep = tn.read_until("\n", 1)
12
+
13
+	# if mdep is "AUTO"
14
+	if mdep == "AUTO\n":
15
+		# ACQuire:SRATe
16
+		tn.write("ACQ:SRAT?")
17
+		srate = tn.read_until("\n", 1)
18
+
19
+		# TIMebase[:MAIN]:SCALe
20
+		tn.write("TIM:SCAL?")
21
+		scal = tn.read_until("\n", 1)
22
+
23
+		# mdep = h_grid * scal * srate
24
+		mdep = h_grid * float(scal) * float(srate)
25
+
26
+	# return mdep
27
+	return float(mdep)
28
+
29
+
30
+# return maximum achieved stop point, or 0 for wrong input parameters
31
+# if achieved == requested, then set the start and stop waveform as n1_d and n2_d
32
+def is_waveform_from_to(tn, n1_d, n2_d):
33
+	# read current
34
+	# WAVeform:STARt
35
+	tn.write("WAV:STAR?")
36
+	n1_c = float(tn.read_until("\n", 1))
37
+
38
+	# WAVeform:STOP
39
+	tn.write("WAV:STOP?")
40
+	n2_c = float(tn.read_until("\n", 1))
41
+
42
+	if (n1_d > n2_d) or (n1_d < 1) or (n2_d < 1):
43
+		# wrong parameters
44
+		return 0
45
+
46
+	elif n2_d < n1_c:
47
+		# first set n1_d then set n2_d
48
+		tn.write("WAV:STAR " + str(n1_d))
49
+		time.sleep(0.3)
50
+		tn.write("WAV:STOP " + str(n2_d))
51
+		time.sleep(0.3)
52
+
53
+	else:
54
+		# first set n2_d then set n1_d
55
+		tn.write("WAV:STOP " + str(n2_d))
56
+		time.sleep(0.3)
57
+		tn.write("WAV:STAR " + str(n1_d))
58
+		time.sleep(0.3)
59
+
60
+	# read achieved n2
61
+	tn.write("WAV:STOP?")
62
+	n2_a = float(tn.read_until("\n", 1))
63
+
64
+	if n2_a < n2_d:
65
+		# restore n1_c, n2_c
66
+		is_waveform_from_to(tn, n1_c, n2_c)
67
+
68
+	# return n2_a
69
+	return n2_a
70
+
71
+

Loading…
Cancel
Save