-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhv_source.py
More file actions
executable file
·159 lines (125 loc) · 5.17 KB
/
hv_source.py
File metadata and controls
executable file
·159 lines (125 loc) · 5.17 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
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
#!/usr/bin/env python
import time
import serial
"""
This is the HV object the controls communication between
the computer and the Glassman HV supply.
"""
class HV(object):
default_current_limit = 0.005 # max 5 mA current draw
def __init__(self, port='/dev/tty.usbserial-A4007BoA'):
self.port = port
self.start_serial(self.port)
#Open a port for serial communication.
def start_serial(self, port):
# Configure the serial connections
# These are specific to the Glassman HV supply
self.ser = serial.Serial(
port=port,
baudrate=9600,
parity=serial.PARITY_NONE,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE)
time.sleep(.25)
def write(self, command):
self.ser.write(command)
time.sleep(.25) # This delay allows response.
def close_serial(self):
self.ser.close()
def read(self):
out = []
while self.ser.inWaiting() > 0:
data = str(self.ser.read())
out.append(data)
if out != '':
return ''.join(out)
else:
return None
def write_and_read(self, command):
self.write(command)
return self.read()
def hv_on(self, volts, currentLimit = default_current_limit):
self.set_voltage_and_current(volts, currentLimit)
def hv_off(self):
self.set_voltage_and_current(0, 0)
# This turns on the high voltage and sets a current limit
def set_voltage_and_current(self, volts, currentLimit):
command = self.build_HV_command(volts, currentLimit, 'on')
response = self.write_and_read(command)
if response != 'A \n' and response != 'A\r':
print("Error from HV supply: "+repr(response))
################# Command Building Methods ##################
def build_HV_command(self, volts, currentLimit, power):
header = '\001'
identifier = 'S'
voltage = self.find_voltage_setting(volts)
current = self.find_current_setting(currentLimit)
dumbZeros = '000000'
if power.upper() == 'ON' or power == 2: power = '2'
if power.upper() == 'OFF' or power == 1: power = '1'
if power.upper() == 'RESET' or power == 4: power = '4'
command = identifier+voltage+current+dumbZeros+power
checksum = self.find_checksum(command)
return header+command+checksum+'\r'
# Returns two character for the checksum of a given command
def find_checksum(self, command):
i = 0
for c in command:
i += ord(c)
remainder = i % 256
checksum = hex(remainder)[2:].upper()
for i in range(2-len(checksum)): checksum = '0' + checksum
return checksum
# Finds voltage settings given an absolute voltage.
# Returns three characters for the voltage command.
def find_voltage_setting(self, absVoltage):
if absVoltage > 5000:
print("Max voltage setting 5kV. Setting to 5kV.")
absVoltage = 5000
scaled = int(absVoltage/5000.0*4095)
command = hex(scaled)[2:].upper()
for i in range(3-len(command)): command= '0' + command
return command
# Finds current limit settings given an absolute current.
# Returns three characters for the current command.
def find_current_setting(self, absCurrent):
if absCurrent > 20E-3:
print("Max current setting 20mA. Setting to 20mA.")
absCurrent = 20E-3
scaled = int(absCurrent/20E-3*4095)
command = hex(scaled)[2:].upper()
for i in range(3-len(command)): command= '0' + command
return command
################# Response Reading Methods ##################
# Ask the Glassman to tell us its status
def get_status(self):
# Build the query command
header = '\001'
identifier = 'Q'
checksum = '51'
command = header+identifier+checksum+'\r'
# Send query and return response
return self.write_and_read(command)
def get_voltage(self):
status = self.get_status()
# Analog monitors have 10-bit resolution, max = 3FF = 1024
scaled_voltage = int(status[1:4], 16)
return scaled_voltage/1023.0*5E3
def get_current(self):
status = self.get_status()
# Analog monitors have 10-bit resolution, max = 3FF = 1024
scaled_current = int(status[4:7], 16)
return scaled_current/1023.0*20E-3
def get_voltage_and_current(self):
status = self.get_status()
# Analog monitors have 10-bit resolution, max = 3FF = 1024
scaled_voltage = int(status[1:4], 16)
scaled_current = int(status[4:7], 16)
return scaled_voltage/1023.0*5E3, scaled_current/1023.0*20E-3
def check_ready(self):
status = self.get_status()
# Very basic test, just want to hear something
if status == '':
return False
else:
return True