-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_supply.py
executable file
·50 lines (45 loc) · 1.45 KB
/
power_supply.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
#!/usr/bin/python
# power supply control for generic automation
import serial, getopt, sys
class tenma():
''' Implements good parts of remote control protocol for an affordable
Tenma power supply - tested with Tenma 72-2535 over USB serial
remote control protocol spec: https://goo.gl/YRNlPu '''
def __init__(self):
self.s = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=1)
def off(self):
self.s.write('OUT0')
def on(self):
self.s.write('OUT1')
def setvoltage(self, voltage):
cmd = "VSET1:%s" % (voltage)
for x in cmd:
self.s.write(x)
if __name__ == '__main__':
power_supply = tenma()
usage = 'usage: %s -u [0-30] -p [ON|OFF]' % sys.argv[0]
try:
opts, args = getopt.getopt(sys.argv[1:],"hu:p:", ["help", "voltage=", "power="])
except getopt.GetoptError:
print usage
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print usage
sys.exit()
elif opt == '-u':
try:
voltage = float(arg)
except:
print usage
if 0.0 <= voltage <= 30.0:
power_supply.setvoltage(str(voltage))
else:
print usage
elif opt == '-p':
if arg == 'ON':
power_supply.on()
elif arg == 'OFF':
power_supply.off()
else:
print usage