forked from tomwphillips/pumpy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpumpy.py
922 lines (758 loc) · 34.6 KB
/
pumpy.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
from __future__ import print_function
import serial
import argparse
import logging
def remove_crud(string):
"""Return string without useless information.
Return string with trailing zeros after a decimal place, trailing
decimal points, and leading and trailing spaces removed.
"""
if "." in string:
string = string.rstrip('0')
string = string.lstrip('0 ')
string = string.rstrip(' .')
return string
class Chain(serial.Serial):
"""Create Chain object.
Harvard syringe pumps are daisy chained together in a 'pump chain'
off a single serial port. A pump address is set on each pump. You
must first create a chain to which you then add Pump objects.
Chain is a subclass of serial.Serial. Chain creates a serial.Serial
instance with the required parameters, flushes input and output
buffers (found during testing that this fixes a lot of problems) and
logs creation of the Chain.
"""
def __init__(self, port):
serial.Serial.__init__(self,port=port, stopbits=serial.STOPBITS_TWO, parity=serial.PARITY_NONE, timeout=2)
self.flushOutput()
self.flushInput()
logging.info('Chain created on %s',port)
class Pump:
"""Create Pump object for Harvard Pump 11.
Argument:
Chain: pump chain
Optional arguments:
address: pump address. Default is 0.
name: used in logging. Default is Pump 11.
"""
def __init__(self, chain, address=0, name='Pump 11'):
self.name = name
self.serialcon = chain
self.address = '{0:02.0f}'.format(address)
self.diameter = None
self.flowrate = None
self.targetvolume = None
"""Query model and version number of firmware to check pump is
OK. Responds with a load of stuff, but the last three characters
are XXY, where XX is the address and Y is pump status. :, > or <
when stopped, running forwards, or running backwards. Confirm
that the address is correct. This acts as a check to see that
the pump is connected and working."""
try:
self.write('VER')
resp = self.read(17)
if int(resp[-3:-1]) != int(self.address):
raise PumpError('No response from pump at address %s' %
self.address)
except PumpError:
self.serialcon.close()
raise
logging.info('%s: created at address %s on %s', self.name,
self.address, self.serialcon.port)
def __repr__(self):
string = ''
for attr in self.__dict__:
string += '%s: %s\n' % (attr,self.__dict__[attr])
return string
def write(self,command):
self.serialcon.write(self.address + command + '\r')
def read(self,bytes=5):
response = self.serialcon.read(bytes)
if len(response) == 0:
raise PumpError('%s: no response to command' % self.name)
else:
return response
def setdiameter(self, diameter):
"""Set syringe diameter (millimetres).
Pump 11 syringe diameter range is 0.1-35 mm. Note that the pump
ignores precision greater than 2 decimal places. If more d.p.
are specificed the diameter will be truncated.
"""
if diameter > 35 or diameter < 0.1:
raise PumpError('%s: diameter %s mm is out of range' %
(self.name, diameter))
# TODO: Got to be a better way of doing this with string formatting
diameter = str(diameter)
# Pump only considers 2 d.p. - anymore are ignored
if len(diameter) > 5:
if diameter[2] is '.': # e.g. 30.2222222
diameter = diameter[0:5]
elif diameter[1] is '.': # e.g. 3.222222
diameter = diameter[0:4]
diameter = remove_crud(diameter)
logging.warning('%s: diameter truncated to %s mm', self.name,
diameter)
else:
diameter = remove_crud(diameter)
# Send command
self.write('MMD' + diameter)
resp = self.read(5)
# Pump replies with address and status (:, < or >)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if diameter has been set correctlry
self.write('DIA')
resp = self.read(15)
returned_diameter = remove_crud(resp[3:9])
# Check diameter was set accurately
if returned_diameter != diameter:
logging.error('%s: set diameter (%s mm) does not match diameter'
' returned by pump (%s mm)', self.name, diameter,
returned_diameter)
elif returned_diameter == diameter:
self.diameter = float(returned_diameter)
logging.info('%s: diameter set to %s mm', self.name,
self.diameter)
else:
raise PumpError('%s: unknown response to setdiameter' % self.name)
def setflowrate(self, flowrate):
"""Set flow rate (microlitres per minute).
Flow rate is converted to a string. Pump 11 requires it to have
a maximum field width of 5, e.g. "XXXX." or "X.XXX". Greater
precision will be truncated.
The pump will tell you if the specified flow rate is out of
range. This depends on the syringe diameter. See Pump 11 manual.
"""
flowrate = str(flowrate)
if len(flowrate) > 5:
flowrate = flowrate[0:5]
flowrate = remove_crud(flowrate)
logging.warning('%s: flow rate truncated to %s uL/min', self.name,
flowrate)
else:
flowrate = remove_crud(flowrate)
self.write('ULM' + flowrate)
resp = self.read(5)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# Flow rate was sent, check it was set correctly
self.write('RAT')
resp = self.read(150)
returned_flowrate = remove_crud(resp[2:8])
if returned_flowrate != flowrate:
logging.error('%s: set flowrate (%s uL/min) does not match'
'flowrate returned by pump (%s uL/min)',
self.name, flowrate, returned_flowrate)
elif returned_flowrate == flowrate:
self.flowrate = returned_flowrate
logging.info('%s: flow rate set to %s uL/min', self.name,
self.flowrate)
elif 'OOR' in resp:
raise PumpError('%s: flow rate (%s uL/min) is out of range' %
(self.name, flowrate))
else:
raise PumpError('%s: unknown response' % self.name)
def infuse(self):
"""Start infusing pump."""
self.write('RUN')
resp = self.read(5)
while resp[-1] != '>':
if resp[-1] == '<': # wrong direction
self.write('REV')
else:
raise PumpError('%s: unknown response to to infuse' % self.name)
resp = self.serialcon.read(5)
logging.info('%s: infusing',self.name)
def withdraw(self):
"""Start withdrawing pump."""
self.write('REV')
resp = self.read(5)
while resp[-1] != '<':
if resp[-1] == ':': # pump not running
self.write('RUN')
elif resp[-1] == '>': # wrong direction
self.write('REV')
else:
raise PumpError('%s: unknown response to withdraw' % self.name)
break
resp = self.read(5)
logging.info('%s: withdrawing',self.name)
def stop(self):
"""Stop pump."""
self.write('STP')
resp = self.read(5)
if resp[-1] != ':':
raise PumpError('%s: unexpected response to stop' % self.name)
else:
logging.info('%s: stopped',self.name)
def settargetvolume(self, targetvolume):
"""Set the target volume to infuse or withdraw (microlitres)."""
self.write('MLT' + str(targetvolume))
resp = self.read(5)
# response should be CRLFXX:, CRLFXX>, CRLFXX< where XX is address
# Pump11 replies with leading zeros, e.g. 03, but PHD2000 misbehaves and
# returns without and gives an extra CR. Use int() to deal with
if resp[-1] == ':' or resp[-1] == '>' or resp[-1] == '<':
self.targetvolume = float(targetvolume)
logging.info('%s: target volume set to %s uL', self.name,
self.targetvolume)
else:
raise PumpError('%s: target volume not set' % self.name)
def waituntiltarget(self):
"""Wait until the pump has reached its target volume."""
logging.info('%s: waiting until target reached',self.name)
# counter - need it to check if it's the first loop
i = 0
while True:
# Read once
self.serialcon.write(self.address + 'VOL\r')
resp1 = self.read(15)
if ':' in resp1 and i == 0:
raise PumpError('%s: not infusing/withdrawing - infuse or '
'withdraw first', self.name)
elif ':' in resp1 and i != 0:
# pump has already come to a halt
logging.info('%s: target volume reached, stopped',self.name)
break
# Read again
self.serialcon.write(self.address + 'VOL\r')
resp2 = self.read(15)
# Check if they're the same - if they are, break, otherwise continue
if resp1 == resp2:
logging.info('%s: target volume reached, stopped',self.name)
break
i = i+1
class PHD2000(Pump):
"""Harvard PHD2000 pump object.
Inherits from Pump class, but needs its own class as it doesn't
stick to the Pump 11 protocol with commands to stop and set the
target volume.
"""
def stop(self):
"""Stop pump."""
self.write('STP')
resp = self.read(5)
if resp[-1] == '*':
logging.info('%s: stopped',self.name)
else:
raise PumpError('%s: unexpected response to stop', self.name)
def settargetvolume(self, targetvolume):
"""Set the target volume to infuse or withdraw (microlitres)."""
# PHD2000 expects target volume in mL not uL like the Pump11, so convert to mL
targetvolume = str(float(targetvolume)/1000.0)
if len(targetvolume) > 5:
targetvolume = targetvolume[0:5]
logging.warning('%s: target volume truncated to %s mL',self.name,targetvolume)
self.write('MLT' + targetvolume)
resp = self.read(5)
# response should be CRLFXX:, CRLFXX>, CRLFXX< where XX is address
# Pump11 replies with leading zeros, e.g. 03, but PHD2000 misbehaves and
# returns without and gives an extra CR. Use int() to deal with
if resp[-1] == ':' or resp[-1] == '>' or resp[-1] == '<':
# Been set correctly, so put it back in the object (as uL, not mL)
self.targetvolume = float(targetvolume)*1000.0
logging.info('%s: target volume set to %s uL', self.name,
self.targetvolume)
class MightyMini():
def __init__(self, chain, name='Mighty Mini'):
self.name = name
self.serialcon = chain.serialcon
logging.info('%s: created on %s',self.name,self.serialcon.port)
def __repr__(self):
string = ''
for attr in self.__dict__:
string += '%s: %s\n' % (attr,self.__dict__[attr])
return string
def setflowrate(self, flowrate):
flowrate = int(flowrate)
if flowrate > 9999:
flowrate = 9999
logging.warning('%s: flow rate limited to %s uL/min', self.name,
flowrate)
self.serialcon.write('FM' + "{:04d}".format(flowrate))
resp = self.serialcon.read(3)
self.serialcon.flushInput()
if len(resp) == 0:
raise PumpError('%s: no response to set flowrate', self.name)
elif resp[0] == 'O' and resp[1] == 'K':
# flow rate sent, check it is correct
self.serialcon.write('CC')
resp = self.serialcon.read(11)
returned_flowrate = int(float(resp[5:-1])*1000)
if returned_flowrate != flowrate:
raise PumpError('%s: set flowrate (%s uL/min) does not match'
' flowrate returned by pump (%s uL/min)',
self.name, flowrate, returned_flowrate)
elif returned_flowrate == flowrate:
self.flowrate = returned_flowrate
logging.info('%s: flow rate set to %s uL/min', self.name,
self.flowrate)
else:
raise PumpError('%s: error setting flow rate (%s uL/min)',
self.name,flowrate)
def infuse(self):
self.serialcon.write('RU')
resp = self.serialcon.read(3)
if len(resp) == 0:
raise PumpError('%s: no response to infuse',self.name)
elif resp[0] == 'O' and resp[1] == 'K':
logging.info('%s: infusing',self.name)
def stop(self):
self.serialcon.write('ST')
resp = self.serialcon.read(3)
if len(resp) == 0:
raise PumpError('%s: no response to stop',self.name)
elif resp[0] == 'O' and resp[1] == 'K':
logging.info('%s: stopping',self.name)
class Pump33:
"""Create Pump object for Harvard Pump 33.
Argument:
Chain: pump chain
Optional arguments:
address: pump address. Default is 0.
name: used in logging. Default is Pump 33.
"""
def __init__(self, chain, address=0, name='Pump 33'):
self.name = name
self.serialcon = chain
self.address = '{0:02.0f}'.format(address)
self.mode = None
self.diameter1 = None
self.diameter2 = None
self.flowrate1 = None
self.flowrate2 = None
self.direction1 = None
self.direction2 = None
"""Query model and version number of firmware to check pump is
OK. Responds with a load of stuff, but the last three characters
are XXY, where XX is the address and Y is pump status. :, > or <
when stopped, running forwards, or running backwards. Confirm
that the address is correct. This acts as a check to see that
the pump is connected and working."""
try:
self.write('VER')
resp = self.read(11)
if int(resp[-3:-1]) != int(self.address):
raise PumpError('No response from pump at address %s' %
self.address)
else:
print ('%s: %s ' % (self.name, resp[1:-4]))
except PumpError:
self.serialcon.close()
raise
logging.info('%s: created at address %s on %s' % (self.name,
self.address, self.serialcon.port))
def __repr__(self):
string = ''
for attr in self.__dict__:
string += '%s: %s\n' % (attr, self.__dict__[attr])
return string
def write(self,command):
self.serialcon.write(self.address + command + '\r')
def read(self,bytes=5):
response = self.serialcon.read(bytes)
if len(response) == 0:
raise PumpError('%s: no response to command' % self.name)
else:
return response
def setmode(self, mode):
"""Set pump mode.
Pump 33 has 3 mode: Auto Stop, Proportional, and Continuous.
The Command for them are AUT, PRO, and CON, respectively.
"""
# Check if the input is a valid mode
if mode not in ['AUT', 'PRO', 'CON']:
raise PumpError('%s: %s is not a mode name' %(self.name, mode))
self.write('MOD' + mode)
resp = self.read(5)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if mode has been set correctlry
self.write('MOD')
resp = self.read(15)
returned_mode = resp[1:-4]
# Check mode was set accurately
if returned_mode != mode:
logging.error('%s: set mode (%s) does not match mode'
' returned by pump (%s)' % (self.name, mode,
returned_mode))
elif returned_mode == mode:
self.mode = mode
print ('%s: mode set to %s' % (self.name, self.mode))
logging.info('%s: mode set to %s' % (self.name, self.mode))
else:
raise PumpError('%s: unknown response to setmode' % self.name)
def setdiameter1(self, diameter):
"""Set syringe 1 diameter (millimetres).
Pump 33 syringe diameter range is 0.1-50 mm. Note that the diameters
are in the following format: ffffff (e.g. 44.755 or 0.3257)
"""
if diameter > 50 or diameter < 0.1:
raise PumpError('%s: diameter %s mm is out of range' % (self.name,
str(diameter)))
# TODO: Got to be a better way of doing this with string formatting
diameter = str(diameter)
# Pump only considers float format: ffffff
if len(diameter) > 6:
diameter = diameter[0:6]
diameter = remove_crud(diameter)
logging.warning('%s: diameter truncated to %s mm' % (self.name,
diameter))
else:
diameter = remove_crud(diameter)
# Send command
self.write('DIA A' + diameter)
resp = self.read(5)
# Pump replies with address and status (:, < or >)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if diameter has been set correctlry
self.write('DIA A')
resp = self.read(15)
returned_diameter = resp[1:-4]
# Check diameter was set accurately
if float(returned_diameter) != float(diameter):
logging.error('%s: set diameter (%s mm) does not match diameter'
'returned by pump (%s mm)' % (self.name, diameter,
returned_diameter))
elif float(returned_diameter) == float(diameter):
self.diameter1 = float(returned_diameter)
print ('%s: syringe 1 diameter set to %s mm' % (self.name,
self.diameter1))
logging.info('%s: syringe 1 diameter set to %s mm' % (self.name,
self.diameter1))
else:
raise PumpError('%s: unknown response to setdiameter1' % self.name)
def setdiameter2(self, diameter):
"""Set syringe 2 diameter (millimetres) (only valid in Proportional
(PRO) mode).
Pump 33 syringe diameter range is 0.1-50 mm. Note that the diameters
are in the following format: ffffff (e.g. 44.755 or 0.3257)
"""
# Check if the pump is in Proportional mode
self.write('MOD')
resp = self.read(15)
returned_mode = resp[1:-4]
if returned_mode != 'PRO':
raise PumpError('%s: set syringe 2 diameter is only valid in '
'Proportional mode' % self.name)
if diameter > 50 or diameter < 0.1:
raise PumpError('%s: diameter %s mm is out of range' %
(self.name, str(diameter)))
# TODO: Got to be a better way of doing this with string formatting
diameter = str(diameter)
# Pump only considers 2 d.p. - anymore are ignored
if len(diameter) > 6:
diameter = diameter[0:6]
diameter = remove_crud(diameter)
logging.warning('%s: diameter truncated to %s mm' % (self.name,
diameter))
else:
diameter = remove_crud(diameter)
# Send command
self.write('DIA B' + diameter)
resp = self.read(5)
# Pump replies with address and status (:, < or >)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if diameter has been set correctlry
self.write('DIA B')
resp = self.read(15)
returned_diameter = resp[1:-4]
# Check diameter was set accurately
if float(returned_diameter) != float(diameter):
logging.error('%s: set diameter (%s mm) does not match diameter'
' returned by pump (%s mm)' % (self.name, diameter,
returned_diameter))
elif float(returned_diameter) == float(diameter):
self.diameter2 = float(returned_diameter)
print ('%s: syringe 2 diameter set to %s mm' % (self.name,
self.diameter2))
logging.info('%s: syringe 2 diameter set to %s mm' % (self.name,
self.diameter2))
else:
raise PumpError('%s: unknown response to setdiameter2' % self.name)
def setflowrate1(self, flowrate):
"""Set both syringe 1 and 2 to the same flow rate (microlitres per
minute) in Auto Stop and Continuous mode. In Proportional mode, it
only sets flow rate for syringe 1.
Flow rate is converted to a string. Pump 33 requires it to have
the format: ffffff.
The pump will tell you if the specified flow rate is out of
range. This depends on the syringe diameter. See Pump 33 manual.
"""
flowrate = str(flowrate)
if len(flowrate) > 6:
flowrate = flowrate[0:6]
flowrate = remove_crud(flowrate)
logging.warning('%s: flow rate truncated to %s uL/min' % (self.name,
flowrate))
else:
flowrate = remove_crud(flowrate)
self.write('RAT A' + flowrate + 'UM')
resp = self.read(5)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# Flow rate was sent, check it was set correctly
self.write('RAT A')
resp = self.read(20)
returned_flowrate = remove_crud(resp[1:7])
if float(returned_flowrate) != float(flowrate):
logging.error('%s: set flowrate (%s uL/min) does not match'
'flowrate returned by pump (%s uL/min)' %
(self.name, flowrate, returned_flowrate))
elif float(returned_flowrate) == float(flowrate):
self.flowrate1 = float(returned_flowrate)
print ('%s: syringe 1 flow rate set to %s uL/min' %
(self.name, str(self.flowrate1)))
logging.info('%s: syringe 1 flow rate set to %s uL/min' %
(self.name, str(self.flowrate1)))
elif 'OOR' in resp:
raise PumpError('%s: flow rate (%s uL/min) is out of range' %
(self.name, flowrate))
else:
raise PumpError('%s: unknown response to setflowrate1' %
self.name)
def setflowrate2(self, flowrate):
"""Set syringe 2 flow rate (microlitres per minute)(only valid
in Proportional (PRO) mode).
Flow rate is converted to a string. Pump 33 requires it to have
the format: ffffff.
The pump will tell you if the specified flow rate is out of
range. This depends on the syringe diameter. See Pump 33 manual.
"""
# Check if the pump is in Proportional mode
self.write('MOD')
resp = self.read(15)
returned_mode = resp[1:-4]
if returned_mode != 'PRO':
raise PumpError('%s: set syringe 2 flow rate is only valid '
'in Proportional mode' % self.name)
flowrate = str(flowrate)
if len(flowrate) > 6:
flowrate = flowrate[0:6]
flowrate = remove_crud(flowrate)
logging.warning('%s: flow rate truncated to %s uL/min' %
(self.name, flowrate))
else:
flowrate = remove_crud(flowrate)
self.write('RAT B' + flowrate + 'UM')
resp = self.read(5)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# Flow rate was sent, check it was set correctly
self.write('RAT B')
resp = self.read(20)
returned_flowrate = remove_crud(resp[1:7])
if float(returned_flowrate) != float(flowrate):
logging.error('%s: set flowrate (%s uL/min) does not match'
'flowrate returned by pump (%s uL/min)' %
(self.name, flowrate, returned_flowrate))
elif float(returned_flowrate) == float(flowrate):
self.flowrate2 = float(returned_flowrate)
print ('%s: syringe 2 flow rate set to %s uL/min' %
(self.name, str(self.flowrate2)))
logging.info('%s: syringe 2 flow rate set to %s uL/min' %
(self.name, str(self.flowrate2)))
elif 'OOR' in resp:
raise PumpError('%s: flow rate (%s uL/min) is out of range' %
(self.name, flowrate))
else:
raise PumpError('%s: unknown response to setflowrate2' %
self.name)
def setdirection1(self, direction):
"""Set syringe 1 direction.
Pump 33 has 3 direction setting: INF (infusion), REF (refill), REV
(reverse).
"""
# Check if the input is a valid direction command
if direction not in ['INF', 'REF', 'REV']:
raise PumpError('%s: %s is not a direction name' % (self.name,
direction))
self.write('DIR')
resp = self.read(15)
original_dir = resp[1:4]
if original_dir == direction:
pass
else:
# this would change directions of both syringes
self.write('DIR REV')
resp = self.read(5)
# change syringe 2 direction back
self.par()
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if direction has been set correctlry
self.write('DIR')
resp = self.read(15)
returned_direction = resp[1:4]
if returned_direction != direction:
logging.error('%s: set syringe 1 direction (%s) does not match'
' direction returned by pump (%s)' % (self.name,
direction, returned_direction))
elif returned_direction == direction:
self.direction1 = direction
print ('%s: syringe 1 direction set to %s' % (self.name,
self.direction1))
logging.info('%s: syringe 1 direction set to %s' % (self.name,
self.direction1))
else:
raise PumpError('%s: unknown response to setdirection1' % self.name)
def setdirection2(self, direction):
"""Set syringe 2 direction.
Pump 33 has 3 direction setting: INF (infusion), REF (refill), REV
(reverse).
"""
# Check if the input is a valid direction command
if direction not in ['INF', 'REF', 'REV']:
raise PumpError('%s: %s is not a direction name' % (self.name,
direction))
self.write('DIR')
resp = self.read(15)
original_dir1 = resp[1:4]
self.write('PAR')
resp = self.read(15)
original_par = resp[1:-4]
# set syringe 2 direction
if original_dir1 == direction and original_par == 'ON':
pass
elif original_dir1 != direction and original_par == 'OFF':
pass
else:
self.par()
self.write('PAR')
resp = self.read(15)
returned_par = resp[1:-4]
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if direction has been set correctlry
self.write('DIR')
resp = self.read(15)
returned_direction = resp[1:4]
if returned_direction != direction and returned_par == 'ON':
logging.error('%s: set syringe 2 direction (%s) does not match'
' direction returned by pump (%s)' % (self.name,
direction, returned_direction))
elif returned_direction == direction and returned_par == 'OFF':
logging.error('%s: set syringe 2 direction (%s) does not match'
' direction returned by pump (%s)' % (self.name,
direction, returned_direction))
else:
self.direction2 = direction
print ('%s: syringe 2 direction set to %s' % (self.name,
self.direction2))
logging.info('%s: syringe 2 direction set to %s' % (self.name,
self.direction2))
else:
raise PumpError('%s: unknown response to setdirection2' % self.name)
def start(self):
"""Start the pump"""
self.write('RUN')
resp = self.read(5)
if resp[-1] != '>' and resp[-1] != '<':
raise PumpError('%s: unknown response to start' % self.name)
else:
logging.info('%s: started' % self.name)
def stop(self):
"""Stop the pump"""
self.write('STP')
resp = self.read(5)
if resp[-1] != ':':
raise PumpError('%s: unexpected response to stop' % self.name)
else:
logging.info('%s: stopped' % self.name)
def par(self):
"""Switch the pump between parallel and reciprocal pumping
direction.
ON = Parallel (syringes in the same direction)
OFF = Reciprocal (syringes in the opposite direction)
"""
self.write('PAR')
resp = self.read(15)
original_par = resp[1:-4]
if original_par == 'ON':
self.write('PAR OFF')
resp = self.read(5)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if Parallel/Reciprocal has been set correctlry
self.write('PAR')
resp = self.read(15)
returned_par = resp[1:-4]
if returned_par != 'OFF':
logging.error('%s: set Parallel/Reciprocal (%s) does not work'
% self.name)
elif returned_par == 'OFF':
logging.info('%s: switch from Parallel to Reciprocal' %
self.name)
elif original_par == 'OFF':
self.write('PAR ON')
resp = self.read(5)
if (resp[-1] == ':' or resp[-1] == '<' or resp[-1] == '>'):
# check if Parallel/Reciprocal has been set correctlry
self.write('PAR')
resp = self.read(15)
returned_par = resp[1:-4]
if returned_par != 'ON':
logging.error('%s: set Parallel/Reciprocal (%s) does not work'
% self.name)
elif returned_par == 'ON':
logging.info('%s: switch from Reciprocal to Parallel' %
self.name)
else:
raise PumpError('%s: unknown response to setdirection' % self.name)
class PumpError(Exception):
pass
# Command line options
# Run with -h flag to see help
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Command line interface to '
'pumpy module for control of Harvard Pump '
'11 (default) or PHD2000 syringe pumps, or'
' SSI Mighty Mini Pump')
parser.add_argument('port', help='serial port')
parser.add_argument('address', help='pump address (Harvard pumps)',type=int,
nargs='?', default=0)
parser.add_argument('-d', dest='diameter', help='set syringe diameter',
type=int)
parser.add_argument('-f', dest='flowrate', help='set flow rate')
parser.add_argument('-t', dest='targetvolume', help='set target volume')
parser.add_argument('-w', dest='wait', help='wait for target volume to be'
' reached; use with -infuse or -withdraw',
action='store_true')
# TODO: only allow -w if infuse, withdraw or stop have been specified
group = parser.add_mutually_exclusive_group()
group.add_argument('-infuse', action='store_true')
group.add_argument('-withdraw', action="store_true")
group.add_argument('-stop', action="store_true")
pumpgroup = parser.add_mutually_exclusive_group()
pumpgroup.add_argument('-PHD2000', help='To control PHD2000',
action='store_true')
pumpgroup.add_argument('-MightyMini', help='To control Mighty Mini',
action='store_true')
args = parser.parse_args()
if args.MightyMini:
chain = Chain(args.port, stopbits=serial.STOPBITS_ONE)
else:
chain = Chain(args.port)
# Command precedence:
# 1. stop
# 2. set diameter
# 3. set flow rate
# 4. set target
# 5. infuse|withdraw (+ wait for target volume)
try:
if args.PHD2000:
pump = PHD2000(chain, args.address, name='PHD2000')
elif args.MightyMini:
pump = MightyMini(chain, name='MightyMini')
else:
pump = Pump(chain,args.address, name='11')
if args.stop:
pump.stop()
if args.diameter:
pump.setdiameter(args.diameter)
if args.flowrate:
pump.setflowrate(args.flowrate)
if args.targetvolume:
pump.settargetvolume(args.targetvolume)
if args.infuse:
pump.infuse()
if args.wait:
pump.waituntiltarget()
if args.withdraw:
pump.withdraw()
if args.wait:
pump.waituntiltarget()
finally:
chain.close()