-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap_clock.py
executable file
·95 lines (65 loc) · 2.27 KB
/
snap_clock.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
#!/usr/bin/python
"""
snap_clock.py
This program will query the SNAP frequency generator for the
output frequency and amplitude.
"""
import telnetlib
from optparse import OptionParser
import sys
_host = "10.10.1.197"
_port = 5025
defaultF = 1800
defaultA = 16
def main():
parser = OptionParser(usage= 'Usage %prog options',
description='display or change the values of SNAP clock generator')
parser.add_option('-p', '--print', dest='do_print', action="store_true", default=False,
help ="Query and print current values")
parser.add_option('-f','--freq', dest='freq', type=float, default=None,
help ='set clock value in MHz')
parser.add_option('-a','--amp', dest='amp', type=float, default=None,
help ='set amplitude')
(options,args) = parser.parse_args()
if len(sys.argv) <= 1:
print("no options provided")
parser.print_help()
sys.exit(1)
do_p = options.do_print;
freq = options.freq
amp = options.amp
if (do_p and (freq or amp)):
print('if -p option is provided, -a and -f option should not be given')
sys.exit(1)
if (do_p):
settings = get_clock_settings()
print("{}:{} {} MHz amp {}".format(_host,_port,settings['freq']/1e6,settings['ampr']))
else:
if (freq):
set_freq(freq)
if (amp):
set_amp(amp)
print("done")
def set_freq(freq):
telnet = telnetlib.Telnet()
telnet.open(_host, _port)
telnet.write("FREQ {} MHz\n".format(freq))
def set_amp(amp):
telnet = telnetlib.Telnet()
telnet.open(_host, _port)
telnet.write("AMPR {} MHz\n".format(amp))
def get_clock_settings():
telnet = telnetlib.Telnet()
telnet.open(_host, _port)
telnet.write("FREQ?\n")
freq = telnet.read_until("\n", 5)
telnet.write("AMPR?\n")
ampr = telnet.read_until("\n", 5)
answer = { "type" : "clock", "freq" : float(freq), "ampr" : float(ampr) }
return answer
def set_clock_freq(freq_mhz):
print ('To set the clock freq, "telnet %s %d" then "FREQ %f MHz"' % (_host, _port, float(freq_mhz)))
def set_ampr(ampr_db):
print ('To set rhe amplitude of the RF output, "telnet %s %d" then "AMPR %f"' % (_host, _port, float(ampr_db)))
if __name__== "__main__":
main()