pytty

changeset 11:7f60883a6ecc

Added baud getting/setting code using termios module.
author Arc Riley <arcriley@gmail.com>
date Wed, 10 Nov 2010 13:33:36 -0500
parents eea316209c01
children 28f009bb22b7
files src/__init__.py
diffstat 1 files changed, 48 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/src/__init__.py	Wed Nov 10 02:13:22 2010 -0500
     1.2 +++ b/src/__init__.py	Wed Nov 10 13:33:36 2010 -0500
     1.3 @@ -30,6 +30,7 @@
     1.4      to tty devices.
     1.5    '''
     1.6  
     1.7 +  # This is intended to be overridden by some subclasses
     1.8    _iobase = io.FileIO
     1.9  
    1.10    def __init__ (self, name) :
    1.11 @@ -43,33 +44,66 @@
    1.12        raise IOError('%s is not a tty device' % name)
    1.13  
    1.14      # set non-blocking mode on the reader
    1.15 -    fd = reader.fileno()
    1.16 -    fcntl(fd, F_SETFL, (fcntl(fd, F_GETFL) | O_NONBLOCK))
    1.17 +    self._fdo = reader.fileno()
    1.18 +    fcntl(self._fdo, F_SETFL, (fcntl(self._fdo, F_GETFL) | O_NONBLOCK))
    1.19  
    1.20      # open a separate writer device
    1.21      writer = self._iobase(name, 'w')
    1.22  
    1.23      # set non-blocking mode on the reader
    1.24 -    fd = reader.fileno()
    1.25 -    fcntl(fd, F_SETFL, (fcntl(fd, F_GETFL) | O_NONBLOCK))
    1.26 -
    1.27 -    # default to 9600 baud for now
    1.28 -    self.__baud = 9600
    1.29 +    self._fdi = reader.fileno()
    1.30 +    fcntl(self._fdi, F_SETFL, (fcntl(self._fdi, F_GETFL) | O_NONBLOCK))
    1.31  
    1.32      # initialize self with BufferedRWPair 
    1.33      super(TTY, self).__init__(reader, writer)
    1.34  
    1.35 +
    1.36    @property
    1.37    def baud (self) :
    1.38 -    '''Baud rate can be set to any value but standard settings are 9600, 56000, 
    1.39 -     and 115200'''
    1.40 -    return self.__baud
    1.41 +    '''Baud rate must be supported by the tty device and in this list:
    1.42 +    [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600,
    1.43 +     19200, 38400, 57600, 115200, 230400, 460800]'''
    1.44 +    import termios
    1.45 +    return {
    1.46 +      termios.B0 : 0,           termios.B50 : 50,
    1.47 +      termios.B75 : 75,         termios.B110 : 110,
    1.48 +      termios.B134 : 134,       termios.B150 : 150,
    1.49 +      termios.B200 : 200,       termios.B300 : 300,
    1.50 +      termios.B600 : 600,       termios.B1200 : 1200,
    1.51 +      termios.B1800 : 1800,     termios.B2400 : 2400,
    1.52 +      termios.B4800 : 4800,     termios.B9600 : 9600,
    1.53 +      termios.B19200 : 19200,   termios.B38400 : 38400,
    1.54 +      termios.B57600 : 57600,   termios.B115200 : 115200,
    1.55 +      termios.B230400 : 230400, termios.B460800 : 460800,
    1.56 +    }[termios.tcgetattr(self._fdi)[4]]
    1.57 +
    1.58  
    1.59    @baud.setter
    1.60 -  def baud (self, rate) :
    1.61 -    # This needs to check to ensure the tty supports the given baudrate,
    1.62 -    # else raise an appropriate error
    1.63 -    self.__baud = rate
    1.64 +  def baud (self, value) :
    1.65 +    import termios
    1.66 +    tci = termios.tcgetattr(self._fdi)
    1.67 +    tco = termios.tcgetattr(self._fdo)
    1.68 +    try :
    1.69 +      tv = {
    1.70 +        0 : termios.B0,           50 : termios.B50,
    1.71 +        75 : termios.B75,         110 : termios.B110,
    1.72 +        134 : termios.B134,       150 : termios.B150,
    1.73 +        200 : termios.B200,       300 : termios.B300,
    1.74 +        600 : termios.B600,       1200 : termios.B1200,
    1.75 +        1800 : termios.B1800,     2400 : termios.B2400,
    1.76 +        4800 : termios.B4800,     9600 : termios.B9600,
    1.77 +        19200 : termios.B19200,   38400 : termios.B38400,
    1.78 +        57600 : termios.B57600,   115200 : termios.B115200,
    1.79 +        230400 : termios.B230400, 460800 : termios.B460800,
    1.80 +      }[value]
    1.81 +    except KeyError :
    1.82 +      raise IOError('Baud rate not supported by PyTTY.')
    1.83 +    if (tci[0] & tv) and (tci[1] & tv) and (tco[0] & tv) and (tco[1] & tv) :
    1.84 +      tci[4], tci[5], tco[4], tco[5] = (tv,)*4
    1.85 +      termios.tcsetattr(self._fdi, termios.TCSANOW, tci)
    1.86 +      termios.tcsetattr(self._fdo, termios.TCSANOW, tco)
    1.87 +    else :
    1.88 +      raise IOError('Baud rate not supported by TTY device.')
    1.89  
    1.90  
    1.91  # Clean up package namespace