-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSuperlinkInterface.py
More file actions
282 lines (208 loc) · 9 KB
/
SuperlinkInterface.py
File metadata and controls
282 lines (208 loc) · 9 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#This program provides a serial interface for a Superconductor Technologies Superlink cryocooler, returning the cold finger temperature,
#rejection temperature, and power drawn. It also allows you to switch the cryocooler between auto and manual shutdown modes
#I haven't used Python in ages, I'm sure there's goofs somewhere, if you find some, tell me!
#a lot of the GUI code is inspired by http://code.activestate.com/recipes/124894-stopwatch-in-tkinter/
from tkinter import *
import time
import serial
def truncate(f, n): #thanks internet stranger! http://stackoverflow.com/questions/783897/truncating-floats-in-python
'''Truncates/pads a float f to n decimal places without rounding'''
s = '{}'.format(f)
if 'e' in s or 'E' in s:
return '{0:.{1}f}'.format(f, n)
i, p, d = s.partition('.')
return '.'.join([i, (d+'0'*n)[:n]])
def SerialQuery(query):
query_bytes = query.encode('ascii') #convert ascii input to bytes for serial
ser.write(query_bytes)
try:
response = ser.readline()
except serial.serialutil.SerialException:
#if no response (ie if serial cable pulled out), return nothing. This doesn't acutally seem to work, need to fix.
response = b''
return response.decode('ascii').strip()
def ConvertTemp(raw_T, startpoint, endpoint, offset, gain):
#need to convert the responses from bytes to ascii to integer
#characters 22 to 25 are the cold side wide range temperature reading raw value. Need to convert it to Kelvin.
try:
temp = int(raw_T[startpoint:endpoint], 16)
temp = (5.0*temp/32768 - offset)/gain
return temp
except ValueError:
print("Error converting temperature!")
return(0.00)
def ConvertPower(raw_P, startpoint, endpoint, scaling):
#Convert power response from bytes to ascii to integer
try:
pwr = int(raw_P[startpoint:endpoint], 16)
pwr = ((5.0*pwr/32768)**2)/scaling
return pwr
except ValueError:
print("Error converting power!")
return(0.00)
#returns the position of the start of the nth space (spacenum) after the SearchFor string.
def FindSpaceAfter(SearchMsg, SearchFor, SpaceNum):
StartPt = SearchMsg.find(SearchFor) + len(SearchFor)-1
for x in range(1, SpaceNum+1):
StartPt = SearchMsg.find(" ", StartPt+1)
return StartPt
#Offset and gain values for temperature variables
Tcold_offset = 5.814
Tcold_gain = -0.01559
Trej_offset = 12.537
Trej_gain = -0.0344
Power_Scaling = 0.0075
#The various states of the superlink
SuperlinkModeOptions = ["Auto", "Manual"]
SuperlinkStateOptions = ["Initial", "Cooldown", "Regulate", "Bypass", "Shutdown"]
#Initialize but don't start serial connection. Superlink uses 19200 baud connection. Timeout is arbitrary. Set the com port at run time.
ser = serial.Serial(port=None, baudrate=19200, timeout=3)
class App(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
#Use a grid
self.columnconfigure(0, pad=20)
self.columnconfigure(1, pad=20)
self.columnconfigure(2, pad=20)
self.columnconfigure(3, pad=20)
self.columnconfigure(4, pad=20)
self.rowconfigure(0, pad=3)
self.rowconfigure(1, pad=3)
self.rowconfigure(2, pad=3)
self.rowconfigure(3, pad=3)
self.rowconfigure(4, pad=3)
self.rowconfigure(5, pad=3)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.TcoldWindow = StringVar()
self.TrejWindow = StringVar()
self.PowerWindow = StringVar()
self.ComPortText = StringVar()
self.SuperlinkModeWindow = StringVar()
self.SuperlinkStateWindow = StringVar()
self.makeWidgets()
def makeWidgets(self):
# Make all visible labels and buttons
ButtonsRow = 5
Button(self, text='Start serial comm', command=self.Start).grid(row=ButtonsRow, column = 0)
Button(self, text='Stop serial comm', command=self.Stop).grid(row=ButtonsRow, column = 1)
Button(self, text='Toggle mode', command=self.PowerToggle).grid(row = ButtonsRow, column = 2)
Button(self, text='Quit', command=self.quit).grid(row=ButtonsRow, column = 3)
ComEntry = Entry(self, textvariable=self.ComPortText)
ComEntry.grid(row = 4, column = 1)
Label(self, text = "COM port").grid(row=4, column = 0)
Label(self, text = "Time elapsed").grid(row=0, column = 2)
l = Label(self, textvariable=self.timestr)
l.grid(row=0, column=3)
Label(self, text="Tcold (K)").grid(row=0, column=0)
Tcold = Label(self, textvariable=self.TcoldWindow)
Tcold.grid(row=0, column=1)
Label(self, text="Trej (K)").grid(row=1, column=0)
Trej = Label(self, textvariable=self.TrejWindow)
Trej.grid(row=1, column=1)
Label(self, text="Power (W)").grid(row=2, column=0)
Power = Label(self, textvariable=self.PowerWindow)
Power.grid(row=2, column=1)
Label(self, text="Status").grid(row=3, column=0)
SuperlinkMode = Label(self, textvariable=self.SuperlinkModeWindow)
SuperlinkMode.grid(row=3, column=1)
SuperlinkState = Label(self, textvariable=self.SuperlinkStateWindow)
SuperlinkState.grid(row=3, column = 2)
self._setTime(self._elapsedtime)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
if ser.isOpen():
#First, query for the temperatures
query = '<TP OP="GT" LC="MS"/>' #inquiry command for cold side and rejection temps.
#Cold side temp is second bit (between spaces 1 and 2)
#Rejection temp is third bit (between spaces 2 and 3)
queryRet = SerialQuery(query)
if len(queryRet)==0:
ColdTemp = "Serial timed out"
RejTemp = "Serial timed out"
else:
ColdTemp = ConvertTemp(queryRet, FindSpaceAfter(queryRet, query, 1) + 1, FindSpaceAfter(queryRet, query, 2), Tcold_offset, Tcold_gain)
ColdTemp = truncate(ColdTemp,1)
RejTemp = ConvertTemp(queryRet, FindSpaceAfter(queryRet, query, 2) + 1, FindSpaceAfter(queryRet, query, 3), Trej_offset, Trej_gain)
RejTemp = truncate(RejTemp,1)
#Next, query for the power
query = '<PW OP="GT" LC="MS"/>' #inquiry command for cooler power
queryRet = SerialQuery(query)
if len(queryRet)==0:
Power = "Serial timed out"
else:
#Power is the first (zeroth) item in this query. The FindSpaceAfter should still work fine.
Power = ConvertPower(queryRet, FindSpaceAfter(queryRet, query, 0) + 1, FindSpaceAfter(queryRet, query, 1), Power_Scaling)
Power = truncate(Power, 1)
#Last, query for the Superlink status and auto/manual mode
query = '<TP OP="GT" LC="SM"/>'
queryRet = SerialQuery(query)
if len(queryRet)==0:
SuperlinkMode= "Serial timed out"
SuperlinkState= "Serial timed out"
else:
SuperlinkMode= int(queryRet[FindSpaceAfter(queryRet, query, 0)+1:FindSpaceAfter(queryRet, query, 0)+2])
SuperlinkMode = SuperlinkModeOptions[SuperlinkMode]
SuperlinkState= int(queryRet[FindSpaceAfter(queryRet, query, 1)+1:FindSpaceAfter(queryRet, query, 1)+2])
SuperlinkState= SuperlinkStateOptions[SuperlinkState]
#write everything to proper labels
self.TcoldWindow.set(ColdTemp)
self.TrejWindow.set(RejTemp)
self.PowerWindow.set(Power)
self.SuperlinkModeWindow.set(SuperlinkMode)
self.SuperlinkStateWindow.set(SuperlinkState)
else:
#If serial is off, do nothing
self.TcoldWindow.set("Serial connection off")
self._timer = self.after(1000, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
self.timestr.set('%02d:%02d' % (minutes, seconds))
def Start(self):
""" Start the stopwatch, ignore if running. """
global ser
if not self._running:
if self.ComPortText.get() == "":
self.TcoldWindow.set("Need to enter a COM port")
else:
try:
#Start serial connection on the proper port.
ser = serial.Serial(port=self.ComPortText.get(), baudrate=19200, timeout=3)
except serial.serialutil.SerialException:
self.TcoldWindow.set("Serial error")
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
global ser
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
#close serial port
ser.close()
def PowerToggle(self):
#toggles the Cryocooler between auto and manual modes
global ser
if self._running and ser.isOpen():
if self.SuperlinkModeWindow.get() == "Auto":
query = '<TP OP="ST" LC="SM">1 4</TP>'
else:
query = '<TP OP="ST" LC="SM">0 4</TP>'
SerialQuery(query)
def main():
root = Tk()
sw = App(root)
root.title("Stupendous Superlink Serial Snooper")
sw.pack(side=TOP)
root.mainloop()
if __name__ == '__main__':
main()