Apache/2.4.7 (Ubuntu) Linux sman1baleendah 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) safemode : OFF MySQL: ON | Perl: ON | cURL: OFF | WGet: ON > / usr / share / doc / python-serial / examples / | server ip : 172.67.156.115 your ip : 172.69.6.191 H O M E |
Filename | /usr/share/doc/python-serial/examples/enhancedserial.py |
Size | 2.12 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 27-Apr-2025 09:56 |
Last modified | 20-Jun-2008 03:29 |
Last accessed | 07-Jul-2025 02:03 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
#!/usr/bin/env python
"""Enhanced Serial Port class
part of pyserial (http://pyserial.sf.net) (C)2002 [email protected]
another implementation of the readline and readlines method.
this one should be more efficient because a bunch of characters are read
on each access, but the drawback is that a timeout must be specified to
make it work (enforced by the class __init__).
this class could be enhanced with a read_until() method and more
like found in the telnetlib.
"""
from serial import Serial
class EnhancedSerial(Serial):
def __init__(self, *args, **kwargs):
#ensure that a reasonable timeout is set
timeout = kwargs.get('timeout',0.1)
if timeout < 0.01: timeout = 0.1
kwargs['timeout'] = timeout
Serial.__init__(self, *args, **kwargs)
self.buf = ''
def readline(self, maxsize=None, timeout=1):
"""maxsize is ignored, timeout in seconds is the max time that is way for a complete line"""
tries = 0
while 1:
self.buf += self.read(512)
pos = self.buf.find('\n')
if pos >= 0:
line, self.buf = self.buf[:pos+1], self.buf[pos+1:]
return line
tries += 1
if tries * self.timeout > timeout:
break
line, self.buf = self.buf, ''
return line
def readlines(self, sizehint=None, timeout=1):
"""read all lines that are available. abort after timout
when no more data arrives."""
lines = []
while 1:
line = self.readline(timeout=timeout)
if line:
lines.append(line)
if not line or line[-1:] != '\n':
break
return lines
if __name__=='__main__':
#do some simple tests with a Loopback HW (see test.py for details)
PORT = 0
#test, only with Loopback HW (shortcut RX/TX pins (3+4 on DSUB 9 and 25) )
s = EnhancedSerial(PORT)
#write out some test data lines
s.write('\n'.join("hello how are you".split()))
#and read them back
print s.readlines()
#this one should print an empty list
print s.readlines(timeout=0.4)
"""Enhanced Serial Port class
part of pyserial (http://pyserial.sf.net) (C)2002 [email protected]
another implementation of the readline and readlines method.
this one should be more efficient because a bunch of characters are read
on each access, but the drawback is that a timeout must be specified to
make it work (enforced by the class __init__).
this class could be enhanced with a read_until() method and more
like found in the telnetlib.
"""
from serial import Serial
class EnhancedSerial(Serial):
def __init__(self, *args, **kwargs):
#ensure that a reasonable timeout is set
timeout = kwargs.get('timeout',0.1)
if timeout < 0.01: timeout = 0.1
kwargs['timeout'] = timeout
Serial.__init__(self, *args, **kwargs)
self.buf = ''
def readline(self, maxsize=None, timeout=1):
"""maxsize is ignored, timeout in seconds is the max time that is way for a complete line"""
tries = 0
while 1:
self.buf += self.read(512)
pos = self.buf.find('\n')
if pos >= 0:
line, self.buf = self.buf[:pos+1], self.buf[pos+1:]
return line
tries += 1
if tries * self.timeout > timeout:
break
line, self.buf = self.buf, ''
return line
def readlines(self, sizehint=None, timeout=1):
"""read all lines that are available. abort after timout
when no more data arrives."""
lines = []
while 1:
line = self.readline(timeout=timeout)
if line:
lines.append(line)
if not line or line[-1:] != '\n':
break
return lines
if __name__=='__main__':
#do some simple tests with a Loopback HW (see test.py for details)
PORT = 0
#test, only with Loopback HW (shortcut RX/TX pins (3+4 on DSUB 9 and 25) )
s = EnhancedSerial(PORT)
#write out some test data lines
s.write('\n'.join("hello how are you".split()))
#and read them back
print s.readlines()
#this one should print an empty list
print s.readlines(timeout=0.4)