blob: 71c7db632f053899c894922ab7502a7c2c4e8044 [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# trick to make that this test run under 2.6 and 3.x without modification.
33# problem is, io library on 2.6 does NOT accept type 'str' and 3.x doesn't
34# like u'nicode' strings with the prefix and it is not providing an unicode
35# function ('str' is now what 'unicode' used to be)
36if sys.version_info >= (3, 0):
Chris Liechti8e37ba92016-02-03 01:22:22 +010037 def unicode(x):
38 return x
cliechtif81362e2009-07-25 03:44:33 +000039
cliechtiddd78132009-07-28 01:13:28 +000040
41# on which port should the tests be performed:
42PORT = 0
43
Chris Liechti8e37ba92016-02-03 01:22:22 +010044
cliechtiddd78132009-07-28 01:13:28 +000045class Test_SerialAndIO(unittest.TestCase):
cliechtif81362e2009-07-25 03:44:33 +000046
47 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +000048 self.s = serial.serial_for_url(PORT, timeout=1)
cliechti38077122013-10-16 02:57:27 +000049 #~ self.io = io.TextIOWrapper(self.s)
cliechtiddd78132009-07-28 01:13:28 +000050 self.io = io.TextIOWrapper(io.BufferedRWPair(self.s, self.s))
cliechtif81362e2009-07-25 03:44:33 +000051
52 def tearDown(self):
53 self.s.close()
54
cliechtiddd78132009-07-28 01:13:28 +000055 def test_hello_raw(self):
Chris Liechti16843852015-09-22 23:24:35 +020056 self.io.write(b"hello\n".decode('utf-8'))
Chris Liechti8e37ba92016-02-03 01:22:22 +010057 self.io.flush() # it is buffering. required to get the data out
cliechtiddd78132009-07-28 01:13:28 +000058 hello = self.io.readline()
Chris Liechtidedd3b72015-12-11 20:44:22 +010059 self.assertEqual(hello, b"hello\n".decode('utf-8'))
cliechtif81362e2009-07-25 03:44:33 +000060
cliechti38077122013-10-16 02:57:27 +000061# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cliechtif81362e2009-07-25 03:44:33 +000062if __name__ == '__main__':
63 import sys
64 sys.stdout.write(__doc__)
65 if len(sys.argv) > 1:
66 PORT = sys.argv[1]
67 sys.stdout.write("Testing port: %r\n" % PORT)
68 sys.argv[1:] = ['-v']
69 # When this module is executed from the command-line, it runs all its tests
70 unittest.main()