pumpAgilent.py 3.73 KB
Newer Older
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
# -*- coding: utf-8 -*-
#
#  Copyright 2017 uberdaff
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#
# Requirement: pyserial
#
# getIdn() - Get instrument identification
# setVolt(tal) - Set the voltage on the output
# getVolt() - Get the 'set' voltage
# readVolt() - Get a measurement of the voltage
# setAmp(tal) - Set the current limit
# getAmp() - Get the 'set' current limit
# readAmp() - Get a measurement of the output current
# setOut(bool) - Set the state of the output
# setOcp(bool) - Set the state of the over current protection
# getStatus() - Get the state of the output and CC/CV
#

import sys
import time
import serial


class pumpAgilent:
    isConnected = False
    pump_com = None
    status = None
Philipp Oleynik's avatar
Philipp Oleynik committed
44

45
46
47
    def __init__(self, dev_com):
        try:
            dev_com = serial.Serial(
Philipp Oleynik's avatar
Philipp Oleynik committed
48
49
50
51
52
53
                port=dev_com,
                baudrate=9600,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS,
                timeout=0.25
54
55
56
57
58
59
60
61
62
63
            )
            dev_com.isOpen()
            self.pump_com = dev_com
            self.isConnected = True
            # self.status = self.getStatus()
        except:
            print("COM port failure:")
            print(sys.exc_info())
            self.pump_com = None
            self.isConnected = False
Philipp Oleynik's avatar
Philipp Oleynik committed
64

65
66
    def close(self):
        self.pump_com.close()
Philipp Oleynik's avatar
Philipp Oleynik committed
67
68

    def serWriteAndRecieve(self, data, delay=0.05):
69
70
71
        self.pump_com.write(data)
        out = ''
        time.sleep(delay)
Philipp Oleynik's avatar
Philipp Oleynik committed
72
        # serial.SerialTimeoutException
73
        while self.pump_com.inWaiting() > 0:
Philipp Oleynik's avatar
Philipp Oleynik committed
74
            out += self.pump_com.read(1).decode(encoding='ascii', errors='ignore')
75
76
77
        if out != '':
            return out
        return None
Philipp Oleynik's avatar
Philipp Oleynik committed
78

79
    def getPressure(self):
Philipp Oleynik's avatar
Philipp Oleynik committed
80
81
        stat = self.serWriteAndRecieve(b"\002\200\062\062\064\060\003\070\067")
        if stat is not None:
82
            # print(":".join("{:02x}".format(ord(c)) for c in stat))
Philipp Oleynik's avatar
Philipp Oleynik committed
83
84
85
86
87
88
89
            hdr = stat.find("2240")
            stat = stat[hdr + 3: hdr + 11]
            try:
                pressure = float(stat)
            except ValueError:
                pressure = 1.20E+03
            return pressure
Philipp Oleynik's avatar
Philipp Oleynik committed
90
91
        return 1.20E+03

92
    def getRpm(self):
Philipp Oleynik's avatar
Philipp Oleynik committed
93
94
95
96
97
98
        stat = self.serWriteAndRecieve(b"\002\200\062\062\066\060\003\070\065")
        if stat is not None:
            stat = stat[4: 11]
            return int(stat)
        return 0

99
    def getStatus(self):
Philipp Oleynik's avatar
Philipp Oleynik committed
100
101
102
103
104
105
106
107
108
        stat = self.serWriteAndRecieve(b"\002\200\062\062\066\060\003\070\065")
        if stat is not None:
            stat = stat[4: 11]
            if int(stat) > 10000:
                self.status = 2
            else:
                self.status = 1
            return self.status
        return 0
Philipp Oleynik's avatar
Philipp Oleynik committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122

    def pumpStart(self):
        stat = self.serWriteAndRecieve(b"\x02\x80\x30\x30\x30\x31\x31\x03\x42\x33")
        if stat[2] == 6:
            return 1
        else:
            return 0

    def pumpStop(self):
        stat = self.serWriteAndRecieve(b"\x02\x80\x30\x30\x30\x31\x30\x03\x42\x32")
        if stat[2] == 6:
            return 1
        else:
            return 0