summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qamp.py
blob: 56511c88ba9c5d7085254f401c8c124cf72b1f46 (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
from test.helpers.syscmd import SysCommand
import unittest
import serial
import time


class Qamp(unittest.TestCase):
    params = None

    # varlist: undercurrent, overcurrent
    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        self._current = 0.0
        if "undercurrent" in varlist:
            self._undercurrent = varlist["undercurrent"]
        else:
            raise Exception('undercurrent param inside Qamp must be defined')
        if "overcurrent" in varlist:
            self._overcurrent = varlist["overcurrent"]
        else:
            raise Exception('overcurrent param inside Qamp must be defined')
        self._vshuntfactor = 16384.0
        self._ser = serial.Serial()
        self._ser.port = '/dev/ttyUSB0'
        self._ser.baudrate = 115200
        self._ser.parity = serial.PARITY_NONE
        self._ser.timeout = 0
        self._ser.writeTimout = 0
        self._ser.xonxoff = False
        self._ser.rtscts = False
        self._ser.dsrdtr = False
        super(Qamp, self).__init__(testfunc)
        self._testMethodDoc = testname

    def __del__(self):
        self._ser.close()

    def execute(self):

        # Open Serial port ttyUSB0
        error = 0
        try:
            self._ser.open()
        except:
            self.fail("failed: IMPOSSIBLE OPEN USB-SERIAL PORT ( {} )".format(self._ser.port))
            error = 1
        if error == 0:
            # Clean input and output buffer of serial port
            self._ser.flushInput()
            self._ser.flushOutput()
            # Send command to read Voltage at Shunt resistor
            # Prepare cmd
            cmd = 'at+in?\n\r'
            i = 0

            # Write, 1 by 1 byte at a time to avoid hanging of serial receiver code of listener, emphasis being made at sleep of 50 ms.
            while (i < len(cmd)):
                i = i + self._ser.write(cmd[i].encode('ascii'))
                time.sleep(0.05)
                self._ser.read(1)

            # Read, 1 by 1 byte
            res = []
            while self._ser.inWaiting() > 0:  # if incoming bytes are waiting to be read from the serial input buffer
                res.append(self._ser.read(1).decode('ascii'))  # read the bytes and convert from binary array to ASCII

            string = ''.join(res)
            string = string.replace('\n', '')
            self._current = float(int(string, 0)) / self._vshuntfactor
            print(self._current)
            # In order to give a valid result it is importarnt to define an under current value
            if self._current > float(self._overcurrent):
                self.fail("failed: OVERCURRENT DETECTED ( {} )".format(self._current))

            if self._current < float(self._undercurrent):
                self.fail("failed: UNDERCURRENT DETECTED ( {} )".format(self._current))