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 unittest |
| 28 | import sys |
cliechti | cb5b0aa | 2009-07-29 21:59:52 +0000 | [diff] [blame] | 29 | |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 30 | if __name__ == '__main__' and sys.version_info < (2, 6): |
cliechti | cb5b0aa | 2009-07-29 21:59:52 +0000 | [diff] [blame] | 31 | sys.stderr.write("""\ |
| 32 | ============================================================================== |
| 33 | WARNING: this test is intended for Python 2.6 and newer where the io library |
| 34 | is available. This seems to be an older version of Python running. |
| 35 | Continuing anyway... |
| 36 | ============================================================================== |
| 37 | """) |
| 38 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 39 | import io |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 40 | import serial |
| 41 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 42 | # trick to make that this test run under 2.6 and 3.x without modification. |
| 43 | # problem is, io library on 2.6 does NOT accept type 'str' and 3.x doesn't |
| 44 | # like u'nicode' strings with the prefix and it is not providing an unicode |
| 45 | # function ('str' is now what 'unicode' used to be) |
| 46 | if sys.version_info >= (3, 0): |
| 47 | def unicode(x): return x |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 48 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 49 | |
| 50 | # on which port should the tests be performed: |
| 51 | PORT = 0 |
| 52 | |
| 53 | class Test_SerialAndIO(unittest.TestCase): |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 54 | |
| 55 | def setUp(self): |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 56 | self.s = serial.serial_for_url(PORT, timeout=1) |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 57 | #~ self.io = io.TextIOWrapper(self.s) |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 58 | self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s)) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 59 | |
| 60 | def tearDown(self): |
| 61 | self.s.close() |
| 62 | |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 63 | def test_hello_raw(self): |
Chris Liechti | 1684385 | 2015-09-22 23:24:35 +0200 | [diff] [blame] | 64 | self.io.write(b"hello\n".decode('utf-8')) |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 65 | self.io.flush() # it is buffering. required to get the data out |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 66 | hello = self.io.readline() |
Chris Liechti | 1684385 | 2015-09-22 23:24:35 +0200 | [diff] [blame] | 67 | self.failUnlessEqual(hello, b"hello\n".decode('utf-8')) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 68 | |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 69 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 70 | if __name__ == '__main__': |
| 71 | import sys |
| 72 | sys.stdout.write(__doc__) |
| 73 | if len(sys.argv) > 1: |
| 74 | PORT = sys.argv[1] |
| 75 | sys.stdout.write("Testing port: %r\n" % PORT) |
| 76 | sys.argv[1:] = ['-v'] |
| 77 | # When this module is executed from the command-line, it runs all its tests |
| 78 | unittest.main() |