blob: 84e3fa2444a854d9d020aa8d1fd26f3875b9a395 [file] [log] [blame]
cliechtif16c7702009-08-02 00:00:55 +00001#! /usr/bin/env python
2#
Chris Liechti7c032f12015-10-27 23:02:00 +01003# This file is part of pySerial - Cross platform serial port support for Python
4# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
cliechtif81362e2009-07-25 03:44:33 +00005#
Chris Liechti7c032f12015-10-27 23:02:00 +01006# SPDX-License-Identifier: BSD-3-Clause
cliechtif81362e2009-07-25 03:44:33 +00007"""\
8Some tests for the serial module.
9Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net
10
11Intended to be run on different platforms, to ensure portability of
12the code.
13
cliechtiddd78132009-07-28 01:13:28 +000014This modules contains test for the interaction between Serial and the io
15library. This only works on Python 2.6+ that introduced the io library.
cliechtif81362e2009-07-25 03:44:33 +000016
17For all these tests a simple hardware is required.
18Loopback HW adapter:
19Shortcut these pin pairs:
20 TX <-> RX
21 RTS <-> CTS
22 DTR <-> DSR
23
24On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
25"""
26
cliechtiddd78132009-07-28 01:13:28 +000027import io
Chris Liechti5524bc02016-05-26 17:17:54 +020028import sys
29import unittest
cliechtif81362e2009-07-25 03:44:33 +000030import serial
31
cliechtiddd78132009-07-28 01:13:28 +000032# on which port should the tests be performed:
Chris Liechti73ff15c2016-10-08 21:06:58 +020033PORT = 'loop://'
cliechtiddd78132009-07-28 01:13:28 +000034
Chris Liechti8e37ba92016-02-03 01:22:22 +010035
cliechtiddd78132009-07-28 01:13:28 +000036class Test_SerialAndIO(unittest.TestCase):
cliechtif81362e2009-07-25 03:44:33 +000037
38 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +000039 self.s = serial.serial_for_url(PORT, timeout=1)
cliechti38077122013-10-16 02:57:27 +000040 #~ self.io = io.TextIOWrapper(self.s)
cliechtiddd78132009-07-28 01:13:28 +000041 self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s))
cliechtif81362e2009-07-25 03:44:33 +000042
43 def tearDown(self):
44 self.s.close()
45
cliechtiddd78132009-07-28 01:13:28 +000046 def test_hello_raw(self):
Chris Liechti16843852015-09-22 23:24:35 +020047 self.io.write(b"hello\n".decode('utf-8'))
Chris Liechti8e37ba92016-02-03 01:22:22 +010048 self.io.flush() # it is buffering. required to get the data out
cliechtiddd78132009-07-28 01:13:28 +000049 hello = self.io.readline()
Chris Liechtidedd3b72015-12-11 20:44:22 +010050 self.assertEqual(hello, b"hello\n".decode('utf-8'))
cliechtif81362e2009-07-25 03:44:33 +000051
cliechti38077122013-10-16 02:57:27 +000052# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cliechtif81362e2009-07-25 03:44:33 +000053if __name__ == '__main__':
54 import sys
55 sys.stdout.write(__doc__)
56 if len(sys.argv) > 1:
57 PORT = sys.argv[1]
Chris Liechti3debab22016-06-20 22:52:22 +020058 sys.stdout.write("Testing port: {!r}\n".format(PORT))
cliechtif81362e2009-07-25 03:44:33 +000059 sys.argv[1:] = ['-v']
60 # When this module is executed from the command-line, it runs all its tests
61 unittest.main()