blob: a059d1eac535a1ceb79d6f57e7d2bb3d2642728a [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#
cliechticb65c682008-06-22 22:47:02 +00005# (C) 2001-2008 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.
10Part of pyserial (http://pyserial.sf.net) (C)2001-2008 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
25import unittest, threading, time
26import serial
cliechtic813b212002-06-04 21:12:10 +000027
cliechtid6bf52c2003-10-01 02:28:12 +000028#on which port should the tests be performed:
cliechtic813b212002-06-04 21:12:10 +000029PORT=0
30
cliechtic813b212002-06-04 21:12:10 +000031
cliechtia135a672002-06-11 15:10:46 +000032class Test4_Nonblocking(unittest.TestCase):
cliechtic813b212002-06-04 21:12:10 +000033 """Test with timeouts"""
34 timeout=0
35 def setUp(self):
36 self.s = serial.Serial(PORT,timeout=self.timeout)
37 def tearDown(self):
38 self.s.close()
39
cliechtia135a672002-06-11 15:10:46 +000040 def test0_Messy(self):
41 """NonBlocking (timeout=0)"""
42 #this is only here to write out the message in verbose mode
43 #because Test3 and Test4 print the same messages
44
cliechtic813b212002-06-04 21:12:10 +000045 def test1_ReadEmpty(self):
cliechtia135a672002-06-11 15:10:46 +000046 """timeout: After port open, the input buffer must be empty"""
cliechtic813b212002-06-04 21:12:10 +000047 self.failUnless(self.s.read(1)=='', "expected empty buffer")
48 def test2_Loopback(self):
cliechtia135a672002-06-11 15:10:46 +000049 """timeout: each sent character should return (binary test).
cliechti1e95baa2002-06-04 22:46:50 +000050 this is also a test for the binary capability of a port."""
cliechtic813b212002-06-04 21:12:10 +000051 for c in map(chr,range(256)):
52 self.s.write(c)
cliechti1e95baa2002-06-04 22:46:50 +000053 time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
cliechtia135a672002-06-11 15:10:46 +000054 self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
cliechti107db8d2004-01-15 01:20:23 +000055 self.failUnless(self.s.read(1)==c, "expected a '%s' which was written before" % c)
cliechtic813b212002-06-04 21:12:10 +000056 self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
57 def test2_LoopbackTimeout(self):
cliechtia135a672002-06-11 15:10:46 +000058 """timeout: test the timeout/immediate return.
59 partial results should be returned."""
cliechtic813b212002-06-04 21:12:10 +000060 self.s.write("HELLO")
cliechticb65c682008-06-22 22:47:02 +000061 time.sleep(0.1) #there might be a small delay until the character is ready (especialy on win32)
cliechtic813b212002-06-04 21:12:10 +000062 #read more characters as are available to run in the timeout
cliechticb65c682008-06-22 22:47:02 +000063 self.failUnless(self.s.read(10)=='HELLO', "expected the 'HELLO' which was written before")
cliechtic813b212002-06-04 21:12:10 +000064 self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
65
66
cliechtia135a672002-06-11 15:10:46 +000067class Test3_Timeout(Test4_Nonblocking):
cliechtic813b212002-06-04 21:12:10 +000068 """Same tests as the NonBlocking ones but this time with timeout"""
69 timeout=1
cliechtia135a672002-06-11 15:10:46 +000070 def test0_Messy(self):
71 """Blocking (timeout=1)"""
72 #this is only here to write out the message in verbose mode
73 #because Test3 and Test4 print the same messages
cliechtic813b212002-06-04 21:12:10 +000074
cliechti1e95baa2002-06-04 22:46:50 +000075class SendEvent(threading.Thread):
76 def __init__(self, serial, delay=1):
77 threading.Thread.__init__(self)
78 self.serial = serial
79 self.delay = delay
80 self.x = threading.Event()
81 self.stopped = 0
82 self.start()
83 def run(self):
84 time.sleep(self.delay)
85 if not self.stopped:
86 self.serial.write("E")
87 self.x.set()
88 def isSet(self):
89 return self.x.isSet()
90 def stop(self):
91 self.stopped = 1
92 self.x.wait()
93
94class Test1_Forever(unittest.TestCase):
95 """Tests a port with no timeout. These tests require that a
96 character is sent after some time to stop the test, this is done
97 through the SendEvent class and the Loopback HW."""
98 def setUp(self):
cliechti62611612004-04-20 01:55:43 +000099 self.s = serial.Serial(PORT, timeout=None)
cliechti1e95baa2002-06-04 22:46:50 +0000100 self.event = SendEvent(self.s)
101 def tearDown(self):
102 self.event.stop()
103 self.s.close()
104
105 def test2_ReadEmpty(self):
cliechtia135a672002-06-11 15:10:46 +0000106 """no timeout: after port open, the input buffer must be empty (read).
107 a character is sent after some time to terminate the test (SendEvent)."""
cliechti1e95baa2002-06-04 22:46:50 +0000108 c = self.s.read(1)
cliechti62611612004-04-20 01:55:43 +0000109 if not (self.event.isSet() and c == 'E'):
cliechti1e95baa2002-06-04 22:46:50 +0000110 self.fail("expected marker")
111
112class Test2_Forever(unittest.TestCase):
113 """Tests a port with no timeout"""
114 def setUp(self):
115 self.s = serial.Serial(PORT,timeout=None)
116 def tearDown(self):
117 self.s.close()
118
119 def test1_inWaitingEmpty(self):
cliechtia135a672002-06-11 15:10:46 +0000120 """no timeout: after port open, the input buffer must be empty (inWaiting)"""
cliechti1e95baa2002-06-04 22:46:50 +0000121 self.failUnless(self.s.inWaiting()==0, "expected empty buffer")
122
123 def test2_Loopback(self):
cliechtia135a672002-06-11 15:10:46 +0000124 """no timeout: each sent character should return (binary test).
cliechti1e95baa2002-06-04 22:46:50 +0000125 this is also a test for the binary capability of a port."""
126 for c in map(chr,range(256)):
127 self.s.write(c)
128 time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
cliechtia135a672002-06-11 15:10:46 +0000129 self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
cliechti1e95baa2002-06-04 22:46:50 +0000130 self.failUnless(self.s.read(1)==c, "expected an '%s' which was written before" % c)
131 self.failUnless(self.s.inWaiting()==0, "expected empty buffer after all sent chars are read")
cliechtic813b212002-06-04 21:12:10 +0000132
133
cliechtia135a672002-06-11 15:10:46 +0000134class Test0_DataWires(unittest.TestCase):
cliechtic813b212002-06-04 21:12:10 +0000135 """Test modem control lines"""
136 def setUp(self):
137 self.s = serial.Serial(PORT)
138 def tearDown(self):
139 self.s.close()
140
141 def test1_RTS(self):
cliechtia135a672002-06-11 15:10:46 +0000142 """Test RTS/CTS"""
cliechtic813b212002-06-04 21:12:10 +0000143 self.s.setRTS(0)
cliechticb65c682008-06-22 22:47:02 +0000144 self.failUnless(not self.s.getCTS(), "CTS -> 0")
cliechtic813b212002-06-04 21:12:10 +0000145 self.s.setRTS(1)
cliechticb65c682008-06-22 22:47:02 +0000146 self.failUnless(self.s.getCTS(), "CTS -> 1")
cliechtic813b212002-06-04 21:12:10 +0000147
148 def test2_DTR(self):
cliechtia135a672002-06-11 15:10:46 +0000149 """Test DTR/DSR"""
cliechtic813b212002-06-04 21:12:10 +0000150 self.s.setDTR(0)
cliechticb65c682008-06-22 22:47:02 +0000151 self.failUnless(not self.s.getDSR(), "DSR -> 0")
cliechtic813b212002-06-04 21:12:10 +0000152 self.s.setDTR(1)
cliechticb65c682008-06-22 22:47:02 +0000153 self.failUnless(self.s.getDSR(), "DSR -> 1")
cliechtic813b212002-06-04 21:12:10 +0000154
cliechtia135a672002-06-11 15:10:46 +0000155 def test3_RI(self):
156 """Test RI"""
cliechticb65c682008-06-22 22:47:02 +0000157 self.failUnless(not self.s.getRI(), "RI -> 0")
cliechtic813b212002-06-04 21:12:10 +0000158
cliechti62611612004-04-20 01:55:43 +0000159class Test_MoreTimeouts(unittest.TestCase):
160 """Test with timeouts"""
161 def setUp(self):
162 self.s = serial.Serial() #create an closed serial port
163
164 def tearDown(self):
165 self.s.close()
166
167 def test_WriteTimeout(self):
168 """Test write() timeout."""
169 #use xonxoff setting and the loopback adapter to switch traffic on hold
170 self.s.port = PORT
171 self.s.writeTimeout = 1
172 self.s.xonxoff = 1
173 self.s.open()
174 self.s.write(serial.XOFF)
175 time.sleep(0.1) #some systems need a little delay so that they can react on XOFF
176 t1 = time.time()
177 self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, "timeout please"*100)
178 t2 = time.time()
179 self.failUnless( 1 <= (t2-t1) < 2, "Timeout not in the given intervall (%s)" % (t2-t1))
180
cliechtic813b212002-06-04 21:12:10 +0000181if __name__ == '__main__':
cliechtia135a672002-06-11 15:10:46 +0000182 import sys
183 print __doc__
cliechti764c88c2008-06-24 11:56:48 +0000184 if len(sys.argv) > 1:
185 PORT = sys.argv[1]
cliechtid6bf52c2003-10-01 02:28:12 +0000186 print "Testing port", PORT
cliechti764c88c2008-06-24 11:56:48 +0000187 sys.argv[1:] = ['-v']
cliechtid6bf52c2003-10-01 02:28:12 +0000188 # When this module is executed from the command-line, it runs all its tests
cliechtic813b212002-06-04 21:12:10 +0000189 unittest.main()