summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qduplex_ser.py
blob: 170039b02d768489c01b9282d678b42a143660ae (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
import unittest
import uuid
import serial
import time


class Qduplex(unittest.TestCase):
    params = None
    __port1 = None
    __port2 = None
    __baudrate = None
    __resultlist = None  # resultlist is a python list of python dictionaries

    # varlist: port1, port2, baudrate
    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qduplex, self).__init__(testfunc)
        if "port1" in varlist:
            self.__port1 = varlist["port1"]
        else:
            raise Exception('port1 param inside Qduplex must be defined')
        if "port2" in varlist:
            self.__port2 = varlist["port2"]
        else:
            raise Exception('port2 param inside Qduplex must be defined')
        if "baudrate" in varlist:
            self.__baudrate = varlist["baudrate"]
        else:
            raise Exception('baudrate param inside Qduplex must be defined')
        self._testMethodDoc = testname
        self.__resultlist = []

        # open serial connection
        self.__serial1 = serial.Serial(self.__port1, self.__baudrate, timeout=1)
        self.__serial2 = serial.Serial(self.__port2, self.__baudrate, timeout=1)

    def __del__(self):
        self.__serial1.close()
        self.__serial2.close()

    def execute(self):
        # generate a random uuid
        test_uuid = str(uuid.uuid4()).encode()

        # clean serial ports
        self.__serial1.flushInput()
        self.__serial1.flushOutput()
        self.__serial2.flushInput()
        self.__serial2.flushOutput()
        # send the uuid through serial port
        self.__serial1.write(test_uuid)
        time.sleep(0.05)  # there might be a small delay
        if self.__serial2.inWaiting() == 0:
            self.fail("failed: port {} wait timeout exceded".format(self.__port2))
        else:
            if self.__serial2.readline() != test_uuid:
                self.fail("failed: port {} write/read mismatch".format(self.__port2))

        time.sleep(0.05)  # there might be a small delay

        # clean serial ports
        self.__serial1.flushInput()
        self.__serial1.flushOutput()
        self.__serial2.flushInput()
        self.__serial2.flushOutput()
        # send the uuid through serial port
        self.__serial2.write(test_uuid)
        time.sleep(0.05)  # there might be a small delay
        if self.__serial1.inWaiting() == 0:
            self.fail("failed: port {} wait timeout exceded".format(self.__port1))
        else:
            if self.__serial1.readline() != test_uuid:
                self.fail("failed: port {} write/read mismatch".format(self.__port1))

    def getresults(self):
        return self.__resultlist

    def gettextresult(self):
        return ""