summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/amper.py
diff options
context:
space:
mode:
Diffstat (limited to 'test-cli/test/helpers/amper.py')
-rw-r--r--test-cli/test/helpers/amper.py154
1 files changed, 154 insertions, 0 deletions
diff --git a/test-cli/test/helpers/amper.py b/test-cli/test/helpers/amper.py
new file mode 100644
index 0000000..ea719f6
--- /dev/null
+++ b/test-cli/test/helpers/amper.py
@@ -0,0 +1,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)
+