-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsig.py
More file actions
executable file
·73 lines (57 loc) · 2.06 KB
/
sig.py
File metadata and controls
executable file
·73 lines (57 loc) · 2.06 KB
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
#!/usr/bin/env python3
import argparse
import math
import pyvisa
def pava_value(scope, ch: str, item: str) -> tuple[float, str]:
raw = scope.query(f"{ch}:PAVA? {item}").strip()
tail = raw.split(",")[-1].strip() if "," in raw else raw.strip()
num = ""
for c in tail:
if c in "+-0123456789.eE":
num += c
else:
break
if num == "":
return math.nan, raw
try:
return float(num), raw
except ValueError:
return math.nan, raw
def fmt_freq_khz(v_hz: float) -> str:
if math.isnan(v_hz):
return "nan kHz"
return f"{v_hz / 1e3:.3f} kHz"
def main():
ap = argparse.ArgumentParser(prog="sig")
ap.add_argument("cmd", choices=["freq", "rms", "pkpk", "all"])
ap.add_argument("-i", "--ip", default="192.168.178.54")
ap.add_argument("-c", "--ch", choices=["C1", "C2", "both"], default="both")
ap.add_argument("--idn", action="store_true")
ap.add_argument("--raw", action="store_true")
args = ap.parse_args()
rm = pyvisa.ResourceManager("@py")
scope = rm.open_resource(f"TCPIP0::{args.ip}::INSTR")
scope.timeout = 5000
scope.read_termination = "\n"
scope.write_termination = "\n"
if args.idn:
print(scope.query("*IDN?").strip())
channels = ["C1", "C2"] if args.ch == "both" else [args.ch]
for ch in channels:
if args.cmd in ("freq", "all"):
f, rawf = pava_value(scope, ch, "FREQ")
if args.raw:
print(f"{ch} RAW FREQ: {rawf}")
print(f"{ch} freq={fmt_freq_khz(f)}")
if args.cmd in ("rms", "all"):
r, rawr = pava_value(scope, ch, "RMS")
if args.raw:
print(f"{ch} RAW RMS: {rawr}")
print(f"{ch} rms={'nan' if math.isnan(r) else f'{r:.6g}'} V")
if args.cmd in ("pkpk", "all"):
p, rawp = pava_value(scope, ch, "PKPK")
if args.raw:
print(f"{ch} RAW PKPK: {rawp}")
print(f"{ch} pkpk={'nan' if math.isnan(p) else f'{p:.6g}'} V")
if __name__ == "__main__":
main()