-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_era_vp.py
320 lines (268 loc) · 10 KB
/
new_era_vp.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import serial
import re
import pdb
# ToDo | 1. Test new functions
# ToDo | 2. See if the floats that come in are correct (the pump will probably
# ToDo | complain if not)
def find_pumps(ser, tot_range=10):
pumps = []
for i in range(tot_range):
cmd = '%iADR\x0D' % i
ser.write(cmd.encode())
output = ser.readline().decode()
if len(output) > 0:
pumps.append(i)
return pumps
def error_handler(fcn, msg): # See page 36/pp 41 in manual
# print('error_handler with fcn={}, msg={}'.format(fcn, msg))
if not msg:
return 'Command in {} not recognized'.format(fcn)
elif 'NA' in msg:
return 'Command not applicable. Command in {}'.format(fcn)
elif 'OOR' in msg:
return 'Command data out of range. Command in {}'.format(fcn)
elif 'COM' in msg:
return 'Invalid packet received. Command in {}'.format(fcn)
elif 'IGN' in msg:
return 'Command ignored. Command in {}'.format(fcn)
else:
return 'Command in {} not understood'.format(fcn)
# TRY THIS ONE OUT
def reset_all(ser):
cmd = '*RESET\x0D'
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from reset_all not understood')
print(error_handler(fcn='reset_all', msg=output))
def run_all(ser):
cmd = '*RUN\x0D'
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from run_all not understood')
print(error_handler(fcn='run_all', msg=output))
def stop_all(ser):
cmd = '*STP\x0D'
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from stop_all not understood')
print(error_handler(fcn='stop_all', msg=output))
def stop_pump(ser, pump):
cmd = '%iSTP\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from stop_pump not understood')
print(error_handler(fcn='stop_pump', msg=output))
cmd = '%iRAT0UH\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from stop_pump not understood')
print(error_handler(fcn='stop_pump', msg=output))
def set_rates(ser, rate):
cmd = ''
for pump in rate:
flowrate = float(rate[pump])
if flowrate < 0:
direction = 'WDR'
else:
direction = 'INF'
frcmd = '%iDIR%s\x0D' % (pump, direction)
ser.write(frcmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(frcmd.strip()+' from set_rate not understood')
print(error_handler(fcn='set_rates', msg=output))
fr = abs(flowrate)
if fr < 5000:
cmd += str(pump)+'RAT'+str(fr)[:5]+'UH*'
else:
fr = fr/1000.0
cmd += str(pump)+'RAT'+str(fr)[:5]+'MH*'
cmd += '\x0D'
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from set_rates not understood')
print(error_handler(fcn='set_rates', msg=output))
def get_direction(ser, pump):
# get direction
cmd = '%iDIR\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
return 'INF' if 'INF' in output else 'WDR'
def is_inf(ser, pump):
return True if 'INF' == get_direction(ser, pump) else False
def get_rate(ser, pump):
sign = ''
if not is_inf(ser, pump):
sign = '-'
cmd = '%iRAT\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from get_rate not understood')
print(error_handler(fcn='get_rate', msg=output))
pf = '(([0-9]|\.)*)(MH|UH|MM|UM)'
m = re.search(pf, output)
# User enters ul -> converted to ul/hr. Any rate might be in pump already.
# Convert current rate to ul/hr
# 1 ml = 1000 ul
# 1 min = (1/60) hr
if m:
if 'MH' in output: # ml/hr = 1000 ul/hr
rate = str(float(m.group(1))*1000)
elif 'UH' in output: # ul/hr
rate = m.group(1)
elif 'MM' in output: # ml/min = (1000*60) ul/hr
rate = str(float(m.group(1))*1000*60.0)
elif 'UM' in output: # ul/min = 60 ul/hr
rate = str(float(m.group(1))*60.0)
else: # Fail; return empty string -> evaluates to False
sign = ''
rate = ''
return sign + rate
def get_rates(ser, pumps):
rates = dict((p, get_rate(ser, p).split('.')[0]) for p in pumps)
return rates
def set_diameter(ser, pump, dia):
cmd = '%iDIA%s\x0D' % (pump, dia)
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from set_diameter not understood')
print(error_handler(fcn='set_diameter', msg=output))
def get_diameter(ser, pump):
cmd = '%iDIA\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from get_diameter not understood')
print(error_handler(fcn='get_diameter', msg=output))
dia = output[4:-1]
return dia
def prime(ser, pump):
# set infuse direction
cmd = '%iDIRINF\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from prime not understood')
print(error_handler(fcn='prime', msg=output))
# set rate
cmd = '%iRAT10.0MH\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from prime not understood')
print(error_handler(fcn='prime', msg=output))
# run
cmd = '%iRUN\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
# print(cmd.strip()+' from prime not understood')
print(error_handler(fcn='prime', msg=output))
def set_volume(ser, pump, vol):
# Using VOL to set volume to be dispensed
cmd = '%iVOL%s\x0D' % (pump, vol)
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
print(error_handler(fcn='set_volume', msg=output))
# print(cmd.strip()+' from set_volume not understood')
def get_volume(ser, pump):
# Using VOL to get volume to be dispensed
cmd = '%iVOL\x0D' % pump
ser.write(cmd.encode())
p = '(S)(([0-9]|\.)*)(UL|ML)'
output = ser.readline().decode()
if '?' in output:
print(error_handler(fcn='get_volume', msg=output))
# print(cmd.strip() + ' from get_volume not understood')
m = re.search(p, output)
if m:
return m.group(2)
else:
return ''
# TRY THIS ONE OUT
def set_volume_units(ser, pump, units):
# Using VOL to get volume to be dispensed
cmd = '%iVOL%s\x0D' % (pump, units)
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
print(error_handler(fcn='set_volume_units', msg=output))
# print(cmd.strip()+' from set_volume_units not understood')
def get_volume_units(ser, pump):
# Using VOL to get volume to be dispensed
cmd = '%iVOL\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
m = re.search('(UL|ML)', output)
if '?' in output:
print(error_handler(fcn='get_volume_units', msg=output))
# print(cmd.strip()+' from get_volume_units not understood')
if m:
return m.group(0)
else:
return ''
def get_dis(ser, pump):
cmd = '%iDIS\x0D' % pump # DIS gets the volumes that have been dispensed
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
print(error_handler(fcn='get_dispensed', msg=output))
# print(cmd.strip() + ' from get_dispensed not understood')
return output
def get_dispensed(ser, pump, dis=None):
output = get_dis(ser, pump)
# Output format 'I<float>W<float><volume units>'
p = '(I)(([0-9]|\.)*)(W)(([0-9]|\.)*)([A-Z][A-Z])' # Match pattern
m = re.search(p, output)
if '?' in output or not m:
return '' # Evaluates to None
elif m:
if dis == 'INF': # Querying infused volume
return m.group(2)
elif dis == 'DIS': # Querying withdrawn volume
return m.group(5)
elif dis is None: # Querying both
return m.group(2, 5)
else:
return '' # Safeguard
# Haven't figured out how to work with it right now
def clear_dispensed(ser, pump):
cmd = '%iCLD\x0D' % pump
ser.write(cmd.encode())
output = ser.readline().decode()
if '?' in output:
print(error_handler(fcn='clear_dispensed', msg=output))
# print(cmd.strip() + ' from clear_dispensed not understood')
# UNDER CONSTRUCTION
# From the manual:
# "The Fill function reverses the pumping direction and withdraws or
# dispenses [or infuses] the volume dispensed [or infused] or withdrawn."
# Try this --> set vol,
# set: dispensed volume = 0
# then: run (assuming proper rate is set)
# and that will hopefully pump at given rate until volume has been dispensed
# and then we should be able to use this to just run 'FIL' again
# def fill_syringe(ser, pump):
# cmd = '%iFUNFIL\x0D' % pump
# ser.write(cmd.encode())
# output = ser.readline().decode()
# if '?' in output:
# print(error_handler(fcn='fill_syringe', msg=output))
# print(cmd.strip() + ' from fill_syringe not understood')
# ser = serial.Serial('COM3',19200, timeout=.1)
# ser = serial.Serial('/dev/ttyUSB0', 19200, timeout=.1)
# print(ser.name) # check which port was really used
# print(ser.isOpen())
# ser.close()
# pdb.set_trace()
# pumps = find_pumps(ser)
# rates = get_rates(ser, pumps)