blob: ea2865086bafa58555acd84f422f9f2c953652de [file] [log] [blame]
cliechticb65c682008-06-22 22:47:02 +00001#! /usr/bin/env python
2# Python Serial Port Extension for Win32, Linux, BSD, Jython
3# see __init__.py
cliechtid6bf52c2003-10-01 02:28:12 +00004#
Chris Liechtid73344d2015-08-06 17:52:51 +02005# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
cliechtid6bf52c2003-10-01 02:28:12 +00006# this is distributed under a free software license, see license.txt
cliechti1e95baa2002-06-04 22:46:50 +00007
cliechticb65c682008-06-22 22:47:02 +00008"""\
9Some tests for the serial module.
Chris Liechtid73344d2015-08-06 17:52:51 +020010Part of pyserial (http://pyserial.sf.net) (C)2001-2015 cliechti@gmx.net
cliechtid6bf52c2003-10-01 02:28:12 +000011
12Intended to be run on different platforms, to ensure portability of
cliechti1e95baa2002-06-04 22:46:50 +000013the code.
14
cliechtid6bf52c2003-10-01 02:28:12 +000015For all these tests a simple hardware is required.
cliechti1e95baa2002-06-04 22:46:50 +000016Loopback HW adapter:
cliechtid6bf52c2003-10-01 02:28:12 +000017Shortcut these pin pairs:
cliechtia135a672002-06-11 15:10:46 +000018 TX <-> RX
19 RTS <-> CTS
20 DTR <-> DSR
cliechti1e95baa2002-06-04 22:46:50 +000021
cliechtid6bf52c2003-10-01 02:28:12 +000022On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
cliechti1e95baa2002-06-04 22:46:50 +000023"""
24
cliechtif16c7702009-08-02 00:00:55 +000025import unittest
26import threading
27import time
cliechtia75900c2009-07-28 00:12:52 +000028import sys
cliechti1e95baa2002-06-04 22:46:50 +000029import serial
cliechtic813b212002-06-04 21:12:10 +000030
cliechti58a3efe2009-07-21 20:56:52 +000031# on which port should the tests be performed:
Chris Liechti6ed12e02015-09-18 21:23:42 +020032PORT = 'loop://'
cliechtic813b212002-06-04 21:12:10 +000033
Chris Liechti6ed12e02015-09-18 21:23:42 +020034# indirection via bytearray b/c bytes(range(256)) does something else in Pyhton 2.7
35bytes_0to255 = bytes(bytearray(range(256)))
cliechti54c534e2009-08-06 23:24:26 +000036
37
38def segments(data, size=16):
39 for a in range(0, len(data), size):
40 yield data[a:a+size]
cliechtia75900c2009-07-28 00:12:52 +000041
cliechtic813b212002-06-04 21:12:10 +000042
cliechtia135a672002-06-11 15:10:46 +000043class Test4_Nonblocking(unittest.TestCase):
cliechtic813b212002-06-04 21:12:10 +000044 """Test with timeouts"""
cliechtia75900c2009-07-28 00:12:52 +000045 timeout = 0
46
cliechtic813b212002-06-04 21:12:10 +000047 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +000048 self.s = serial.serial_for_url(PORT, timeout=self.timeout)
cliechti58a3efe2009-07-21 20:56:52 +000049
cliechtic813b212002-06-04 21:12:10 +000050 def tearDown(self):
51 self.s.close()
52
cliechtia135a672002-06-11 15:10:46 +000053 def test0_Messy(self):
54 """NonBlocking (timeout=0)"""
cliechti58a3efe2009-07-21 20:56:52 +000055 # this is only here to write out the message in verbose mode
56 # because Test3 and Test4 print the same messages
cliechtia135a672002-06-11 15:10:46 +000057
cliechtic813b212002-06-04 21:12:10 +000058 def test1_ReadEmpty(self):
cliechtia135a672002-06-11 15:10:46 +000059 """timeout: After port open, the input buffer must be empty"""
Chris Liechti6ed12e02015-09-18 21:23:42 +020060 self.failUnlessEqual(self.s.read(1), b'', "expected empty buffer")
cliechti58a3efe2009-07-21 20:56:52 +000061
cliechtic813b212002-06-04 21:12:10 +000062 def test2_Loopback(self):
cliechtia135a672002-06-11 15:10:46 +000063 """timeout: each sent character should return (binary test).
cliechti1e95baa2002-06-04 22:46:50 +000064 this is also a test for the binary capability of a port."""
cliechti54c534e2009-08-06 23:24:26 +000065 for block in segments(bytes_0to255):
66 length = len(block)
67 self.s.write(block)
cliechtia75900c2009-07-28 00:12:52 +000068 # there might be a small delay until the character is ready (especially on win32)
cliechtif16c7702009-08-02 00:00:55 +000069 time.sleep(0.05)
Chris Liechti20ed5fd2015-09-02 02:48:51 +020070 self.failUnlessEqual(self.s.in_waiting, length, "expected exactly %d character for inWainting()" % length)
cliechti54c534e2009-08-06 23:24:26 +000071 self.failUnlessEqual(self.s.read(length), block)#, "expected a %r which was written before" % block)
Chris Liechti6ed12e02015-09-18 21:23:42 +020072 self.failUnlessEqual(self.s.read(1), b'', "expected empty buffer after all sent chars are read")
cliechti58a3efe2009-07-21 20:56:52 +000073
cliechtic813b212002-06-04 21:12:10 +000074 def test2_LoopbackTimeout(self):
cliechtia135a672002-06-11 15:10:46 +000075 """timeout: test the timeout/immediate return.
76 partial results should be returned."""
Chris Liechti6ed12e02015-09-18 21:23:42 +020077 self.s.write(b"HELLO")
cliechtif16c7702009-08-02 00:00:55 +000078 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 +000079 # read more characters as are available to run in the timeout
Chris Liechti6ed12e02015-09-18 21:23:42 +020080 self.failUnlessEqual(self.s.read(10), b'HELLO', "expected the 'HELLO' which was written before")
81 self.failUnlessEqual(self.s.read(1), b'', "expected empty buffer after all sent chars are read")
cliechtic813b212002-06-04 21:12:10 +000082
83
cliechtia135a672002-06-11 15:10:46 +000084class Test3_Timeout(Test4_Nonblocking):
cliechtic813b212002-06-04 21:12:10 +000085 """Same tests as the NonBlocking ones but this time with timeout"""
cliechtia75900c2009-07-28 00:12:52 +000086 timeout = 1
cliechti58a3efe2009-07-21 20:56:52 +000087
cliechtia135a672002-06-11 15:10:46 +000088 def test0_Messy(self):
89 """Blocking (timeout=1)"""
cliechti58a3efe2009-07-21 20:56:52 +000090 # this is only here to write out the message in verbose mode
91 # because Test3 and Test4 print the same messages
cliechtic813b212002-06-04 21:12:10 +000092
cliechtif16c7702009-08-02 00:00:55 +000093
cliechti1e95baa2002-06-04 22:46:50 +000094class SendEvent(threading.Thread):
cliechtif16c7702009-08-02 00:00:55 +000095 def __init__(self, serial, delay=3):
cliechti1e95baa2002-06-04 22:46:50 +000096 threading.Thread.__init__(self)
97 self.serial = serial
98 self.delay = delay
99 self.x = threading.Event()
100 self.stopped = 0
101 self.start()
cliechti58a3efe2009-07-21 20:56:52 +0000102
cliechti1e95baa2002-06-04 22:46:50 +0000103 def run(self):
104 time.sleep(self.delay)
cliechti8b599c92011-08-15 13:12:09 +0000105 self.x.set()
cliechti1e95baa2002-06-04 22:46:50 +0000106 if not self.stopped:
Chris Liechti6ed12e02015-09-18 21:23:42 +0200107 self.serial.write(b"E")
cliechti7fc7da02009-08-03 23:49:02 +0000108 self.serial.flush()
cliechti58a3efe2009-07-21 20:56:52 +0000109
cliechti1e95baa2002-06-04 22:46:50 +0000110 def isSet(self):
111 return self.x.isSet()
cliechti58a3efe2009-07-21 20:56:52 +0000112
cliechti1e95baa2002-06-04 22:46:50 +0000113 def stop(self):
114 self.stopped = 1
115 self.x.wait()
116
117class Test1_Forever(unittest.TestCase):
118 """Tests a port with no timeout. These tests require that a
119 character is sent after some time to stop the test, this is done
120 through the SendEvent class and the Loopback HW."""
121 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +0000122 self.s = serial.serial_for_url(PORT, timeout=None)
cliechti1e95baa2002-06-04 22:46:50 +0000123 self.event = SendEvent(self.s)
cliechti58a3efe2009-07-21 20:56:52 +0000124
cliechti1e95baa2002-06-04 22:46:50 +0000125 def tearDown(self):
126 self.event.stop()
127 self.s.close()
128
129 def test2_ReadEmpty(self):
cliechtia135a672002-06-11 15:10:46 +0000130 """no timeout: after port open, the input buffer must be empty (read).
131 a character is sent after some time to terminate the test (SendEvent)."""
cliechti1e95baa2002-06-04 22:46:50 +0000132 c = self.s.read(1)
Chris Liechti6ed12e02015-09-18 21:23:42 +0200133 if not (self.event.isSet() and c == b'E'):
cliechti8b599c92011-08-15 13:12:09 +0000134 self.fail("expected marker (evt=%r, c=%r)" % (self.event.isSet(), c))
cliechti1e95baa2002-06-04 22:46:50 +0000135
cliechtif16c7702009-08-02 00:00:55 +0000136
cliechti1e95baa2002-06-04 22:46:50 +0000137class Test2_Forever(unittest.TestCase):
138 """Tests a port with no timeout"""
139 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +0000140 self.s = serial.serial_for_url(PORT, timeout=None)
cliechti58a3efe2009-07-21 20:56:52 +0000141
cliechti1e95baa2002-06-04 22:46:50 +0000142 def tearDown(self):
143 self.s.close()
144
145 def test1_inWaitingEmpty(self):
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200146 """no timeout: after port open, the input buffer must be empty (in_waiting)"""
147 self.failUnlessEqual(self.s.in_waiting, 0, "expected empty buffer")
cliechti1e95baa2002-06-04 22:46:50 +0000148
149 def test2_Loopback(self):
cliechtia135a672002-06-11 15:10:46 +0000150 """no timeout: each sent character should return (binary test).
cliechti1e95baa2002-06-04 22:46:50 +0000151 this is also a test for the binary capability of a port."""
cliechti54c534e2009-08-06 23:24:26 +0000152 for block in segments(bytes_0to255):
153 length = len(block)
154 self.s.write(block)
cliechtif16c7702009-08-02 00:00:55 +0000155 # there might be a small delay until the character is ready (especially on win32 and rfc2217)
156 time.sleep(0.05)
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200157 self.failUnlessEqual(self.s.in_waiting, length)#, "expected exactly %d character for inWainting()" % length)
cliechti54c534e2009-08-06 23:24:26 +0000158 self.failUnlessEqual(self.s.read(length), block) #, "expected %r which was written before" % block)
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200159 self.failUnlessEqual(self.s.in_waiting, 0, "expected empty buffer after all sent chars are read")
cliechtic813b212002-06-04 21:12:10 +0000160
161
cliechtia135a672002-06-11 15:10:46 +0000162class Test0_DataWires(unittest.TestCase):
cliechtic813b212002-06-04 21:12:10 +0000163 """Test modem control lines"""
164 def setUp(self):
cliechtie3ab3532009-08-05 12:40:38 +0000165 self.s = serial.serial_for_url(PORT)
cliechti58a3efe2009-07-21 20:56:52 +0000166
cliechtic813b212002-06-04 21:12:10 +0000167 def tearDown(self):
168 self.s.close()
169
170 def test1_RTS(self):
cliechtia135a672002-06-11 15:10:46 +0000171 """Test RTS/CTS"""
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200172 self.s.rts = False
cliechti7fc7da02009-08-03 23:49:02 +0000173 time.sleep(1.1)
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200174 self.failUnless(not self.s.cts, "CTS -> 0")
175 self.s.rts = True
cliechti7fc7da02009-08-03 23:49:02 +0000176 time.sleep(1.1)
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200177 self.failUnless(self.s.cts, "CTS -> 1")
cliechtic813b212002-06-04 21:12:10 +0000178
179 def test2_DTR(self):
cliechtia135a672002-06-11 15:10:46 +0000180 """Test DTR/DSR"""
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200181 self.s.dtr = False
cliechti7fc7da02009-08-03 23:49:02 +0000182 time.sleep(1.1)
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200183 self.failUnless(not self.s.dsr, "DSR -> 0")
184 self.s.dtr = True
cliechti7fc7da02009-08-03 23:49:02 +0000185 time.sleep(1.1)
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200186 self.failUnless(self.s.dsr, "DSR -> 1")
cliechtic813b212002-06-04 21:12:10 +0000187
cliechtia135a672002-06-11 15:10:46 +0000188 def test3_RI(self):
189 """Test RI"""
Chris Liechti20ed5fd2015-09-02 02:48:51 +0200190 self.failUnless(not self.s.ri, "RI -> 0")
cliechtic813b212002-06-04 21:12:10 +0000191
cliechtif16c7702009-08-02 00:00:55 +0000192
cliechti62611612004-04-20 01:55:43 +0000193class Test_MoreTimeouts(unittest.TestCase):
194 """Test with timeouts"""
195 def setUp(self):
cliechtif16c7702009-08-02 00:00:55 +0000196 # create an closed serial port
cliechtie3ab3532009-08-05 12:40:38 +0000197 self.s = serial.serial_for_url(PORT, do_not_open=True)
cliechti58a3efe2009-07-21 20:56:52 +0000198
cliechti62611612004-04-20 01:55:43 +0000199 def tearDown(self):
Chris Liechtia6f0b762015-08-03 15:48:56 +0200200 #~ self.s.write(serial.XON)
Chris Liechtid73344d2015-08-06 17:52:51 +0200201 self.s.close()
202 # reopen... some faulty USB-serial adapter make next test fail otherwise...
203 self.s.timeout = 1
204 self.s.xonxoff = False
205 self.s.open()
206 self.s.read(10)
cliechti62611612004-04-20 01:55:43 +0000207 self.s.close()
208
209 def test_WriteTimeout(self):
210 """Test write() timeout."""
cliechti58a3efe2009-07-21 20:56:52 +0000211 # use xonxoff setting and the loop-back adapter to switch traffic on hold
cliechti62611612004-04-20 01:55:43 +0000212 self.s.port = PORT
Chris Liechtid73344d2015-08-06 17:52:51 +0200213 self.s.writeTimeout = True
214 self.s.xonxoff = True
cliechti62611612004-04-20 01:55:43 +0000215 self.s.open()
216 self.s.write(serial.XOFF)
cliechti58a3efe2009-07-21 20:56:52 +0000217 time.sleep(0.5) # some systems need a little delay so that they can react on XOFF
cliechti62611612004-04-20 01:55:43 +0000218 t1 = time.time()
Chris Liechti6ed12e02015-09-18 21:23:42 +0200219 self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, b"timeout please"*200)
cliechti62611612004-04-20 01:55:43 +0000220 t2 = time.time()
cliechti42007d32009-07-30 17:21:26 +0000221 self.failUnless( 0.9 <= (t2-t1) < 2.1, "Timeout not in the given interval (%s)" % (t2-t1))
222
cliechti62611612004-04-20 01:55:43 +0000223
cliechtic813b212002-06-04 21:12:10 +0000224if __name__ == '__main__':
cliechtia135a672002-06-11 15:10:46 +0000225 import sys
cliechti8f9e5772009-02-07 00:30:53 +0000226 sys.stdout.write(__doc__)
cliechti764c88c2008-06-24 11:56:48 +0000227 if len(sys.argv) > 1:
228 PORT = sys.argv[1]
cliechti58a3efe2009-07-21 20:56:52 +0000229 sys.stdout.write("Testing port: %r\n" % PORT)
cliechti764c88c2008-06-24 11:56:48 +0000230 sys.argv[1:] = ['-v']
cliechtid6bf52c2003-10-01 02:28:12 +0000231 # When this module is executed from the command-line, it runs all its tests
cliechtic813b212002-06-04 21:12:10 +0000232 unittest.main()