-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
executable file
·102 lines (81 loc) · 2.86 KB
/
logger.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
#!/usr/bin/env python3
r"""
@author Josh Wood
@date Dec 13, 2019
@brief Logs waveforms from a SIGLENT SDS 1104X-E scope
usage: ./logger.py -c <channel> -o <dir> -n <number>
"""
import os
import time
import pyvisa
import argparse
from datetime import datetime
# arguments
p = argparse.ArgumentParser(
prog='Waveform Logger',
description='Waveform logger for SIGLENT SDS 1104X-E scope')
p.add_argument('-c', '--channels', nargs='+', default='C1',
help='Scope channel (C1, C2, C3, C4)')
p.add_argument('-o', '--output', default='',
help="Output directory")
p.add_argument('-n', '--num', type=int, default=1,
help='Number of traces to record')
args = p.parse_args()
if isinstance(args.channels , str):
args.channels = [args.channels]
if len(args.output):
if os.name == 'nt':
os.system('md ' + args.output)
else:
os.system('mkdir -p ' + args.output)
chans = ['C1', 'C2', 'C3', 'C4']
for chan in args.channels:
if chan not in ['C1', 'C2', 'C3', 'C4']:
raise ValueError('Channel %s not in %s' % (chan, chans))
# connect to oscilloscope, assumes only one scope connected
rm = pyvisa.ResourceManager()
inst = rm.open_resource(rm.list_resources()[0])
inst.write_termination = '\n'
inst.read_termination = '\n'
inst.timeout = 30000
# check connection
name = inst.query('*IDN?').strip()
print('\nConnected: ' + name)
for i in range(args.num):
# get timestamp
t0 = time.time()
ts = datetime.fromtimestamp(t0).strftime('%Y-%m-%dT%H.%M.%S')
ts += ("%.4f" % (datetime.fromtimestamp(t0).microsecond / 1e6))[1:]
print('\n%s Acquiring ...' % ts)
try:
inst.write('ARM')
# loop to ensure trigger is received
while 'Stop' not in inst.query('SAST?'):
pass
for chan in args.channels:
trigger = os.path.join(args.output, 'waveform_%s_%s.txt' % (chan, ts))
tdiv = inst.query('TDIV?')
sara = inst.query('SARA?')
vdiv = inst.query(chan + ':VDIV?')
ofst = inst.query(chan + ':OFST?')
# number of waveform points to sample
sanu = int(float(inst.query('SANU? ' + chan).split()[-1][:-3]))
inst.write(chan + ':WF? DAT2')
raw = []
while len(raw) < int(22 + sanu + 2):
raw.extend(list(inst.read_raw()))
waveform = list(raw)[22:22 + sanu]
f = open(trigger, 'w')
f.write(tdiv + '\n')
f.write(sara + '\n')
f.write(vdiv + '\n')
f.write(ofst + '\n')
f.write(' '.join([str(data) for data in waveform]))
f.close()
print(' - Saved ' + trigger)
# END for (chan)
print(' - Completed in %.2f sec' % (time.time() - t0))
except BaseException:
print(' - Skipping due to read timeout')
# END for (i)
inst.close()