blob: 527cc479dafc1831e6c11fc524f1ae10ad225748 [file] [log] [blame]
cliechtid6bf52c2003-10-01 02:28:12 +00001#!/usr/bin/env python
cliechtid6bf52c2003-10-01 02:28:12 +00002#
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>
5#
6# SPDX-License-Identifier: BSD-3-Clause
cliechti7fa1a9c2009-07-21 21:11:58 +00007"""\
8Some tests for the serial module.
cliechtid6bf52c2003-10-01 02:28:12 +00009Part of pyserial (http://pyserial.sf.net) (C)2002 cliechti@gmx.net
10
11Intended to be run on different platforms, to ensure portability of
12the code.
13
14These tests open a serial port and change all the settings on the fly.
cliechti7fa1a9c2009-07-21 21:11:58 +000015If the port is really correctly configured cannot be determined - that
cliechtif16c7702009-08-02 00:00:55 +000016would require external hardware or a null modem cable and an other
cliechtid6bf52c2003-10-01 02:28:12 +000017serial port library... Thus it mainly tests that all features are
18correctly implemented and that the interface does what it should.
cliechti7fa1a9c2009-07-21 21:11:58 +000019
cliechtid6bf52c2003-10-01 02:28:12 +000020"""
21
22import unittest
23import serial
24
cliechti7fa1a9c2009-07-21 21:11:58 +000025# on which port should the tests be performed:
Chris Liechti88802642016-10-07 01:17:27 +020026PORT = 'loop://'
cliechtid6bf52c2003-10-01 02:28:12 +000027
Chris Liechti8e37ba92016-02-03 01:22:22 +010028
cliechtid6bf52c2003-10-01 02:28:12 +000029class Test_ChangeAttributes(unittest.TestCase):
30 """Test with timeouts"""
cliechti7fa1a9c2009-07-21 21:11:58 +000031
cliechtid6bf52c2003-10-01 02:28:12 +000032 def setUp(self):
cliechtif16c7702009-08-02 00:00:55 +000033 # create a closed serial port
cliechtie3ab3532009-08-05 12:40:38 +000034 self.s = serial.serial_for_url(PORT, do_not_open=True)
cliechti7fa1a9c2009-07-21 21:11:58 +000035
cliechtid6bf52c2003-10-01 02:28:12 +000036 def tearDown(self):
37 self.s.close()
38
39 def test_PortSetting(self):
40 self.s.port = PORT
Chris Liechti88802642016-10-07 01:17:27 +020041 self.assertEqual(self.s.portstr.lower(), PORT.lower())
cliechti7fa1a9c2009-07-21 21:11:58 +000042 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +010043 self.assertEqual(self.s._port, PORT)
cliechti7fa1a9c2009-07-21 21:11:58 +000044 # test on the fly change
cliechtid6bf52c2003-10-01 02:28:12 +000045 self.s.open()
Chris Liechtidedd3b72015-12-11 20:44:22 +010046 self.assertTrue(self.s.isOpen())
cliechtid6bf52c2003-10-01 02:28:12 +000047
cliechti8f69e702011-03-19 00:22:32 +000048 def test_DoubleOpen(self):
cliechti8f69e702011-03-19 00:22:32 +000049 self.s.open()
50 # calling open for a second time is an error
Chris Liechtidedd3b72015-12-11 20:44:22 +010051 self.assertRaises(serial.SerialException, self.s.open)
cliechti8f69e702011-03-19 00:22:32 +000052
cliechtid6bf52c2003-10-01 02:28:12 +000053 def test_BaudrateSetting(self):
cliechti107db8d2004-01-15 01:20:23 +000054 self.s.open()
cliechtid6bf52c2003-10-01 02:28:12 +000055 for baudrate in (300, 9600, 19200, 115200):
56 self.s.baudrate = baudrate
cliechti7fa1a9c2009-07-21 21:11:58 +000057 # test get method
Chris Liechtidedd3b72015-12-11 20:44:22 +010058 self.assertEqual(self.s.baudrate, baudrate)
cliechti7fa1a9c2009-07-21 21:11:58 +000059 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +010060 self.assertEqual(self.s._baudrate, baudrate)
cliechti7fa1a9c2009-07-21 21:11:58 +000061 # test illegal values
cliechti62611612004-04-20 01:55:43 +000062 for illegal_value in (-300, -1, 'a', None):
Chris Liechtidedd3b72015-12-11 20:44:22 +010063 self.assertRaises(ValueError, setattr, self.s, 'baudrate', illegal_value)
cliechti107db8d2004-01-15 01:20:23 +000064
cliechti7fa1a9c2009-07-21 21:11:58 +000065 # skip this test as pyserial now tries to set even non standard baud rates.
66 # therefore the test can not choose a value that fails on any system.
67 def disabled_test_BaudrateSetting2(self):
68 # test illegal values, depending on machine/port some of these may be valid...
cliechti107db8d2004-01-15 01:20:23 +000069 self.s.open()
cliechtif16c7702009-08-02 00:00:55 +000070 for illegal_value in (500000, 576000, 921600, 92160):
Chris Liechtidedd3b72015-12-11 20:44:22 +010071 self.assertRaises(ValueError, setattr, self.s, 'baudrate', illegal_value)
cliechtid6bf52c2003-10-01 02:28:12 +000072
73 def test_BytesizeSetting(self):
Chris Liechti8e37ba92016-02-03 01:22:22 +010074 for bytesize in (5, 6, 7, 8):
cliechtid6bf52c2003-10-01 02:28:12 +000075 self.s.bytesize = bytesize
cliechti7fa1a9c2009-07-21 21:11:58 +000076 # test get method
Chris Liechtidedd3b72015-12-11 20:44:22 +010077 self.assertEqual(self.s.bytesize, bytesize)
cliechti7fa1a9c2009-07-21 21:11:58 +000078 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +010079 self.assertEqual(self.s._bytesize, bytesize)
cliechti7fa1a9c2009-07-21 21:11:58 +000080 # test illegal values
cliechtid6bf52c2003-10-01 02:28:12 +000081 for illegal_value in (0, 1, 3, 4, 9, 10, 'a', None):
Chris Liechtidedd3b72015-12-11 20:44:22 +010082 self.assertRaises(ValueError, setattr, self.s, 'bytesize', illegal_value)
cliechtid6bf52c2003-10-01 02:28:12 +000083
84 def test_ParitySetting(self):
85 for parity in (serial.PARITY_NONE, serial.PARITY_EVEN, serial.PARITY_ODD):
86 self.s.parity = parity
cliechti7fa1a9c2009-07-21 21:11:58 +000087 # test get method
Chris Liechtidedd3b72015-12-11 20:44:22 +010088 self.assertEqual(self.s.parity, parity)
cliechti7fa1a9c2009-07-21 21:11:58 +000089 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +010090 self.assertEqual(self.s._parity, parity)
cliechti7fa1a9c2009-07-21 21:11:58 +000091 # test illegal values
cliechtid6bf52c2003-10-01 02:28:12 +000092 for illegal_value in (0, 57, 'a', None):
Chris Liechtidedd3b72015-12-11 20:44:22 +010093 self.assertRaises(ValueError, setattr, self.s, 'parity', illegal_value)
cliechtid6bf52c2003-10-01 02:28:12 +000094
95 def test_StopbitsSetting(self):
96 for stopbits in (1, 2):
97 self.s.stopbits = stopbits
cliechti7fa1a9c2009-07-21 21:11:58 +000098 # test get method
Chris Liechtidedd3b72015-12-11 20:44:22 +010099 self.assertEqual(self.s.stopbits, stopbits)
cliechti7fa1a9c2009-07-21 21:11:58 +0000100 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +0100101 self.assertEqual(self.s._stopbits, stopbits)
cliechti7fa1a9c2009-07-21 21:11:58 +0000102 # test illegal values
cliechti5fe48d42009-07-24 12:18:49 +0000103 for illegal_value in (0, 3, 2.5, 57, 'a', None):
Chris Liechtidedd3b72015-12-11 20:44:22 +0100104 self.assertRaises(ValueError, setattr, self.s, 'stopbits', illegal_value)
cliechtid6bf52c2003-10-01 02:28:12 +0000105
106 def test_TimeoutSetting(self):
107 for timeout in (None, 0, 1, 3.14159, 10, 1000, 3600):
108 self.s.timeout = timeout
cliechti7fa1a9c2009-07-21 21:11:58 +0000109 # test get method
Chris Liechtidedd3b72015-12-11 20:44:22 +0100110 self.assertEqual(self.s.timeout, timeout)
cliechti7fa1a9c2009-07-21 21:11:58 +0000111 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +0100112 self.assertEqual(self.s._timeout, timeout)
cliechti7fa1a9c2009-07-21 21:11:58 +0000113 # test illegal values
cliechtid6bf52c2003-10-01 02:28:12 +0000114 for illegal_value in (-1, 'a'):
Chris Liechtidedd3b72015-12-11 20:44:22 +0100115 self.assertRaises(ValueError, setattr, self.s, 'timeout', illegal_value)
cliechtid6bf52c2003-10-01 02:28:12 +0000116
117 def test_XonXoffSetting(self):
118 for xonxoff in (True, False):
119 self.s.xonxoff = xonxoff
cliechti7fa1a9c2009-07-21 21:11:58 +0000120 # test get method
Chris Liechtidedd3b72015-12-11 20:44:22 +0100121 self.assertEqual(self.s.xonxoff, xonxoff)
cliechti7fa1a9c2009-07-21 21:11:58 +0000122 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +0100123 self.assertEqual(self.s._xonxoff, xonxoff)
cliechti7fa1a9c2009-07-21 21:11:58 +0000124 # no illegal values here, normal rules for the boolean value of an
125 # object are used thus all objects have a truth value.
cliechtid6bf52c2003-10-01 02:28:12 +0000126
127 def test_RtsCtsSetting(self):
128 for rtscts in (True, False):
129 self.s.rtscts = rtscts
cliechti7fa1a9c2009-07-21 21:11:58 +0000130 # test get method
Chris Liechtidedd3b72015-12-11 20:44:22 +0100131 self.assertEqual(self.s.rtscts, rtscts)
cliechti7fa1a9c2009-07-21 21:11:58 +0000132 # test internals
Chris Liechtidedd3b72015-12-11 20:44:22 +0100133 self.assertEqual(self.s._rtscts, rtscts)
cliechti7fa1a9c2009-07-21 21:11:58 +0000134 # no illegal values here, normal rules for the boolean value of an
135 # object are used thus all objects have a truth value.
cliechtid6bf52c2003-10-01 02:28:12 +0000136
cliechtie3ab3532009-08-05 12:40:38 +0000137 # this test does not work anymore since serial_for_url that is used
cliechtic110bf22009-08-02 23:46:29 +0000138 # now, already sets a port
139 def disabled_test_UnconfiguredPort(self):
cliechti7fa1a9c2009-07-21 21:11:58 +0000140 # an unconfigured port cannot be opened
Chris Liechtidedd3b72015-12-11 20:44:22 +0100141 self.assertRaises(serial.SerialException, self.s.open)
cliechti7fa1a9c2009-07-21 21:11:58 +0000142
cliechtid6bf52c2003-10-01 02:28:12 +0000143 def test_PortOpenClose(self):
cliechtid6bf52c2003-10-01 02:28:12 +0000144 for i in range(3):
cliechti7fa1a9c2009-07-21 21:11:58 +0000145 # open the port and check flag
Chris Liechtidedd3b72015-12-11 20:44:22 +0100146 self.assertTrue(not self.s.isOpen())
cliechtid6bf52c2003-10-01 02:28:12 +0000147 self.s.open()
Chris Liechtidedd3b72015-12-11 20:44:22 +0100148 self.assertTrue(self.s.isOpen())
cliechtid6bf52c2003-10-01 02:28:12 +0000149 self.s.close()
Chris Liechtidedd3b72015-12-11 20:44:22 +0100150 self.assertTrue(not self.s.isOpen())
cliechtid6bf52c2003-10-01 02:28:12 +0000151
cliechtif16c7702009-08-02 00:00:55 +0000152
cliechtid6bf52c2003-10-01 02:28:12 +0000153if __name__ == '__main__':
154 import sys
cliechti8f9e5772009-02-07 00:30:53 +0000155 sys.stdout.write(__doc__)
cliechti764c88c2008-06-24 11:56:48 +0000156 if len(sys.argv) > 1:
157 PORT = sys.argv[1]
Chris Liechti3debab22016-06-20 22:52:22 +0200158 sys.stdout.write("Testing port: {!r}\n".format(PORT))
cliechti764c88c2008-06-24 11:56:48 +0000159 sys.argv[1:] = ['-v']
cliechtid6bf52c2003-10-01 02:28:12 +0000160 # When this module is executed from the command-line, it runs all its tests
161 unittest.main()