blob: 61381f41ac8408a96ba149660df14e73fd1d78c1 [file] [log] [blame]
cliechtif7bc3bf2010-07-21 15:47:21 +00001#! /usr/bin/env python
cliechtif7bc3bf2010-07-21 15:47:21 +00002#
Chris Liechti7c032f12015-10-27 23:02:00 +01003# This file is part of pySerial - Cross platform serial port support for Python
4# (C) 2010-2015 Chris Liechti <cliechti@gmx.net>
5#
6# SPDX-License-Identifier: BSD-3-Clause
cliechtif7bc3bf2010-07-21 15:47:21 +00007"""\
8Some tests for the serial module.
9Part of pyserial (http://pyserial.sf.net) (C)2010 cliechti@gmx.net
10
11Intended to be run on different platforms, to ensure portability of
12the code.
13
14For all these tests a simple hardware is required.
15Loopback HW adapter:
16Shortcut these pin pairs:
17 TX <-> RX
18 RTS <-> CTS
19 DTR <-> DSR
20
21On a 9 pole DSUB these are the pins (2-3) (4-6) (7-8)
22"""
23
24import unittest
25import threading
26import time
27import sys
28import serial
29
30#~ print serial.VERSION
31
32# on which port should the tests be performed:
33PORT = 0
34
35if sys.version_info >= (3, 0):
36 def data(string):
37 return bytes(string, 'latin1')
38else:
39 def data(string): return string
40
41
42
43class Test_Readline(unittest.TestCase):
44 """Test readline function"""
45
46 def setUp(self):
47 self.s = serial.serial_for_url(PORT, timeout=1)
48
49 def tearDown(self):
50 self.s.close()
51
52 def test_readline(self):
53 """Test readline method"""
cliechtie30868d2013-10-16 15:35:11 +000054 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
55 self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x31, 0x0a]))
56 self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x32, 0x0a]))
57 self.failUnlessEqual(self.s.readline(), serial.to_bytes([0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000058 # this time we will get a timeout
cliechtie30868d2013-10-16 15:35:11 +000059 self.failUnlessEqual(self.s.readline(), serial.to_bytes([]))
cliechtif7bc3bf2010-07-21 15:47:21 +000060
61 def test_readlines(self):
62 """Test readlines method"""
cliechtie30868d2013-10-16 15:35:11 +000063 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000064 self.failUnlessEqual(
65 self.s.readlines(),
cliechtie30868d2013-10-16 15:35:11 +000066 [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
cliechtif7bc3bf2010-07-21 15:47:21 +000067 )
68
69 def test_xreadlines(self):
70 """Test xreadlines method (skipped for io based systems)"""
71 if hasattr(self.s, 'xreadlines'):
cliechtie30868d2013-10-16 15:35:11 +000072 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000073 self.failUnlessEqual(
74 list(self.s.xreadlines()),
cliechtie30868d2013-10-16 15:35:11 +000075 [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
cliechtif7bc3bf2010-07-21 15:47:21 +000076 )
77
78 def test_for_in(self):
79 """Test for line in s"""
cliechtie30868d2013-10-16 15:35:11 +000080 self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
cliechtif7bc3bf2010-07-21 15:47:21 +000081 lines = []
82 for line in self.s:
83 lines.append(line)
84 self.failUnlessEqual(
85 lines,
cliechtie30868d2013-10-16 15:35:11 +000086 [serial.to_bytes([0x31, 0x0a]), serial.to_bytes([0x32, 0x0a]), serial.to_bytes([0x33, 0x0a])]
cliechtif7bc3bf2010-07-21 15:47:21 +000087 )
88
89 def test_alternate_eol(self):
90 """Test readline with alternative eol settings (skipped for io based systems)"""
91 if hasattr(self.s, 'xreadlines'): # test if it is our FileLike base class
92 self.s.write(serial.to_bytes("no\rno\nyes\r\n"))
93 self.failUnlessEqual(
94 self.s.readline(eol=serial.to_bytes("\r\n")),
95 serial.to_bytes("no\rno\nyes\r\n"))
96
97
98if __name__ == '__main__':
99 import sys
100 sys.stdout.write(__doc__)
101 if len(sys.argv) > 1:
102 PORT = sys.argv[1]
103 sys.stdout.write("Testing port: %r\n" % PORT)
104 sys.argv[1:] = ['-v']
105 # When this module is executed from the command-line, it runs all its tests
106 unittest.main()