summaryrefslogtreecommitdiff
path: root/scripts/PYTHON_SOFTWARE/test/tests/qamp.py
diff options
context:
space:
mode:
authorManel Caro <mcaro@iseebcn.com>2019-03-09 21:25:56 +0100
committerManel Caro <mcaro@iseebcn.com>2019-03-09 21:25:56 +0100
commit9332c933fc05f42882640c9a4e35fab09854af84 (patch)
tree2b9b00b5a411b27f9705603c0d93b8925afd4677 /scripts/PYTHON_SOFTWARE/test/tests/qamp.py
downloadboard-9332c933fc05f42882640c9a4e35fab09854af84.zip
board-9332c933fc05f42882640c9a4e35fab09854af84.tar.gz
board-9332c933fc05f42882640c9a4e35fab09854af84.tar.bz2
Board: Client Test Suite Initial Commit
Diffstat (limited to 'scripts/PYTHON_SOFTWARE/test/tests/qamp.py')
-rw-r--r--scripts/PYTHON_SOFTWARE/test/tests/qamp.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/scripts/PYTHON_SOFTWARE/test/tests/qamp.py b/scripts/PYTHON_SOFTWARE/test/tests/qamp.py
new file mode 100644
index 0000000..58f4daf
--- /dev/null
+++ b/scripts/PYTHON_SOFTWARE/test/tests/qamp.py
@@ -0,0 +1,86 @@
+from test.helpers.syscmd import SysCommand
+import unittest
+import serial
+import time
+
+class Qamp(unittest.TestCase):
+
+ def __init__(self, testname, testfunc, undercurrent=0.1, overcurrent=2):
+ self._current = 0.0
+ self._undercurrent=undercurrent
+ self._overcurrent = overcurrent
+ 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
+ return -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\n\r')
+ i=0
+ while (i < len(cmd)):
+ i = i + self._ser.write(cmd[i].encode('ascii'))
+ time.sleep(0.05)
+ self._ser.read(1)
+ res0 = []
+ while (self._ser.inWaiting() > 0): # if incoming bytes are waiting to be read from the serial input buffer
+ res0.append(self._ser.read(1).decode('ascii')) # read the bytes and convert from binary array to ASCII
+ print(res0)
+ #CHECK COM FIRST
+ 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', '')
+ try:
+ self._current = float(int(string, 0)) / self._vshuntfactor
+ except:
+ self.fail("failed: CAN'T READ CONSUMPTION (CURRENT=0?)")
+ error=1
+ return -1
+ if error==0:
+ 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))
+ return -1
+
+ if (self._current < float(self._undercurrent)):
+ self.fail("failed: UNDERCURRENT DETECTED ( {} )".format(self._current))
+ return -1
+