cliechti | f16c770 | 2009-08-02 00:00:55 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # |
Chris Liechti | 7c032f1 | 2015-10-27 23:02:00 +0100 | [diff] [blame] | 3 | # This file is part of pySerial - Cross platform serial port support for Python |
| 4 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 5 | # |
Chris Liechti | 7c032f1 | 2015-10-27 23:02:00 +0100 | [diff] [blame] | 6 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 7 | """\ |
| 8 | Some tests for the serial module. |
| 9 | Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net |
| 10 | |
| 11 | Intended to be run on different platforms, to ensure portability of |
| 12 | the code. |
| 13 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 14 | This modules contains test for the interaction between Serial and the io |
| 15 | library. This only works on Python 2.6+ that introduced the io library. |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 16 | |
| 17 | For all these tests a simple hardware is required. |
| 18 | Loopback HW adapter: |
| 19 | Shortcut these pin pairs: |
| 20 | TX <-> RX |
| 21 | RTS <-> CTS |
| 22 | DTR <-> DSR |
| 23 | |
| 24 | On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8) |
| 25 | """ |
| 26 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 27 | import io |
Chris Liechti | 5524bc0 | 2016-05-26 17:17:54 +0200 | [diff] [blame] | 28 | import sys |
| 29 | import unittest |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 30 | import serial |
| 31 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 32 | # on which port should the tests be performed: |
Chris Liechti | 73ff15c | 2016-10-08 21:06:58 +0200 | [diff] [blame] | 33 | PORT = 'loop://' |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 34 | |
Chris Liechti | 8e37ba9 | 2016-02-03 01:22:22 +0100 | [diff] [blame] | 35 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 36 | class Test_SerialAndIO(unittest.TestCase): |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 37 | |
| 38 | def setUp(self): |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 39 | self.s = serial.serial_for_url(PORT, timeout=1) |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 40 | #~ self.io = io.TextIOWrapper(self.s) |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 41 | self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s)) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 42 | |
| 43 | def tearDown(self): |
| 44 | self.s.close() |
| 45 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 46 | def test_hello_raw(self): |
Chris Liechti | 1684385 | 2015-09-22 23:24:35 +0200 | [diff] [blame] | 47 | self.io.write(b"hello\n".decode('utf-8')) |
Chris Liechti | 8e37ba9 | 2016-02-03 01:22:22 +0100 | [diff] [blame] | 48 | self.io.flush() # it is buffering. required to get the data out |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 49 | hello = self.io.readline() |
Chris Liechti | dedd3b7 | 2015-12-11 20:44:22 +0100 | [diff] [blame] | 50 | self.assertEqual(hello, b"hello\n".decode('utf-8')) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 51 | |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 52 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 53 | if __name__ == '__main__': |
| 54 | import sys |
| 55 | sys.stdout.write(__doc__) |
| 56 | if len(sys.argv) > 1: |
| 57 | PORT = sys.argv[1] |
Chris Liechti | 3debab2 | 2016-06-20 22:52:22 +0200 | [diff] [blame] | 58 | sys.stdout.write("Testing port: {!r}\n".format(PORT)) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 59 | sys.argv[1:] = ['-v'] |
| 60 | # When this module is executed from the command-line, it runs all its tests |
| 61 | unittest.main() |