Chris Liechti | 790be84 | 2016-09-08 23:36:57 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # This file is part of pySerial - Cross platform serial port support for Python |
| 4 | # (C) 2016 Chris Liechti <cliechti@gmx.net> |
| 5 | # |
| 6 | # SPDX-License-Identifier: BSD-3-Clause |
| 7 | """\ |
| 8 | Test serial.threaded related functionality. |
| 9 | """ |
| 10 | |
| 11 | import os |
| 12 | import unittest |
| 13 | import serial |
| 14 | import serial.threaded |
| 15 | import time |
| 16 | |
| 17 | |
| 18 | # on which port should the tests be performed: |
| 19 | PORT = 'loop://' |
| 20 | |
| 21 | class Test_asyncio(unittest.TestCase): |
| 22 | """Test asyncio related functionality""" |
| 23 | |
| 24 | def test_line_reader(self): |
| 25 | """simple test of line reader class""" |
| 26 | |
| 27 | class TestLines(serial.threaded.LineReader): |
| 28 | def __init__(self): |
| 29 | super(TestLines, self).__init__() |
| 30 | self.received_lines = [] |
| 31 | |
| 32 | def handle_line(self, data): |
| 33 | self.received_lines.append(data) |
| 34 | |
| 35 | ser = serial.serial_for_url(PORT, baudrate=115200, timeout=1) |
| 36 | with serial.threaded.ReaderThread(ser, TestLines) as protocol: |
| 37 | protocol.write_line('hello') |
| 38 | time.sleep(1) |
| 39 | self.assertEqual(protocol.received_lines, ['hello']) |
| 40 | |
| 41 | |
| 42 | if __name__ == '__main__': |
| 43 | import sys |
| 44 | sys.stdout.write(__doc__) |
| 45 | if len(sys.argv) > 1: |
| 46 | PORT = sys.argv[1] |
| 47 | sys.stdout.write("Testing port: {!r}\n".format(PORT)) |
| 48 | sys.argv[1:] = ['-v'] |
| 49 | # When this module is executed from the command-line, it runs all its tests |
| 50 | unittest.main() |