from test.helpers.syscmd import SysCommand import unittest import uuid import serial import time class Qserial(unittest.TestCase): def __init__(self, testname, testfunc, varlist): super(Qserial, self).__init__(testfunc) if "port" in varlist: self.__port = varlist["port"] else: raise Exception('port param inside Qserial must be defined') self.__serial = serial.Serial(self.__port, timeout=1) if "baudrate" in varlist: self.__baudrate = varlist["baudrate"] else: raise Exception('baudrate param inside Qserial must be defined') self._testMethodDoc = testname def __del__(self): self.__serial.close() def execute(self): self.__serial.flushInput() self.__serial.flushOutput() #generate a random uuid test_uuid = str(uuid.uuid4()).encode() #send the uuid through serial port self.__serial.write(test_uuid) time.sleep(0.05) # there might be a small delay if self.__serial.inWaiting() == 0: self.fail("failed: PORT {} wait timeout exceded, wrong communication?".format(self.__port)) else: # check if what it was sent is equal to what has been received if (self.__serial.readline() != test_uuid): self.fail("failed: PORT {} write/read mismatch".format(self.__port))