-
Notifications
You must be signed in to change notification settings - Fork 0
/
pps-ctrl
executable file
·225 lines (187 loc) · 6.78 KB
/
pps-ctrl
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
#
# pps-ctrl: control Atten PPS3205T-3S reprogrammable power supplies
#
# Send command and configuration data to reprogrammable power supply over
# RS-232. If only a TTY is provided then configuration for all channels is
# printed to STDOUT. If only a TTY and a channel are provided then
# configuration is printed for that channel.
#
# See ppsrecv for current measurement and data aquisition from the power
# supply.
#
# Requires the pyserial library, http://pyserial.sourceforge.net/
#
# Authors: Mike Turquette <[email protected]>
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
import serial
import os, sys, getopt
import math
import time
import signal
# options
debug = False
verbose = False
quiet = False
loop = False
address = 0x00
channel = -1
voltage = -1.0
current = -1.0
output = -1
# arguments
baud = 9600
def usage():
name = os.path.basename(sys.argv[0])
print
print name, "[options] <tty> [baud]"
print " -h: this help message"
print " -d: enable debug mode"
print " -V: verbose output"
print " -q: quiet"
print " -l: loop endlessly (break with SIGINT or ctrl-c)"
print " -c <channel>: 1, 2 or 3 for triple output supply"
print " -v <voltage>: voltage in millivolts"
print " -i <current>: current in milliamps"
print " -o <on/off>: turn output on or off"
# XXX someday
#print " -a: target address in hexadecimal/base 16"
#print " -l <on/off>: turn alarm on or off"
#print " -p <on/off>: turn Over Voltage Protection on or off"
#print " -n <independent/series/parallel>: select output relationships"
print
def generate_output_buf(conn, channel, voltage, current, output):
if not conn.isOpen():
print "Could not open %s" % tty
sys.exit(1)
# new message header
output_buf = chr(0xaa) + chr(address)
# iterate over the three channels
for i in range(1, 4):
# each channel (starting from 1 and going to 3) has 4 bytes set aside for
# programming voltage and current (more on that below). Fill in the
# supplied per-channel data, otherwise zero-fill
# FIXME better to read the target values and re-use them instead of zero-filling
if (channel != i):
output_buf += chr(0x00) + chr(0x00) + chr(0x00) + chr(0x00)
continue
# voltage is split into two bytes. The first byte is a multiple of 2.56V. E.g.
# to get 5.12V output the first voltage byte would be 2, and the second byte 0.
# The second voltage byte is in hundredths of a volt (centivolts). E.g. to get
# 0.03V the first byte would be 0 and the second byte would be 3
if (voltage > -1):
voltage /= 1000.0
v1 = int(voltage / 2.56)
v2 = int(math.ceil(voltage % 2.56 * 100))
output_buf += chr(v1) + chr(v2)
else:
# FIXME should read the target value and re-use that value, not zero-fill
output_buf += chr(0x00) + chr(0x00)
# current is similarly split into two bytes. The first byte is a multiple of
# 0.256A. E.g. to get 0.512A output the first current byte would be 2, and the
# second byte 0. The second current byte is in thousanths of an amp
# (milliamps). E.g. to get 0.003A the first byte would be 0 and the second byte
# would be 3
if (current > -1):
current /= 1000.0
c1 = int(current / 0.256)
c2 = int(math.ceil(current % 0.256 * 1000))
output_buf += chr(c1) + chr(c2)
else:
# FIXME should read the target value and re-use that value, not zero-fill
output_buf += chr(0x00) + chr(0x00)
# byte 14 is RESERVED which is why we program two bytes here, for those of
# you keeping count. Byte 15 is the output control byte
# FIXME in The Future we can program more than one channel at once, so we
# need to make this bitmask less dumb and OR it with the target value
if (output > -1):
output_buf += chr(0x00) + chr(output)
else:
# FIXME should read the target value and re-use that value, not zero-fill
output_buf += chr(0x00) + chr(0x00)
# FIXME zero-fill the last 8 bytes since they are non-critical
output_buf += chr(0x00) + chr(0x00) + chr(0x00) + chr(0x00) + \
chr(0x00) + chr(0x00) + chr(0x00) + chr(0x00)
return output_buf
def program_supply_and_print(conn, channel, voltage, current, output, output_buf, verbose):
conn.write(output_buf)
input_buf = conn.read(24)
current = (ord(input_buf[4]) * 256 + ord(input_buf[5]))
if verbose == True:
print "time (sec), voltage (mV), current (mA), power (mW)"
print "%.3f, %d, %d, %d" % ((time.time() - start_time), voltage, current, voltage * current / 1000)
if (verbose):
print "byte #\toutput\tinput"
for i in range(0, 24):
print "%d\t%d\t%d" % (i, ord(output_buf[i]), ord(input_buf[i]))
print
def signal_handler(signal, frame):
global conn
conn.close()
print "\nSIGINT caught"
sys.exit(0)
# main program
signal.signal(signal.SIGINT, signal_handler)
try:
opts, args = getopt.getopt(sys.argv[1:], "hdVqla:c:v:i:o:")
except getopt.GetoptError as err:
print str(err) # will print something like "option -a not recognized"
sys.exit(2)
for o, a in opts:
if o == "-h":
usage()
sys.exit(0)
elif o == "-d":
debug = True
elif o == "-V":
verbose = True
elif o == "-q":
quiet = True
elif o == "-l":
loop = True
elif o == "-a":
address = int(a, 16)
elif o == "-c":
channel = int(a)
elif o == "-v":
voltage = int(a)
elif o == "-i":
current = int(a)
elif o == "-o":
if a.lower() == 'on':
output = 1
elif a.lower() == 'off':
output = 0
else:
print "Invalid value for parameter -o. Use 'on' or 'off'"
sys.exit(2)
else:
assert False, "Unhandled option %s" %o
if len(args) < 1:
usage()
sys.exit(1)
if len(args) > 0:
tty = args[0]
else:
usage()
sys.exit(1)
if len(args) > 1:
baud = int(args[1])
conn = serial.Serial(
port=tty,
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
start_time = time.time()
output_buf = generate_output_buf(conn, channel, voltage, current, output)
program_supply_and_print(conn, channel, voltage, current, output, output_buf, verbose)
while loop == True:
program_supply_and_print(conn, channel, voltage, current, output, output_buf, verbose)
conn.close()