blob: 83f380e30aa7d4b711be52f7c30f492c2e326130 [file] [log] [blame]
cliechticb65c682008-06-22 22:47:02 +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
Chris Liechtid73344d2015-08-06 17:52:51 +02004# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
Chris Liechti7c032f12015-10-27 23:02:00 +01005#
6# SPDX-License-Identifier: BSD-3-Clause
cliechticb65c682008-06-22 22:47:02 +00007"""\
8Some tests for the serial module.
Chris Liechtid73344d2015-08-06 17:52:51 +02009Part of pyserial (http://pyserial.sf.net) (C)2001-2015 cliechti@gmx.net
cliechtid6bf52c2003-10-01 02:28:12 +000010
11Intended to be run on different platforms, to ensure portability of
cliechti1e95baa2002-06-04 22:46:50 +000012the code.
13
cliechtid6bf52c2003-10-01 02:28:12 +000014For all these tests a simple hardware is required.
cliechti1e95baa2002-06-04 22:46:50 +000015Loopback HW adapter:
cliechtid6bf52c2003-10-01 02:28:12 +000016Shortcut these pin pairs:
cliechtia135a672002-06-11 15:10:46 +000017 TX <-> RX
18 RTS <-> CTS
19 DTR <-> DSR
cliechti1e95baa2002-06-04 22:46:50 +000020
cliechtid6bf52c2003-10-01 02:28:12 +000021On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
cliechti1e95baa2002-06-04 22:46:50 +000022"""
23
cliechtif16c7702009-08-02 00:00:55 +000024import unittest
25import threading
26import time
cliechtia75900c2009-07-28 00:12:52 +000027import sys
cliechti1e95baa2002-06-04 22:46:50 +000028import serial
cliechtic813b212002-06-04 21:12:10 +000029
cliechti58a3efe2009-07-21 20:56:52 +000030# on which port should the tests be performed:
Chris Liechti6ed12e02015-09-18 21:23:42 +020031PORT = 'loop://'
cliechtic813b212002-06-04 21:12:10 +000032
Chris Liechti6ed12e02015-09-18 21:23:42 +020033# indirection via bytearray b/c bytes(range(256)) does something else in Pyhton 2.7
34bytes_0to255 = bytes(bytearray(range(256)))
cliechti54c534e2009-08-06 23:24:26 +000035
36
37def segments(data, size=16):
38 for a in range(0, len(data), size):
39 yield data[a:a+size]
cliechtia75900c2009-07-28 00:12:52 +000040
cliechtic813b212002-06-04 21:12:10 +000041
cliechtia135a672002-06-11 15:10:46 +000042class Test4_Nonblocking(unittest.TestCase):
cliechtic813b212002-06-04 21:12:10 +000043 """Test with timeouts"""
cliechtia75900c2009-07-28 00:12:52 +000044 timeout = 0
45
cliechtic813b212002-06-04 21:12:10 +000046 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +000047 self.s = serial.serial_for_url(PORT, timeout=self.timeout)
cliechti58a3efe2009-07-21 20:56:52 +000048
cliechtic813b212002-06-04 21:12:10 +000049 def tearDown(self):
50 self.s.close()
51
cliechtia135a672002-06-11 15:10:46 +000052 def test0_Messy(self):
53 """NonBlocking (timeout=0)"""
cliechti58a3efe2009-07-21 20:56:52 +000054 # this is only here to write out the message in verbose mode
55 # because Test3 and Test4 print the same messages
cliechtia135a672002-06-11 15:10:46 +000056
cliechtic813b212002-06-04 21:12:10 +000057 def test1_ReadEmpty(self):
cliechtia135a672002-06-11 15:10:46 +000058 """timeout: After port open, the input buffer must be empty"""
Chris Liechtidedd3b72015-12-11 20:44:22 +010059 self.assertEqual(self.s.read(1), b'', "expected empty buffer")
cliechti58a3efe2009-07-21 20:56:52 +000060
cliechtic813b212002-06-04 21:12:10 +000061 def test2_Loopback(self):
cliechtia135a672002-06-11 15:10:46 +000062 """timeout: each sent character should return (binary test).
cliechti1e95baa2002-06-04 22:46:50 +000063 this is also a test for the binary capability of a port."""
cliechti54c534e2009-08-06 23:24:26 +000064 for block in segments(bytes_0to255):
65 length = len(block)
66 self.s.write(block)
cliechtia75900c2009-07-28 00:12:52 +000067 # there might be a small delay until the character is ready (especially on win32)
cliechtif16c7702009-08-02 00:00:55 +000068 time.sleep(0.05)
Chris Liechtidedd3b72015-12-11 20:44:22 +010069 self.assertEqual(self.s.in_waiting, length, "expected exactly %d character for inWainting()" % length)
70 self.assertEqual(self.s.read(length), block)#, "expected a %r which was written before" % block)
71 self.assertEqual(self.s.read(1), b'', "expected empty buffer after all sent chars are read")
cliechti58a3efe2009-07-21 20:56:52 +000072
cliechtic813b212002-06-04 21:12:10 +000073 def test2_LoopbackTimeout(self):
cliechtia135a672002-06-11 15:10:46 +000074 """timeout: test the timeout/immediate return.
75 partial results should be returned."""
Chris Liechti6ed12e02015-09-18 21:23:42 +020076 self.s.write(b"HELLO")
cliechtif16c7702009-08-02 00:00:55 +000077 time.sleep(0.1) # there might be a small delay until the character is ready (especially on win32 and rfc2217)
cliechti58a3efe2009-07-21 20:56:52 +000078 # read more characters as are available to run in the timeout
Chris Liechtidedd3b72015-12-11 20:44:22 +010079 self.assertEqual(self.s.read(10), b'HELLO', "expected the 'HELLO' which was written before")
80 self.assertEqual(self.s.read(1), b'', "expected empty buffer after all sent chars are read")
cliechtic813b212002-06-04 21:12:10 +000081
82
cliechtia135a672002-06-11 15:10:46 +000083class Test3_Timeout(Test4_Nonblocking):
cliechtic813b212002-06-04 21:12:10 +000084 """Same tests as the NonBlocking ones but this time with timeout"""
cliechtia75900c2009-07-28 00:12:52 +000085 timeout = 1
cliechti58a3efe2009-07-21 20:56:52 +000086
cliechtia135a672002-06-11 15:10:46 +000087 def test0_Messy(self):
88 """Blocking (timeout=1)"""
cliechti58a3efe2009-07-21 20:56:52 +000089 # this is only here to write out the message in verbose mode
90 # because Test3 and Test4 print the same messages
cliechtic813b212002-06-04 21:12:10 +000091
cliechtif16c7702009-08-02 00:00:55 +000092
cliechti1e95baa2002-06-04 22:46:50 +000093class SendEvent(threading.Thread):
cliechtif16c7702009-08-02 00:00:55 +000094 def __init__(self, serial, delay=3):
cliechti1e95baa2002-06-04 22:46:50 +000095 threading.Thread.__init__(self)
96 self.serial = serial
97 self.delay = delay
98 self.x = threading.Event()
99 self.stopped = 0
100 self.start()
cliechti58a3efe2009-07-21 20:56:52 +0000101
cliechti1e95baa2002-06-04 22:46:50 +0000102 def run(self):
103 time.sleep(self.delay)
cliechti8b599c92011-08-15 13:12:09 +0000104 self.x.set()
cliechti1e95baa2002-06-04 22:46:50 +0000105 if not self.stopped:
Chris Liechti6ed12e02015-09-18 21:23:42 +0200106 self.serial.write(b"E")
cliechti7fc7da02009-08-03 23:49:02 +0000107 self.serial.flush()
cliechti58a3efe2009-07-21 20:56:52 +0000108
cliechti1e95baa2002-06-04 22:46:50 +0000109 def isSet(self):
110 return self.x.isSet()
cliechti58a3efe2009-07-21 20:56:52 +0000111
cliechti1e95baa2002-06-04 22:46:50 +0000112 def stop(self):
113 self.stopped = 1
114 self.x.wait()
115
116class Test1_Forever(unittest.TestCase):
117 """Tests a port with no timeout. These tests require that a
118 character is sent after some time to stop the test, this is done
119 through the SendEvent class and the Loopback HW."""
120 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +0000121 self.s = serial.serial_for_url(PORT, timeout=None)
cliechti1e95baa2002-06-04 22:46:50 +0000122 self.event = SendEvent(self.s)
cliechti58a3efe2009-07-21 20:56:52 +0000123
cliechti1e95baa2002-06-04 22:46:50 +0000124 def tearDown(self):
125 self.event.stop()
126 self.s.close()
127
128 def test2_ReadEmpty(self):
cliechtia135a672002-06-11 15:10:46 +0000129 """no timeout: after port open, the input buffer must be empty (read).
130 a character is sent after some time to terminate the test (SendEvent)."""
cliechti1e95baa2002-06-04 22:46:50 +0000131 c = self.s.read(1)
Chris Liechti6ed12e02015-09-18 21:23:42 +0200132 if not (self.event.isSet() and c == b'E'):
cliechti8b599c92011-08-15 13:12:09 +0000133 self.fail("expected marker (evt=%r, c=%r)" % (self.event.isSet(), c))
cliechti1e95baa2002-06-04 22:46:50 +0000134
cliechtif16c7702009-08-02 00:00:55 +0000135
cliechti1e95baa2002-06-04 22:46:50 +0000136class Test2_Forever(unittest.TestCase):
137 """Tests a port with no timeout"""
138 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +0000139 self.s = serial.serial_for_url(PORT, timeout=None)
cliechti58a3efe2009-07-21 20:56:52 +0000140
cliechti1e95baa2002-06-04 22:46:50 +0000141 def tearDown(self):
142 self.s.close()
143
144 def test1_inWaitingEmpty(self):
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200145 """no timeout: after port open, the input buffer must be empty (in_waiting)"""
Chris Liechtidedd3b72015-12-11 20:44:22 +0100146 self.assertEqual(self.s.in_waiting, 0, "expected empty buffer")
cliechti1e95baa2002-06-04 22:46:50 +0000147
148 def test2_Loopback(self):
cliechtia135a672002-06-11 15:10:46 +0000149 """no timeout: each sent character should return (binary test).
cliechti1e95baa2002-06-04 22:46:50 +0000150 this is also a test for the binary capability of a port."""
cliechti54c534e2009-08-06 23:24:26 +0000151 for block in segments(bytes_0to255):
152 length = len(block)
153 self.s.write(block)
cliechtif16c7702009-08-02 00:00:55 +0000154 # there might be a small delay until the character is ready (especially on win32 and rfc2217)
155 time.sleep(0.05)
Chris Liechtidedd3b72015-12-11 20:44:22 +0100156 self.assertEqual(self.s.in_waiting, length)#, "expected exactly %d character for inWainting()" % length)
157 self.assertEqual(self.s.read(length), block) #, "expected %r which was written before" % block)
158 self.assertEqual(self.s.in_waiting, 0, "expected empty buffer after all sent chars are read")
cliechtic813b212002-06-04 21:12:10 +0000159
160
cliechtia135a672002-06-11 15:10:46 +0000161class Test0_DataWires(unittest.TestCase):
cliechtic813b212002-06-04 21:12:10 +0000162 """Test modem control lines"""
163 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +0000164 self.s = serial.serial_for_url(PORT)
cliechti58a3efe2009-07-21 20:56:52 +0000165
cliechtic813b212002-06-04 21:12:10 +0000166 def tearDown(self):
167 self.s.close()
168
169 def test1_RTS(self):
cliechtia135a672002-06-11 15:10:46 +0000170 """Test RTS/CTS"""
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200171 self.s.rts = False
cliechti7fc7da02009-08-03 23:49:02 +0000172 time.sleep(1.1)
Chris Liechtidedd3b72015-12-11 20:44:22 +0100173 self.assertTrue(not self.s.cts, "CTS -> 0")
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200174 self.s.rts = True
cliechti7fc7da02009-08-03 23:49:02 +0000175 time.sleep(1.1)
Chris Liechtidedd3b72015-12-11 20:44:22 +0100176 self.assertTrue(self.s.cts, "CTS -> 1")
cliechtic813b212002-06-04 21:12:10 +0000177
178 def test2_DTR(self):
cliechtia135a672002-06-11 15:10:46 +0000179 """Test DTR/DSR"""
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200180 self.s.dtr = False
cliechti7fc7da02009-08-03 23:49:02 +0000181 time.sleep(1.1)
Chris Liechtidedd3b72015-12-11 20:44:22 +0100182 self.assertTrue(not self.s.dsr, "DSR -> 0")
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200183 self.s.dtr = True
cliechti7fc7da02009-08-03 23:49:02 +0000184 time.sleep(1.1)
Chris Liechtidedd3b72015-12-11 20:44:22 +0100185 self.assertTrue(self.s.dsr, "DSR -> 1")
cliechtic813b212002-06-04 21:12:10 +0000186
cliechtia135a672002-06-11 15:10:46 +0000187 def test3_RI(self):
188 """Test RI"""
Chris Liechtidedd3b72015-12-11 20:44:22 +0100189 self.assertTrue(not self.s.ri, "RI -> 0")
cliechtic813b212002-06-04 21:12:10 +0000190
cliechtif16c7702009-08-02 00:00:55 +0000191
cliechti62611612004-04-20 01:55:43 +0000192class Test_MoreTimeouts(unittest.TestCase):
193 """Test with timeouts"""
194 def setUp(self):
cliechtif16c7702009-08-02 00:00:55 +0000195 # create an closed serial port
cliechtie3ab3532009-08-05 12:40:38 +0000196 self.s = serial.serial_for_url(PORT, do_not_open=True)
cliechti58a3efe2009-07-21 20:56:52 +0000197
cliechti62611612004-04-20 01:55:43 +0000198 def tearDown(self):
Chris Liechtia6f0b762015-08-03 15:48:56 +0200199 #~ self.s.write(serial.XON)
Chris Liechtid73344d2015-08-06 17:52:51 +0200200 self.s.close()
201 # reopen... some faulty USB-serial adapter make next test fail otherwise...
202 self.s.timeout = 1
203 self.s.xonxoff = False
204 self.s.open()
205 self.s.read(10)
cliechti62611612004-04-20 01:55:43 +0000206 self.s.close()
207
208 def test_WriteTimeout(self):
209 """Test write() timeout."""
cliechti58a3efe2009-07-21 20:56:52 +0000210 # use xonxoff setting and the loop-back adapter to switch traffic on hold
cliechti62611612004-04-20 01:55:43 +0000211 self.s.port = PORT
Chris Liechtid73344d2015-08-06 17:52:51 +0200212 self.s.writeTimeout = True
213 self.s.xonxoff = True
cliechti62611612004-04-20 01:55:43 +0000214 self.s.open()
215 self.s.write(serial.XOFF)
cliechti58a3efe2009-07-21 20:56:52 +0000216 time.sleep(0.5) # some systems need a little delay so that they can react on XOFF
cliechti62611612004-04-20 01:55:43 +0000217 t1 = time.time()
Chris Liechtidedd3b72015-12-11 20:44:22 +0100218 self.assertRaises(serial.SerialTimeoutException, self.s.write, b"timeout please"*200)
cliechti62611612004-04-20 01:55:43 +0000219 t2 = time.time()
Chris Liechtidedd3b72015-12-11 20:44:22 +0100220 self.assertTrue( 0.9 <= (t2-t1) < 2.1, "Timeout not in the given interval (%s)" % (t2-t1))
cliechti42007d32009-07-30 17:21:26 +0000221
cliechti62611612004-04-20 01:55:43 +0000222
cliechtic813b212002-06-04 21:12:10 +0000223if __name__ == '__main__':
cliechtia135a672002-06-11 15:10:46 +0000224 import sys
cliechti8f9e5772009-02-07 00:30:53 +0000225 sys.stdout.write(__doc__)
cliechti764c88c2008-06-24 11:56:48 +0000226 if len(sys.argv) > 1:
227 PORT = sys.argv[1]
cliechti58a3efe2009-07-21 20:56:52 +0000228 sys.stdout.write("Testing port: %r\n" % PORT)
cliechti764c88c2008-06-24 11:56:48 +0000229 sys.argv[1:] = ['-v']
cliechtid6bf52c2003-10-01 02:28:12 +0000230 # When this module is executed from the command-line, it runs all its tests
cliechtic813b212002-06-04 21:12:10 +0000231 unittest.main()