pytty

changeset 2:bca4822e1783

Adding basic package, TTY class
author Arc Riley <arcriley@gmail.com>
date Fri, 05 Nov 2010 01:57:13 -0400
parents 97db84e166ea
children 299b8d005ff5
files src/__init__.py
diffstat 1 files changed, 62 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/__init__.py	Fri Nov 05 01:57:13 2010 -0400
     1.3 @@ -0,0 +1,62 @@
     1.4 +# -*- coding: utf-8 -*-
     1.5 +
     1.6 +''' PyTTY - Python serial access package '''
     1.7 +
     1.8 +__credits__ = '''Copyright (C) 2010 Arc Riley
     1.9 +
    1.10 +  This program is free software; you can redistribute it and/or modify
    1.11 +  it under the terms of the GNU Lesser General Public License as published
    1.12 +  by the Free Software Foundation, either version 3 of the License, or
    1.13 +  (at your option) any later version.
    1.14 +
    1.15 +  This program is distributed in the hope that it will be useful,
    1.16 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 +  GNU Affero General Public License for more details.
    1.19 +
    1.20 +  You should have received a copy of the GNU Lesser General Public License
    1.21 +  along with this program; if not, see http://www.gnu.org/licenses
    1.22 +'''
    1.23 +__author__  = 'Arc Riley <arcriley@gmail.com>'
    1.24 +
    1.25 +import io
    1.26 +
    1.27 +class TTY (io.BufferedRWPair) :
    1.28 +  ''' TTY io class
    1.29 +
    1.30 +    This is a subclass of io.BufferedRWPair from the Python standard library 
    1.31 +    which opens a tty device, sets nonblock mode on the device, and allows the
    1.32 +    user to change baud rate, flow control, and other settings often available 
    1.33 +    to tty devices.
    1.34 +  '''
    1.35 +
    1.36 +  iobase = io.FileIO
    1.37 +
    1.38 +  def __init__ (self, name) :
    1.39 +    from fcntl import fcntl, F_SETFL, F_GETFL
    1.40 +    from os import O_NONBLOCK
    1.41 +
    1.42 +    reader = self.iobase(name, 'r')
    1.43 +
    1.44 +    # ensure this is actually a tty device
    1.45 +    if not reader.isatty() :
    1.46 +      raise IOError('%s is not a tty device' % name)
    1.47 +
    1.48 +    # set non-blocking mode on the reader
    1.49 +    fd = reader.fileno()
    1.50 +    fcntl(fd, F_SETFL, (fcntl(fd, F_GETFL) | O_NONBLOCK))
    1.51 +
    1.52 +    # open a separate writer device
    1.53 +    writer = self.iobase(name, 'w')
    1.54 +
    1.55 +    # set non-blocking mode on the reader
    1.56 +    fd = reader.fileno()
    1.57 +    fcntl(fd, F_SETFL, (fcntl(fd, F_GETFL) | O_NONBLOCK))
    1.58 +
    1.59 +    # initialize self with BufferedRWPair 
    1.60 +    super(TTY, self).__init__(reader, writer)
    1.61 +
    1.62 +
    1.63 +# Clean up package namespace
    1.64 +del(io)
    1.65 +