2
# Python Serial Port Extension for Win32, Linux, BSD, Jython
5
# (C) 2001-2008 Chris Liechti <cliechti@gmx.net>
6
# this is distributed under a free software license, see license.txt
9
Some tests for the serial module.
10
Part of pyserial (http://pyserial.sf.net) (C)2001-2009 cliechti@gmx.net
12
Intended to be run on different platforms, to ensure portability of
15
For all these tests a simple hardware is required.
17
Shortcut these pin pairs:
22
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
31
# on which port should the tests be performed:
34
if sys.version_info >= (3, 0):
36
return bytes(string, 'latin1')
37
bytes_0to255 = bytes(range(256))
39
def data(string): return string
40
bytes_0to255 = ''.join([chr(x) for x in range(256)])
43
def segments(data, size=16):
44
for a in range(0, len(data), size):
48
class Test4_Nonblocking(unittest.TestCase):
49
"""Test with timeouts"""
53
self.s = serial.serial_for_url(PORT, timeout=self.timeout)
58
def test0_Messy(self):
59
"""NonBlocking (timeout=0)"""
60
# this is only here to write out the message in verbose mode
61
# because Test3 and Test4 print the same messages
63
def test1_ReadEmpty(self):
64
"""timeout: After port open, the input buffer must be empty"""
65
self.failUnlessEqual(self.s.read(1), data(''), "expected empty buffer")
67
def test2_Loopback(self):
68
"""timeout: each sent character should return (binary test).
69
this is also a test for the binary capability of a port."""
70
for block in segments(bytes_0to255):
73
# there might be a small delay until the character is ready (especially on win32)
75
self.failUnlessEqual(self.s.inWaiting(), length, "expected exactly %d character for inWainting()" % length)
76
self.failUnlessEqual(self.s.read(length), block)#, "expected a %r which was written before" % block)
77
self.failUnlessEqual(self.s.read(1), data(''), "expected empty buffer after all sent chars are read")
79
def test2_LoopbackTimeout(self):
80
"""timeout: test the timeout/immediate return.
81
partial results should be returned."""
82
self.s.write(data("HELLO"))
83
time.sleep(0.1) # there might be a small delay until the character is ready (especially on win32 and rfc2217)
84
# read more characters as are available to run in the timeout
85
self.failUnlessEqual(self.s.read(10), data('HELLO'), "expected the 'HELLO' which was written before")
86
self.failUnlessEqual(self.s.read(1), data(''), "expected empty buffer after all sent chars are read")
89
class Test3_Timeout(Test4_Nonblocking):
90
"""Same tests as the NonBlocking ones but this time with timeout"""
93
def test0_Messy(self):
94
"""Blocking (timeout=1)"""
95
# this is only here to write out the message in verbose mode
96
# because Test3 and Test4 print the same messages
99
class SendEvent(threading.Thread):
100
def __init__(self, serial, delay=3):
101
threading.Thread.__init__(self)
104
self.x = threading.Event()
109
time.sleep(self.delay)
111
self.serial.write(data("E"))
116
return self.x.isSet()
122
class Test1_Forever(unittest.TestCase):
123
"""Tests a port with no timeout. These tests require that a
124
character is sent after some time to stop the test, this is done
125
through the SendEvent class and the Loopback HW."""
127
self.s = serial.serial_for_url(PORT, timeout=None)
128
self.event = SendEvent(self.s)
134
def test2_ReadEmpty(self):
135
"""no timeout: after port open, the input buffer must be empty (read).
136
a character is sent after some time to terminate the test (SendEvent)."""
138
if not (self.event.isSet() and c == data('E')):
139
self.fail("expected marker")
142
class Test2_Forever(unittest.TestCase):
143
"""Tests a port with no timeout"""
145
self.s = serial.serial_for_url(PORT, timeout=None)
150
def test1_inWaitingEmpty(self):
151
"""no timeout: after port open, the input buffer must be empty (inWaiting)"""
152
self.failUnlessEqual(self.s.inWaiting(), 0, "expected empty buffer")
154
def test2_Loopback(self):
155
"""no timeout: each sent character should return (binary test).
156
this is also a test for the binary capability of a port."""
157
for block in segments(bytes_0to255):
160
# there might be a small delay until the character is ready (especially on win32 and rfc2217)
162
self.failUnlessEqual(self.s.inWaiting(), length)#, "expected exactly %d character for inWainting()" % length)
163
self.failUnlessEqual(self.s.read(length), block) #, "expected %r which was written before" % block)
164
self.failUnlessEqual(self.s.inWaiting(), 0, "expected empty buffer after all sent chars are read")
167
class Test0_DataWires(unittest.TestCase):
168
"""Test modem control lines"""
170
self.s = serial.serial_for_url(PORT)
179
self.failUnless(not self.s.getCTS(), "CTS -> 0")
182
self.failUnless(self.s.getCTS(), "CTS -> 1")
188
self.failUnless(not self.s.getDSR(), "DSR -> 0")
191
self.failUnless(self.s.getDSR(), "DSR -> 1")
195
self.failUnless(not self.s.getRI(), "RI -> 0")
198
class Test_MoreTimeouts(unittest.TestCase):
199
"""Test with timeouts"""
201
# create an closed serial port
202
self.s = serial.serial_for_url(PORT, do_not_open=True)
207
def test_WriteTimeout(self):
208
"""Test write() timeout."""
209
# use xonxoff setting and the loop-back adapter to switch traffic on hold
211
self.s.writeTimeout = 1
214
self.s.write(serial.XOFF)
215
time.sleep(0.5) # some systems need a little delay so that they can react on XOFF
217
self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, data("timeout please"*200))
219
self.failUnless( 0.9 <= (t2-t1) < 2.1, "Timeout not in the given interval (%s)" % (t2-t1))
222
if __name__ == '__main__':
224
sys.stdout.write(__doc__)
225
if len(sys.argv) > 1:
227
sys.stdout.write("Testing port: %r\n" % PORT)
228
sys.argv[1:] = ['-v']
229
# When this module is executed from the command-line, it runs all its tests