~ampelbein/ubuntu/natty/pyserial/lp-715766

« back to all changes in this revision

Viewing changes to examples/test.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-06-24 19:14:57 UTC
  • mfrom: (3.1.4 lenny)
  • Revision ID: james.westby@ubuntu.com-20080624191457-l7snsahtf9ngtuti
Tags: 2.3-1
* New upstream version.
* Update watch file. Closes: #450106.
* Mention the upstream name in the package description. Closes: #459590.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#Python Serial Port Extension for Win32, Linux, BSD, Jython
3
 
#see __init__.py
4
 
#
5
 
#(C) 2001-2003 Chris Liechti <cliechti@gmx.net>
6
 
# this is distributed under a free software license, see license.txt
7
 
 
8
 
"""Some tests for the serial module.
9
 
Part of pyserial (http://pyserial.sf.net)  (C)2002-2003 cliechti@gmx.net
10
 
 
11
 
Intended to be run on different platforms, to ensure portability of
12
 
the code.
13
 
 
14
 
For all these tests a simple hardware is required.
15
 
Loopback HW adapter:
16
 
Shortcut these pin pairs:
17
 
 TX  <-> RX
18
 
 RTS <-> CTS
19
 
 DTR <-> DSR
20
 
 
21
 
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
22
 
"""
23
 
 
24
 
import unittest, threading, time
25
 
import serial
26
 
 
27
 
#on which port should the tests be performed:
28
 
PORT=0
29
 
 
30
 
 
31
 
class Test4_Nonblocking(unittest.TestCase):
32
 
    """Test with timeouts"""
33
 
    timeout=0
34
 
    def setUp(self):
35
 
        self.s = serial.Serial(PORT,timeout=self.timeout)
36
 
    def tearDown(self):
37
 
        self.s.close()
38
 
 
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
43
 
 
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)):
51
 
            self.s.write(c)
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."""
59
 
        self.s.write("HELLO")
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")
64
 
 
65
 
 
66
 
class Test3_Timeout(Test4_Nonblocking):
67
 
    """Same tests as the NonBlocking ones but this time with timeout"""
68
 
    timeout=1
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
73
 
 
74
 
class SendEvent(threading.Thread):
75
 
    def __init__(self, serial, delay=1):
76
 
        threading.Thread.__init__(self)
77
 
        self.serial = serial
78
 
        self.delay = delay
79
 
        self.x = threading.Event()
80
 
        self.stopped = 0
81
 
        self.start()
82
 
    def run(self):
83
 
        time.sleep(self.delay)
84
 
        if not self.stopped:
85
 
            self.serial.write("E")
86
 
        self.x.set()
87
 
    def isSet(self):
88
 
        return self.x.isSet()
89
 
    def stop(self):
90
 
        self.stopped = 1
91
 
        self.x.wait()
92
 
 
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."""
97
 
    def setUp(self):
98
 
        self.s = serial.Serial(PORT, timeout=None)
99
 
        self.event = SendEvent(self.s)
100
 
    def tearDown(self):
101
 
        self.event.stop()
102
 
        self.s.close()
103
 
 
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)."""
107
 
        c = self.s.read(1)
108
 
        if not (self.event.isSet() and c == 'E'):
109
 
            self.fail("expected marker")
110
 
 
111
 
class Test2_Forever(unittest.TestCase):
112
 
    """Tests a port with no timeout"""
113
 
    def setUp(self):
114
 
        self.s = serial.Serial(PORT,timeout=None)
115
 
    def tearDown(self):
116
 
        self.s.close()
117
 
 
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")
121
 
 
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)):
126
 
            self.s.write(c)
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")
131
 
 
132
 
 
133
 
class Test0_DataWires(unittest.TestCase):
134
 
    """Test modem control lines"""
135
 
    def setUp(self):
136
 
        self.s = serial.Serial(PORT)
137
 
    def tearDown(self):
138
 
        self.s.close()
139
 
 
140
 
    def test1_RTS(self):
141
 
        """Test RTS/CTS"""
142
 
        self.s.setRTS(0)
143
 
        self.failUnless(self.s.getCTS()==0, "CTS -> 0")
144
 
        self.s.setRTS(1)
145
 
        self.failUnless(self.s.getCTS()==1, "CTS -> 1")
146
 
 
147
 
    def test2_DTR(self):
148
 
        """Test DTR/DSR"""
149
 
        self.s.setDTR(0)
150
 
        self.failUnless(self.s.getDSR()==0, "DSR -> 0")
151
 
        self.s.setDTR(1)
152
 
        self.failUnless(self.s.getDSR()==1, "DSR -> 1")
153
 
 
154
 
    def test3_RI(self):
155
 
        """Test RI"""
156
 
        self.failUnless(self.s.getRI()==0, "RI -> 0")
157
 
 
158
 
class Test_MoreTimeouts(unittest.TestCase):
159
 
    """Test with timeouts"""
160
 
    def setUp(self):
161
 
        self.s = serial.Serial()        #create an closed serial port
162
 
    
163
 
    def tearDown(self):
164
 
        self.s.close()
165
 
 
166
 
    def test_WriteTimeout(self):
167
 
        """Test write() timeout."""
168
 
        #use xonxoff setting and the loopback adapter to switch traffic on hold
169
 
        self.s.port = PORT
170
 
        self.s.writeTimeout = 1
171
 
        self.s.xonxoff = 1
172
 
        self.s.open()
173
 
        self.s.write(serial.XOFF)
174
 
        time.sleep(0.1) #some systems need a little delay so that they can react on XOFF
175
 
        t1 = time.time()
176
 
        self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, "timeout please"*100)
177
 
        t2 = time.time()
178
 
        self.failUnless( 1 <= (t2-t1) < 2, "Timeout not in the given intervall (%s)" % (t2-t1))
179
 
 
180
 
if __name__ == '__main__':
181
 
    import sys
182
 
    print __doc__
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
186
 
    unittest.main()
 
1
#!/usr/bin/env python
 
2
#Python Serial Port Extension for Win32, Linux, BSD, Jython
 
3
#see __init__.py
 
4
#
 
5
#(C) 2001-2003 Chris Liechti <cliechti@gmx.net>
 
6
# this is distributed under a free software license, see license.txt
 
7
 
 
8
"""Some tests for the serial module.
 
9
Part of pyserial (http://pyserial.sf.net)  (C)2002-2003 cliechti@gmx.net
 
10
 
 
11
Intended to be run on different platforms, to ensure portability of
 
12
the code.
 
13
 
 
14
For all these tests a simple hardware is required.
 
15
Loopback HW adapter:
 
16
Shortcut these pin pairs:
 
17
 TX  <-> RX
 
18
 RTS <-> CTS
 
19
 DTR <-> DSR
 
20
 
 
21
On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
 
22
"""
 
23
 
 
24
import unittest, threading, time
 
25
import serial
 
26
 
 
27
#on which port should the tests be performed:
 
28
PORT=0
 
29
 
 
30
 
 
31
class Test4_Nonblocking(unittest.TestCase):
 
32
    """Test with timeouts"""
 
33
    timeout=0
 
34
    def setUp(self):
 
35
        self.s = serial.Serial(PORT,timeout=self.timeout)
 
36
    def tearDown(self):
 
37
        self.s.close()
 
38
 
 
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
 
43
 
 
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)):
 
51
            self.s.write(c)
 
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."""
 
59
        self.s.write("HELLO")
 
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")
 
64
 
 
65
 
 
66
class Test3_Timeout(Test4_Nonblocking):
 
67
    """Same tests as the NonBlocking ones but this time with timeout"""
 
68
    timeout=1
 
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
 
73
 
 
74
class SendEvent(threading.Thread):
 
75
    def __init__(self, serial, delay=1):
 
76
        threading.Thread.__init__(self)
 
77
        self.serial = serial
 
78
        self.delay = delay
 
79
        self.x = threading.Event()
 
80
        self.stopped = 0
 
81
        self.start()
 
82
    def run(self):
 
83
        time.sleep(self.delay)
 
84
        if not self.stopped:
 
85
            self.serial.write("E")
 
86
        self.x.set()
 
87
    def isSet(self):
 
88
        return self.x.isSet()
 
89
    def stop(self):
 
90
        self.stopped = 1
 
91
        self.x.wait()
 
92
 
 
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."""
 
97
    def setUp(self):
 
98
        self.s = serial.Serial(PORT, timeout=None)
 
99
        self.event = SendEvent(self.s)
 
100
    def tearDown(self):
 
101
        self.event.stop()
 
102
        self.s.close()
 
103
 
 
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)."""
 
107
        c = self.s.read(1)
 
108
        if not (self.event.isSet() and c == 'E'):
 
109
            self.fail("expected marker")
 
110
 
 
111
class Test2_Forever(unittest.TestCase):
 
112
    """Tests a port with no timeout"""
 
113
    def setUp(self):
 
114
        self.s = serial.Serial(PORT,timeout=None)
 
115
    def tearDown(self):
 
116
        self.s.close()
 
117
 
 
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")
 
121
 
 
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)):
 
126
            self.s.write(c)
 
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")
 
131
 
 
132
 
 
133
class Test0_DataWires(unittest.TestCase):
 
134
    """Test modem control lines"""
 
135
    def setUp(self):
 
136
        self.s = serial.Serial(PORT)
 
137
    def tearDown(self):
 
138
        self.s.close()
 
139
 
 
140
    def test1_RTS(self):
 
141
        """Test RTS/CTS"""
 
142
        self.s.setRTS(0)
 
143
        self.failUnless(self.s.getCTS()==0, "CTS -> 0")
 
144
        self.s.setRTS(1)
 
145
        self.failUnless(self.s.getCTS()==1, "CTS -> 1")
 
146
 
 
147
    def test2_DTR(self):
 
148
        """Test DTR/DSR"""
 
149
        self.s.setDTR(0)
 
150
        self.failUnless(self.s.getDSR()==0, "DSR -> 0")
 
151
        self.s.setDTR(1)
 
152
        self.failUnless(self.s.getDSR()==1, "DSR -> 1")
 
153
 
 
154
    def test3_RI(self):
 
155
        """Test RI"""
 
156
        self.failUnless(self.s.getRI()==0, "RI -> 0")
 
157
 
 
158
class Test_MoreTimeouts(unittest.TestCase):
 
159
    """Test with timeouts"""
 
160
    def setUp(self):
 
161
        self.s = serial.Serial()        #create an closed serial port
 
162
    
 
163
    def tearDown(self):
 
164
        self.s.close()
 
165
 
 
166
    def test_WriteTimeout(self):
 
167
        """Test write() timeout."""
 
168
        #use xonxoff setting and the loopback adapter to switch traffic on hold
 
169
        self.s.port = PORT
 
170
        self.s.writeTimeout = 1
 
171
        self.s.xonxoff = 1
 
172
        self.s.open()
 
173
        self.s.write(serial.XOFF)
 
174
        time.sleep(0.1) #some systems need a little delay so that they can react on XOFF
 
175
        t1 = time.time()
 
176
        self.failUnlessRaises(serial.SerialTimeoutException, self.s.write, "timeout please"*100)
 
177
        t2 = time.time()
 
178
        self.failUnless( 1 <= (t2-t1) < 2, "Timeout not in the given intervall (%s)" % (t2-t1))
 
179
 
 
180
if __name__ == '__main__':
 
181
    import sys
 
182
    print __doc__
 
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
 
186
    unittest.main()