-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeysight_atten.py
92 lines (75 loc) · 3.27 KB
/
keysight_atten.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
#keysight_atten.py
import sys
import simplessh
from time import sleep
class keysight:
def __init__(self, addr = 28, comport = '/dev/ttyUSB0'):
self.con = simplessh.Connection( host = '192.168.1.66',username = 'pi',
private_key = None,
password = 'raspberry',
port = 22 )
self.comport = comport
self.addr = addr
self.con.execute(r'echo ++mode 1 >%s; ' % self.comport)
self.con.execute(r'echo ++ifc >%s; '% self.comport)
self.con.execute(r'echo ++read_tmo_ms 100 >%s; '% self.comport)
self.con.execute(r'echo ++auto 1 >%s; ' % self.comport)
# This can only be set once! All devices will have to output to that file.
self.out_file = 'output.txt'
self.con.execute('stty raw -echo < /dev/ttyUSB0; cat -vte </dev/ttyUSB0 > %s' % self.out_file)
# attenuator parameters
self.attenx = 1
self.atteny = 10
def send_cmd(self, cmd):
self.con.execute(r'echo ++addr %s >%s; '% (self.addr, self.comport))
self.con.execute(r'echo '+ cmd + ' >%s; ' % self.comport)
def set_atten(self, atten):
y = atten // self.atteny
x = atten % self.atteny
setx = x * self.attenx
sety = y * self.atteny
self.con.execute(r'echo ++addr %s >%s; '% (self.addr, self.comport))
self.con.execute(r'echo ATT:X '+ str(setx) + ' >%s; ' % self.comport)
self.con.execute(r'echo ATT:Y '+ str(sety) + ' >%s; ' % self.comport)
def get_attenx(self):
self.con.execute(r'echo ++addr %s >%s; '% (self.addr, self.comport))
self.con.execute(r'echo ++read >%s; ' % self.comport)
self.con.execute(r'truncate -s 0 %s; stty raw -echo < %s; tail <%s > %s | echo ATT:X? >%s; '
%(self.out_file, self.comport, self.comport,self.out_file, self.comport))
sleep(.3)
self.con.get(self.out_file)
f = open(self.out_file)
output = f.readline()
f.close()
if output == '':
return 0
else:
return eval(output.strip('\x00').strip('\n').strip(' ').strip('$'))
def get_atteny(self):
self.con.execute(r'echo ++addr %s >%s; '% (self.addr, self.comport))
self.con.execute(r'echo ++read >%s; ' % self.comport)
self.con.execute(r'truncate -s 0 %s; stty raw -echo < %s; tail <%s > %s | echo ATT:Y? >%s; '
%(self.out_file, self.comport, self.comport,self.out_file, self.comport))
sleep(.3)
self.con.get(self.out_file)
f = open(self.out_file)
output = f.readline()
f.close()
if output == '':
return 0
else:
return eval(output.strip('\x00').strip('\n').strip(' ').strip('$'))
def get_atten(self):
xx = self.get_attenx()
sleep(.3)
yy = self.get_atteny()
return xx + yy
def keysight_test():
# test of the class
from random import randint
atten = randint(0,60)
print(atten)
ke = keysight()
ke.set_atten(atten)
sleep(.5)
ke.get_atten()