summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-07-21 20:56:52 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2009-07-21 20:56:52 +0000
commit58a3efe7e74f5f9ece196d0dcc5636bf55779037 (patch)
treeee562e33254dbbf0d66d21a11403e1527cadefd8
parent54f69b6418ad30ac4c281a180628d3f19b98d878 (diff)
downloadpyserial-git-58a3efe7e74f5f9ece196d0dcc5636bf55779037.tar.gz
add delay, in case of USB serial adapters in may be needed to for the successful test
-rw-r--r--pyserial/examples/test.py44
1 files changed, 29 insertions, 15 deletions
diff --git a/pyserial/examples/test.py b/pyserial/examples/test.py
index 26d5192..e3002c3 100644
--- a/pyserial/examples/test.py
+++ b/pyserial/examples/test.py
@@ -25,7 +25,7 @@ On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
import unittest, threading, time
import serial
-#on which port should the tests be performed:
+# on which port should the tests be performed:
PORT=0
@@ -34,32 +34,35 @@ class Test4_Nonblocking(unittest.TestCase):
timeout=0
def setUp(self):
self.s = serial.Serial(PORT,timeout=self.timeout)
+
def tearDown(self):
self.s.close()
def test0_Messy(self):
"""NonBlocking (timeout=0)"""
- #this is only here to write out the message in verbose mode
- #because Test3 and Test4 print the same messages
+ # this is only here to write out the message in verbose mode
+ # because Test3 and Test4 print the same messages
def test1_ReadEmpty(self):
"""timeout: After port open, the input buffer must be empty"""
self.failUnless(self.s.read(1)=='', "expected empty buffer")
+
def test2_Loopback(self):
"""timeout: each sent character should return (binary test).
this is also a test for the binary capability of a port."""
for c in map(chr,range(256)):
self.s.write(c)
- time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
+ time.sleep(0.02) # there might be a small delay until the character is ready (especially on win32)
self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
self.failUnless(self.s.read(1)==c, "expected a '%s' which was written before" % c)
self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
+
def test2_LoopbackTimeout(self):
"""timeout: test the timeout/immediate return.
partial results should be returned."""
self.s.write("HELLO")
- time.sleep(0.1) #there might be a small delay until the character is ready (especialy on win32)
- #read more characters as are available to run in the timeout
+ time.sleep(0.1) # there might be a small delay until the character is ready (especially on win32)
+ # read more characters as are available to run in the timeout
self.failUnless(self.s.read(10)=='HELLO', "expected the 'HELLO' which was written before")
self.failUnless(self.s.read(1)=='', "expected empty buffer after all sent chars are read")
@@ -67,10 +70,11 @@ class Test4_Nonblocking(unittest.TestCase):
class Test3_Timeout(Test4_Nonblocking):
"""Same tests as the NonBlocking ones but this time with timeout"""
timeout=1
+
def test0_Messy(self):
"""Blocking (timeout=1)"""
- #this is only here to write out the message in verbose mode
- #because Test3 and Test4 print the same messages
+ # this is only here to write out the message in verbose mode
+ # because Test3 and Test4 print the same messages
class SendEvent(threading.Thread):
def __init__(self, serial, delay=1):
@@ -80,13 +84,16 @@ class SendEvent(threading.Thread):
self.x = threading.Event()
self.stopped = 0
self.start()
+
def run(self):
time.sleep(self.delay)
if not self.stopped:
self.serial.write("E")
self.x.set()
+
def isSet(self):
return self.x.isSet()
+
def stop(self):
self.stopped = 1
self.x.wait()
@@ -98,6 +105,7 @@ class Test1_Forever(unittest.TestCase):
def setUp(self):
self.s = serial.Serial(PORT, timeout=None)
self.event = SendEvent(self.s)
+
def tearDown(self):
self.event.stop()
self.s.close()
@@ -113,6 +121,7 @@ class Test2_Forever(unittest.TestCase):
"""Tests a port with no timeout"""
def setUp(self):
self.s = serial.Serial(PORT,timeout=None)
+
def tearDown(self):
self.s.close()
@@ -125,7 +134,7 @@ class Test2_Forever(unittest.TestCase):
this is also a test for the binary capability of a port."""
for c in map(chr,range(256)):
self.s.write(c)
- time.sleep(0.02) #there might be a small delay until the character is ready (especialy on win32)
+ time.sleep(0.02) # there might be a small delay until the character is ready (especially on win32)
self.failUnless(self.s.inWaiting()==1, "expected exactly one character for inWainting()")
self.failUnless(self.s.read(1)==c, "expected an '%s' which was written before" % c)
self.failUnless(self.s.inWaiting()==0, "expected empty buffer after all sent chars are read")
@@ -135,21 +144,26 @@ class Test0_DataWires(unittest.TestCase):
"""Test modem control lines"""
def setUp(self):
self.s = serial.Serial(PORT)
+
def tearDown(self):
self.s.close()
def test1_RTS(self):
"""Test RTS/CTS"""
self.s.setRTS(0)
+ time.sleep(0.1)
self.failUnless(not self.s.getCTS(), "CTS -> 0")
self.s.setRTS(1)
+ time.sleep(0.1)
self.failUnless(self.s.getCTS(), "CTS -> 1")
def test2_DTR(self):
"""Test DTR/DSR"""
self.s.setDTR(0)
+ time.sleep(0.1)
self.failUnless(not self.s.getDSR(), "DSR -> 0")
self.s.setDTR(1)
+ time.sleep(0.1)
self.failUnless(self.s.getDSR(), "DSR -> 1")
def test3_RI(self):
@@ -159,31 +173,31 @@ class Test0_DataWires(unittest.TestCase):
class Test_MoreTimeouts(unittest.TestCase):
"""Test with timeouts"""
def setUp(self):
- self.s = serial.Serial() #create an closed serial port
-
+ self.s = serial.Serial() # create an closed serial port
+
def tearDown(self):
self.s.close()
def test_WriteTimeout(self):
"""Test write() timeout."""
- #use xonxoff setting and the loopback adapter to switch traffic on hold
+ # use xonxoff setting and the loop-back adapter to switch traffic on hold
self.s.port = PORT
self.s.writeTimeout = 1
self.s.xonxoff = 1
self.s.open()
self.s.write(serial.XOFF)
- time.sleep(0.1) #some systems need a little delay so that they can react on XOFF
+ time.sleep(0.5) # some systems need a little delay so that they can react on XOFF
t1 = time.time()
self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, "timeout please"*100)
t2 = time.time()
- self.failUnless( 1 <= (t2-t1) < 2, "Timeout not in the given intervall (%s)" % (t2-t1))
+ self.failUnless( 1 <= (t2-t1) < 2, "Timeout not in the given interval (%s)" % (t2-t1))
if __name__ == '__main__':
import sys
sys.stdout.write(__doc__)
if len(sys.argv) > 1:
PORT = sys.argv[1]
- sys.stdout.write("Testing port: %r" % PORT)
+ sys.stdout.write("Testing port: %r\n" % PORT)
sys.argv[1:] = ['-v']
# When this module is executed from the command-line, it runs all its tests
unittest.main()