2
#Python Serial Port Extension for Win32, Linux, BSD, Jython
5
#(C) 2001-2003 Chris Liechti <cliechti@gmx.net>
6
# this is distributed under a free software license, see license.txt
8
"""Some tests for the serial module.
9
Part of pyserial (http://pyserial.sf.net) (C)2002-2003 cliechti@gmx.net
11
Intended to be run on different platforms, to ensure portability of
14
For all these tests a simple hardware is required.
16
Shortcut these pin pairs:
21
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
24
import unittest, threading, time
27
#on which port should the tests be performed:
31
class Test4_Nonblocking(unittest.TestCase):
32
"""Test with timeouts"""
35
self.s = serial.Serial(PORT,timeout=self.timeout)
39
def test0_Messy(self):
40
"""NonBlocking (timeout=0)"""
41
#this is only here to write out the message in verbose mode
42
#because Test3 and Test4 print the same messages
44
def test1_ReadEmpty(self):
45
"""timeout: After port open, the input buffer must be empty"""
46
self.failUnless(self.s.read(1)=='', "expected empty buffer")
47
def test2_Loopback(self):
48
"""timeout: each sent character should return (binary test).
49
this is also a test for the binary capability of a port."""
50
for c in map(chr,range(256)):
52
time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
53
self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
54
self.failUnless(self.s.read(1)==c, "expected a '%s' which was written before" % c)
55
self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
56
def test2_LoopbackTimeout(self):
57
"""timeout: test the timeout/immediate return.
58
partial results should be returned."""
60
time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
61
#read more characters as are available to run in the timeout
62
self.failUnless(self.s.read(10)=='HELLO', "expected an 'HELLO' which was written before")
63
self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
66
class Test3_Timeout(Test4_Nonblocking):
67
"""Same tests as the NonBlocking ones but this time with timeout"""
69
def test0_Messy(self):
70
"""Blocking (timeout=1)"""
71
#this is only here to write out the message in verbose mode
72
#because Test3 and Test4 print the same messages
74
class SendEvent(threading.Thread):
75
def __init__(self, serial, delay=1):
76
threading.Thread.__init__(self)
79
self.x = threading.Event()
83
time.sleep(self.delay)
85
self.serial.write("E")
93
class Test1_Forever(unittest.TestCase):
94
"""Tests a port with no timeout. These tests require that a
95
character is sent after some time to stop the test, this is done
96
through the SendEvent class and the Loopback HW."""
98
self.s = serial.Serial(PORT, timeout=None)
99
self.event = SendEvent(self.s)
104
def test2_ReadEmpty(self):
105
"""no timeout: after port open, the input buffer must be empty (read).
106
a character is sent after some time to terminate the test (SendEvent)."""
108
if not (self.event.isSet() and c == 'E'):
109
self.fail("expected marker")
111
class Test2_Forever(unittest.TestCase):
112
"""Tests a port with no timeout"""
114
self.s = serial.Serial(PORT,timeout=None)
118
def test1_inWaitingEmpty(self):
119
"""no timeout: after port open, the input buffer must be empty (inWaiting)"""
120
self.failUnless(self.s.inWaiting()==0, "expected empty buffer")
122
def test2_Loopback(self):
123
"""no timeout: each sent character should return (binary test).
124
this is also a test for the binary capability of a port."""
125
for c in map(chr,range(256)):
127
time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
128
self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
129
self.failUnless(self.s.read(1)==c, "expected an '%s' which was written before" % c)
130
self.failUnless(self.s.inWaiting()==0, "expected empty buffer after all sent chars are read")
133
class Test0_DataWires(unittest.TestCase):
134
"""Test modem control lines"""
136
self.s = serial.Serial(PORT)
143
self.failUnless(self.s.getCTS()==0, "CTS -> 0")
145
self.failUnless(self.s.getCTS()==1, "CTS -> 1")
150
self.failUnless(self.s.getDSR()==0, "DSR -> 0")
152
self.failUnless(self.s.getDSR()==1, "DSR -> 1")
156
self.failUnless(self.s.getRI()==0, "RI -> 0")
158
class Test_MoreTimeouts(unittest.TestCase):
159
"""Test with timeouts"""
161
self.s = serial.Serial() #create an closed serial port
166
def test_WriteTimeout(self):
167
"""Test write() timeout."""
168
#use xonxoff setting and the loopback adapter to switch traffic on hold
170
self.s.writeTimeout = 1
173
self.s.write(serial.XOFF)
174
time.sleep(0.1) #some systems need a little delay so that they can react on XOFF
176
self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, "timeout please"*100)
178
self.failUnless( 1 <= (t2-t1) < 2, "Timeout not in the given intervall (%s)" % (t2-t1))
180
if __name__ == '__main__':
183
print "Testing port", PORT
184
sys.argv.append('-v')
185
# When this module is executed from the command-line, it runs all its tests
2
#Python Serial Port Extension for Win32, Linux, BSD, Jython
5
#(C) 2001-2003 Chris Liechti <cliechti@gmx.net>
6
# this is distributed under a free software license, see license.txt
8
"""Some tests for the serial module.
9
Part of pyserial (http://pyserial.sf.net) (C)2002-2003 cliechti@gmx.net
11
Intended to be run on different platforms, to ensure portability of
14
For all these tests a simple hardware is required.
16
Shortcut these pin pairs:
21
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
24
import unittest, threading, time
27
#on which port should the tests be performed:
31
class Test4_Nonblocking(unittest.TestCase):
32
"""Test with timeouts"""
35
self.s = serial.Serial(PORT,timeout=self.timeout)
39
def test0_Messy(self):
40
"""NonBlocking (timeout=0)"""
41
#this is only here to write out the message in verbose mode
42
#because Test3 and Test4 print the same messages
44
def test1_ReadEmpty(self):
45
"""timeout: After port open, the input buffer must be empty"""
46
self.failUnless(self.s.read(1)=='', "expected empty buffer")
47
def test2_Loopback(self):
48
"""timeout: each sent character should return (binary test).
49
this is also a test for the binary capability of a port."""
50
for c in map(chr,range(256)):
52
time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
53
self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
54
self.failUnless(self.s.read(1)==c, "expected a '%s' which was written before" % c)
55
self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
56
def test2_LoopbackTimeout(self):
57
"""timeout: test the timeout/immediate return.
58
partial results should be returned."""
60
time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
61
#read more characters as are available to run in the timeout
62
self.failUnless(self.s.read(10)=='HELLO', "expected an 'HELLO' which was written before")
63
self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
66
class Test3_Timeout(Test4_Nonblocking):
67
"""Same tests as the NonBlocking ones but this time with timeout"""
69
def test0_Messy(self):
70
"""Blocking (timeout=1)"""
71
#this is only here to write out the message in verbose mode
72
#because Test3 and Test4 print the same messages
74
class SendEvent(threading.Thread):
75
def __init__(self, serial, delay=1):
76
threading.Thread.__init__(self)
79
self.x = threading.Event()
83
time.sleep(self.delay)
85
self.serial.write("E")
93
class Test1_Forever(unittest.TestCase):
94
"""Tests a port with no timeout. These tests require that a
95
character is sent after some time to stop the test, this is done
96
through the SendEvent class and the Loopback HW."""
98
self.s = serial.Serial(PORT, timeout=None)
99
self.event = SendEvent(self.s)
104
def test2_ReadEmpty(self):
105
"""no timeout: after port open, the input buffer must be empty (read).
106
a character is sent after some time to terminate the test (SendEvent)."""
108
if not (self.event.isSet() and c == 'E'):
109
self.fail("expected marker")
111
class Test2_Forever(unittest.TestCase):
112
"""Tests a port with no timeout"""
114
self.s = serial.Serial(PORT,timeout=None)
118
def test1_inWaitingEmpty(self):
119
"""no timeout: after port open, the input buffer must be empty (inWaiting)"""
120
self.failUnless(self.s.inWaiting()==0, "expected empty buffer")
122
def test2_Loopback(self):
123
"""no timeout: each sent character should return (binary test).
124
this is also a test for the binary capability of a port."""
125
for c in map(chr,range(256)):
127
time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
128
self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
129
self.failUnless(self.s.read(1)==c, "expected an '%s' which was written before" % c)
130
self.failUnless(self.s.inWaiting()==0, "expected empty buffer after all sent chars are read")
133
class Test0_DataWires(unittest.TestCase):
134
"""Test modem control lines"""
136
self.s = serial.Serial(PORT)
143
self.failUnless(self.s.getCTS()==0, "CTS -> 0")
145
self.failUnless(self.s.getCTS()==1, "CTS -> 1")
150
self.failUnless(self.s.getDSR()==0, "DSR -> 0")
152
self.failUnless(self.s.getDSR()==1, "DSR -> 1")
156
self.failUnless(self.s.getRI()==0, "RI -> 0")
158
class Test_MoreTimeouts(unittest.TestCase):
159
"""Test with timeouts"""
161
self.s = serial.Serial() #create an closed serial port
166
def test_WriteTimeout(self):
167
"""Test write() timeout."""
168
#use xonxoff setting and the loopback adapter to switch traffic on hold
170
self.s.writeTimeout = 1
173
self.s.write(serial.XOFF)
174
time.sleep(0.1) #some systems need a little delay so that they can react on XOFF
176
self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, "timeout please"*100)
178
self.failUnless( 1 <= (t2-t1) < 2, "Timeout not in the given intervall (%s)" % (t2-t1))
180
if __name__ == '__main__':
183
print "Testing port", PORT
184
sys.argv.append('-v')
185
# When this module is executed from the command-line, it runs all its tests