summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/amper.py
blob: ea719f62e38b78b50ad629632acc127d98d26ed6 (plain)
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
import serial
import scanf


class Amper(object):
    __ser = None
    __port = '/dev/ttyUSB0'
    __speed = 115200
    __parity = None
    __rtscts = 0
    __timeout = 1
    __isThere = False
    __version = None
    __voltage = 0.0
    __current = 0.0
    __alarm_condition = 0

    def __init__(self, port='/dev/ttyUSB0', serial_speed=115200, parity=serial.PARITY_NONE, rtscts=0):
        self.__port = port
        self.__speed = serial_speed
        self.__parity = parity
        self.__rtscts = rtscts

    def open(self):
        try:
            if self.__ser is not None:
                self.close()
            self.__ser = serial.Serial(port=self.__port,
                                   baudrate=self.__speed,
                                   timeout=self.__timeout,
                                   parity=self.__parity,
                                   rtscts=self.__rtscts,
                                   bytesize=serial.EIGHTBITS,
                                   stopbits=serial.STOPBITS_ONE,
                                   xonxoff=0)

            self.__ser.flushInput()
            self.__ser.flushOutput()
            self.__ser.break_condition = False
            return True
        except serial.SerialException as err:
            print(err)
            return False

    def close(self):
        if self.__ser is not None:
            self.__ser.close()
            self.__ser = None
            self.__isThere = False
            self.__version = None
            self.__voltage = 0.0
            self.__current = 0.0
            self.__alarm_condition = 0

    def __write(self, data):
        data += "\n\r"
        dat = data.encode('ascii')
        self.__ser.write(dat)

    def __read(self):
        return self.__ser.readline().decode().rstrip()

    def __set_voltage_underlimit(self, under_limit):
        if self.__isThere and self.__ser is not None:
            self.__write('AT+VIN_UV_W_LIM={}'.format(under_limit))
            dat = self.__read()
            print(dat)

    def __get_voltage_underlimit(self):
        self.__write('AT+VIN_UV_W_LIM=?')
        dat = self.__read()
        res = scanf.scanf("%f", dat)
        return res[0]

    def __set_voltage_overlimit(self, over_limit):
        if self.__isThere and self.__ser is not None:
            self.__write('AT+VIN_OV_W_LIM={}'.format(over_limit))
            dat = self.__read()
            print(dat)

    def __get_voltage_overlimit(self):
        if self.__isThere and self.__ser is not None:
            self.__write('AT+VIN_OV_W_LIM?')
            dat = self.__read()
            res = scanf.scanf("%f", dat)
            return res[0]

    def __set_current_overlimit(self, over_limit):
        if self.__isThere and self.__ser is not None:
            self.__write('AT+IOUT_W_LIM={}'.format(over_limit))
            dat = self.__read()
            print(dat)

    def __get_current_overlimit(self):
        if self.__isThere and self.__ser is not None:
            self.__write('AT+IOUT_W_LIM?')
            dat = self.__read()
            dat = self.__read()
            res = scanf.scanf("%f", dat)
            return res[0]

    def hello(self):
        self.__isThere = False
        if self.__ser is None:
            return False
        self.__write('at+ver?')
        dat = self.__read()
        res = scanf.scanf("ISEE Amper v%3c", dat)
        if res is None:
            return False
        self.__isThere = True
        self.__version = res[0]
        return True

    def getVersion(self):
        return self.__version

    def getVoltage(self):
        if self.__isThere and self.__ser is not None:
            self.__write('at+vin?')
            dat = self.__read()
            res = scanf.scanf("%f", dat)
            self.__voltage = res[0]
            # print(self.__voltage)
            return self.__voltage
        else:
            return None

    def getCurrent(self):
        if self.__isThere and self.__ser is not None:
            self.__write('at+in?')
            dat = self.__read()
            res = scanf.scanf("%f", dat)
            self.__current = res[0]
            # print(self.__current)
        else:
            return None
        return self.__current

    def getAlarm(self):
        if self.__isThere and self.__ser is not None:
            self.__write('at+st_mfr?')
            dat = self.__read()
            # print(dat)
            self.__alarm_condition = (int(dat) & 0x0F)
            # print(self.__alarm_condition)
            return self.__alarm_condition

    def clearAlarm(self):
        if self.__isThere and self.__ser is not None:
            self.__write('at+CLRFAULT')
            dat = self.__read()
            # print(dat)