-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvxi-gps-raw.py
executable file
·526 lines (453 loc) · 15.7 KB
/
vxi-gps-raw.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env python
#
# coding: utf-8
#
# Read raw memory from the Rigol oscilloscope
# VS and MK
#
# One big mess. Sorry.
import matplotlib.pyplot as plt
import sys
import os
import time
import h5py
import numpy as np
import glob
import vxi11
import ublox # pyUblox library
import util
import datetime
DEBUG = False
#DEBUG = True
def log(s):
if DEBUG:
print s
#------------------------------------------------------------------------------
# Delete data files if so requested
filename = 0
if (filename == 1):
for f in glob.iglob("./data/*.h5"): # delete all .h5 files
print 'Deleting', f
os.remove(f)
else:
print 'Not removing old files, as filename {0} is not 1.'.format(filename)
# Set the initial delay for frame capture
#start_wfd = 0.005 #0.05 #0.012
start_wfd = 0.02
wfd=start_wfd
#------------------------------------------------------------------------------
# This will need a rewrite
class TmcDriver:
def __init__(self, device):
print("Initializing connection to: " + device)
self.device = device
self.instr = vxi11.Instrument(device)
def write(self, command):
#print command
self.instr.write(command);
def read(self, length = 500):
return self.instr.read(length)
def read_raw(self, length = 500):
return self.instr.read_raw(length)
def getName(self):
self.write("*IDN?")
return self.read(300)
def ask(self, command):
return self.instr.ask(command)
def sendReset(self):
self.write("*RST") # Be carefull, this real resets an oscilloscope
def read_this_frame_raw():
osc.write(':WAV:MODE RAW')
osc.write(':WAV:FORM BYTE')
osc.write(':ACQ:MDEP?')
mdepth = float(osc.read(100))
print 'MDEPTH:', int(mdepth),
osc.write(':STOP')
time.sleep(1)
retry_count = 3
while (retry_count > 0):
retry_count = retry_count - 1
osc.write(':WAV:STAR 1')
time.sleep(0.5)
osc.write(':WAV:STOP '+"{0:.0f}".format(mdepth))
time.sleep(0.5)
osc.write(':WAV:RES')
time.sleep(1)
osc.write(':WAV:BEG')
time.sleep(1)
wavepart = bytearray()
lastwave = bytearray()
bytesread = 0
flag = 1
nowwaiting = 0
chrup = 0.25
# We will try to read the waveform three times
while (nowwaiting < 5.0):
time.sleep(0.2)
status = osc.ask(':WAV:STAT?')
read_status = status[:4]
bytes_to_read = int(status[5:])
bytesread = bytesread + bytes_to_read
log(' '+status)
if (read_status == 'IDLE' and bytes_to_read == 0):
break
if (read_status == 'READ' or read_status == 'IDLE'):
if (bytes_to_read > 0):
osc.write(':WAV:DATA?')
time.sleep(0.2)
wavepart = bytearray(osc.read_raw(bytes_to_read))
lastwave = lastwave + wavepart
sys.stdout.write(' '+str(len(wavepart))+' ')
sys.stdout.flush()
time.sleep(0)
nowwaiting = 0
else:
# There are no data to read now
time.sleep(chrup)
nowwaiting = nowwaiting + chrup
if (flag == 0):
break
if (read_status == 'IDLE'):
flag = 0
osc.write(':WAV:END')
if (len(lastwave) == mdepth and mdepth == bytesread):
log(' Expected and read bytes match: '+str(mdepth))
break
else:
print ' Adjust delay after BEG - we are loosing data somewhere.'
return(lastwave)
def get_one_frame_raw(thetime,ns):
global filename
global wfd
while True:
osc.write(':FUNC:WREC:OPER?') # finish recording?
reply = osc.read()
if reply == 'STOP':
run_time = round(time.time() - run_start_time, 2)
log(' Frame capture finished after %.2f seconds.' % run_time)
break
time.sleep(0.05)
for channel in ['CHAN1','CHAN2']:
if (osc.ask(':'+channel+':DISP?') == u'0'):
log(channel+' is not enabled')
else:
log('Reading out '+channel)
osc.write(':WAV:SOUR '+channel)
osc.write(':WAV:MODE RAW')
osc.write(':WAV:FORM BYTE')
print 'TIME:', thetime,
print 'NSPART:', ns
# Read oscilloscope settings
osc.write(':ACQ:MDEP?')
mdepth = float(osc.read(100))
print 'MDEPTH:', int(mdepth),
osc.write(':WAV:XINC?')
xinc = float(osc.read(100))
print 'XINC:', xinc,
osc.write(':WAV:YINC?')
yinc = float(osc.read(100))
print 'YINC:', yinc,
osc.write(':TRIGger:EDGe:LEVel?')
trig = float(osc.read(100))
print 'TRIG:', trig,
osc.write(':WAVeform:YORigin?')
yorig = float(osc.read(100))
print 'YORIGIN:', yorig,
osc.write(':WAVeform:XORigin?')
xorig = float(osc.read(100))
print 'XORIGIN:', xorig,
osc.write(':WAVeform:XREFerence?')
xref = float(osc.read(100))
print 'XREF:', xref
osc.write(':FUNC:WREP:FEND?') # get number of last frame
frames = int(osc.read(100))
print ' FRAMES:', frames, 'SUBRUN', filename
wave = bytearray()
lastwave = bytearray()
with h5py.File('./data/data'+'{:02.0f}'.format(filename)+'_'+str(int(round(run_start_time,0)))+'-'+channel+'.h5', 'w') as hf:
hf.create_dataset('FRAMES', data=(frames)) # write number of frames
hf.create_dataset('XINC', data=(xinc)) # write axis parameters
hf.create_dataset('YINC', data=(yinc))
hf.create_dataset('TRIG', data=(trig))
hf.create_dataset('YORIGIN', data=(yorig))
hf.create_dataset('XORIGIN', data=(xorig))
hf.create_dataset('XREFERENCE', data=(xref))
hf.create_dataset('THETIME', data=(thetime))
hf.create_dataset('NSPART', data=(ns))
hf.create_dataset('CAPTURING', data=(run_time))
if (frames > 1):
osc.write(':FUNC:WREP:FCUR 1') # go to first frame
time.sleep(0.5)
for n in range(1,frames+1):
if (frames > 1):
osc.write(':FUNC:WREP:FCUR ' + str(n)) # skip to n-th frame
while True:
time.sleep(0.05)
fcur = osc.ask(':FUNC:WREP:FCUR?')
if (str(n) == fcur):
# Rigol returns correct current frame
sys.stdout.write(str(n))
break
else:
# Rigol has not yet made the seek, wait
# or consider extending the sleep above
print("Needwait: "+str(n)+' vs '+fcur)
reread_count = 0
while True:
# Read this frame
wave = read_this_frame_raw()
if (np.array_equal(wave, lastwave)):
wfd = wfd + 0.005
print(' Same waveform, wait ' + str(wfd) + ' and reread')
reread_count = reread_count + 1
if (reread_count > 5):
print('------------ Wrong trigger level?')
else:
hf.create_dataset(str(n), data=wave)
lastwave = wave
sys.stdout.write('O')
wfd = start_wfd
break
print 'K'
# End of channel recording
def get_one_frame(thetime,ns):
global filename
global wfd
while True:
osc.write(':FUNC:WREC:OPER?') # finish recording?
reply = osc.read()
if reply == 'STOP':
run_time = round(time.time() - run_start_time, 2)
log(' Frame capture finished after %.2f seconds.' % run_time)
break
time.sleep(0.05)
for channel in ['CHAN1','CHAN2']:
if (osc.ask(':'+channel+':DISP?') == u'0'):
log(channel+' is not enabled')
else:
log('Reading out '+channel)
osc.write(':WAV:SOUR '+channel)
print ' TIME:', thetime,
print ' NSPART:', ns
#------------------------------
# RAW
print ' RAW ',
osc.write(':WAV:MODE RAW')
osc.write(':WAV:FORM BYTE')
#osc.write(':WAV:POIN 28000')
osc.write(':WAV:XINC?')
xincr = float(osc.read(100))
print 'XINC:', xincr,
osc.write(':WAV:YINC?')
yincr = float(osc.read(100))
print 'YINC:', yincr,
osc.write(':TRIGger:EDGe:LEVel?')
trigr = float(osc.read(100))
print 'TRIG:', trigr,
osc.write(':WAVeform:YORigin?')
yorigr = float(osc.read(100))
print 'YORIGIN:', yorigr,
osc.write(':WAVeform:XORigin?')
xorigr = float(osc.read(100))
print 'XORIGIN:', xorigr,
osc.write(':WAVeform:XREFerence?')
xrefr = float(osc.read(100))
print 'XREF:', xrefr
#-------------------------------
print ' NORMAL ',
# NORM
osc.write(':WAV:MODE NORM')
osc.write(':WAV:FORM BYTE')
osc.write(':WAV:POIN 1400')
osc.write(':WAV:XINC?')
xinc = float(osc.read(100))
print 'XINC:', xinc,
osc.write(':WAV:YINC?')
yinc = float(osc.read(100))
print 'YINC:', yinc,
osc.write(':TRIGger:EDGe:LEVel?')
trig = float(osc.read(100))
print 'TRIG:', trig,
osc.write(':WAVeform:YORigin?')
yorig = float(osc.read(100))
print 'YORIGIN:', yorig,
osc.write(':WAVeform:XORigin?')
xorig = float(osc.read(100))
print 'XORIGIN:', xorig,
osc.write(':WAVeform:XREFerence?')
xref = float(osc.read(100))
print 'XREF:', xref,
#---------------------------
osc.write(':FUNC:WREP:FEND?') # get number of last frame
frames = int(osc.read(100))
print 'FRAMES:', frames, 'SUBRUN', filename
lastwave = bytearray()
with h5py.File('./data/data'+'{:02.0f}'.format(filename)+'_'+str(int(round(run_start_time,0)))+'-'+channel+'.h5', 'w') as hf:
hf.create_dataset('FRAMES', data=(frames)) # write number of frames
hf.create_dataset('XINC', data=(xinc)) # write axis parameters
hf.create_dataset('YINC', data=(yinc))
hf.create_dataset('TRIG', data=(trig))
hf.create_dataset('YORIGIN', data=(yorig))
hf.create_dataset('XORIGIN', data=(xorig))
hf.create_dataset('XREFERENCE', data=(xref))
# These are for the RAW writeout
hf.create_dataset('XINCR', data=(xincr)) # write axis parameters
hf.create_dataset('YINCR', data=(yincr))
hf.create_dataset('TRIGR', data=(trigr))
hf.create_dataset('YORIGINR', data=(yorigr))
hf.create_dataset('XORIGINR', data=(xorigr))
hf.create_dataset('XREFERENCER', data=(xrefr))
hf.create_dataset('THETIME', data=(thetime))
hf.create_dataset('NSPART', data=(ns))
hf.create_dataset('CAPTURING', data=(run_time))
if (frames > 1):
osc.write(':FUNC:WREP:FCUR 1') # go to first frame
time.sleep(0.5)
for n in range(1,frames+1):
if (frames > 1):
osc.write(':FUNC:WREP:FCUR ' + str(n)) # skip to n-th frame
while True:
time.sleep(0.05)
fcur = osc.ask(':FUNC:WREP:FCUR?')
if (str(n) == fcur):
# Rigol returns correct current frame
print(str(n)+':')
break
else:
# Rigol has not yet made the seek, wait
# or consider extending the sleep above
print("Needwait: "+str(n)+' vs '+fcur)
reread_count = 0
lastwave = bytearray()
# Now, get the frame in NORMAL mode
print ' NORMAL',
osc.write(':WAV:MODE NORM')
osc.write(':WAV:FORM BYTE')
osc.write(':WAV:POIN 1400')
while True:
time.sleep(wfd)
osc.write(':WAV:DATA?') # read data
time.sleep(wfd)
wave1 = bytearray(osc.read_raw(500))
wave2 = bytearray(osc.read_raw(500))
wave3 = bytearray(osc.read_raw(500))
#wave4 = bytearray(osc.read(500))
#wave = np.concatenate((wave1[11:],wave2[:(500-489)],wave3[:(700-489)]))
wave = np.concatenate((wave1[11:],wave2,wave3[:-1]))
if (np.array_equal(wave, lastwave)):
wfd = wfd + 0.005
print(' Same waveform, wait ' + str(wfd) + ' and reread')
reread_count = reread_count + 1
if (reread_count > 5):
print('------------ Wrong trigger level?')
else:
hf.create_dataset(str(n), data=wave)
lastwave = wave
print(' OK ')
wfd = start_wfd
break
# Read this frame in RAW mode
reread_count = 0
lastwave = bytearray()
print ' RAW',
while True:
# Read this frame
wave = read_this_frame_raw()
if (np.array_equal(wave, lastwave)):
wfd = wfd + 0.005
print(' Same waveform, wait ' + str(wfd) + ' and reread')
reread_count = reread_count + 1
if (reread_count > 5):
print('------------ Wrong trigger level?')
else:
hf.create_dataset(str(n)+'R', data=wave)
lastwave = wave
print(' OK')
wfd = start_wfd
break
print ' '
# End of channel recording
# Initialize ethernet link to oscilloscope
# For Ethernet
#osc = TmcDriver("TCPIP::10.1.1.3::INSTR")
osc = TmcDriver("TCPIP::192.168.1.166::INSTR")
print(osc.ask("*IDN?"))
osc.write(':STOP') # go to STOP mode
time.sleep(0.5)
# Initialize GPS serial link
# If the device was disconnected briefly, this can jump to ACM1 or whatever
gpsport = '/dev/ttyACM0'
gpsbaudrate = 921600
# Open GPS port
dev = ublox.UBlox(gpsport, baudrate=gpsbaudrate, timeout=0)
print "# Configured to listen at " + gpsport, ' forcing trigger.'
# Prepare the oscilloscope
# Switch to single trigger mode and do one trigger
time.sleep(1)
osc.write(':SINGle')
time.sleep(1)
osc.write(':TFORce')
# ----------------------------
# Read GPS messages, if any
last_count = -1
first_count = 0
while True:
try:
msg = dev.receive_message()
except:
continue
if (msg is None):
pass#break
else:
#print msg.name()
if msg.name() == 'TIM_TM2':
#print('Got TM2 message')
#try:
msg.unpack()
if (last_count < 0):
first_count = msg.count
last_count = first_count
print('# May the force be with you. Setting first count to '+str(first_count))
run_start_time = time.time()
time.sleep(wfd)
log(' Starting single capture at time: %.2f' % run_start_time)
osc.write(':SINGle')
else:
# If this is not a duplicate message
if (msg.count > last_count):
timestring = '$HIT,'
# Edges since first count
timestring += str(msg.count-first_count)
timestring += ','
# Edges in this run
timestring += str(msg.count-last_count)
timestring += ','
# Computer time
timestring += str(datetime.datetime.utcnow())
timestring += ','
# This provides the rising edge time to microsecond precision
timestring += str(util.gpsTimeToTime(msg.wnR, 1.0e-3*msg.towMsR + 1.0e-9*msg.towSubMsR))
timestring += ','
timestring += str(datetime.datetime.utcfromtimestamp(util.gpsTimeToTime(msg.wnR, 1.0e-3*msg.towMsR + 1.0e-9*msg.towSubMsR)))
#timestring += str(datetime.datetime.utcfromtimestamp(util.gpsTimeToTime(msg.wnR, 1.0e6*(msg.towMsR % 1000 + msg.SubMsR)))
timestring += ','
# Tow sub millisecond fraction, in nanoseconds
timestring += str(msg.towMsR)
timestring += ','
# Tow sub millisecond fraction, in nanoseconds
timestring += str(msg.towSubMsR)
log(timestring)
sys.stdout.flush()
last_count = msg.count
# Get the data frame from oscilloscope
get_one_frame(util.gpsTimeToTime(msg.wnR, 1.0e-3*msg.towMsR + 1.0e-9*msg.towSubMsR), msg.towSubMsR)
filename = filename + 1
# and go again for single capture
time.sleep(0.5)
run_start_time = time.time()
log(' Run start time: %.2f' % run_start_time)
log(' Capturing...')
osc.write(':SINGle')
#
# else:
# print('# DUP')