<s> import os <EOL> import fcntl <EOL> import array <EOL> import termios <EOL> import select <EOL> class SerialError ( IOError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Serial ( object ) : <EOL> _DATABITS_TO_CFLAG = { <EOL> <NUM_LIT:5> : termios . CS5 , <NUM_LIT:6> : termios . CS6 , <NUM_LIT:7> : termios . CS7 , <NUM_LIT:8> : termios . CS8 <EOL> } <EOL> _CFLAG_TO_DATABITS = { v : k for k , v in _DATABITS_TO_CFLAG . items ( ) } <EOL> _BAUDRATE_TO_OSPEED = { <EOL> <NUM_LIT:50> : termios . B50 , <NUM_LIT> : termios . B75 , <NUM_LIT> : termios . B110 , <NUM_LIT> : termios . B134 , <EOL> <NUM_LIT> : termios . B150 , <NUM_LIT:200> : termios . B200 , <NUM_LIT> : termios . B300 , <EOL> <NUM_LIT> : termios . B600 , <NUM_LIT> : termios . B1200 , <NUM_LIT> : termios . B1800 , <EOL> <NUM_LIT> : termios . B2400 , <NUM_LIT> : termios . B4800 , <NUM_LIT> : termios . B9600 , <EOL> <NUM_LIT> : termios . B19200 , <NUM_LIT> : termios . B38400 , <NUM_LIT> : termios . B57600 , <EOL> <NUM_LIT> : termios . B115200 , <NUM_LIT> : termios . B230400 , <EOL> <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <EOL> <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <EOL> <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <EOL> <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> , <EOL> } <EOL> _OSPEED_TO_BAUDRATE = { v : k for k , v in _BAUDRATE_TO_OSPEED . items ( ) } <EOL> def __init__ ( self , devpath , baudrate , databits = <NUM_LIT:8> , parity = "<STR_LIT:none>" , stopbits = <NUM_LIT:1> , xonxoff = False , rtscts = False ) : <EOL> """<STR_LIT>""" <EOL> self . _fd = None <EOL> self . _devpath = None <EOL> self . _open ( devpath , baudrate , databits , parity , stopbits , xonxoff , rtscts ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , t , value , traceback ) : <EOL> self . close ( ) <EOL> def _open ( self , devpath , baudrate , databits , parity , stopbits , xonxoff , rtscts ) : <EOL> if not isinstance ( devpath , str ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif not isinstance ( baudrate , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif not isinstance ( databits , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif not isinstance ( parity , str ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif not isinstance ( stopbits , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif not isinstance ( xonxoff , bool ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif not isinstance ( rtscts , bool ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if baudrate not in Serial . _BAUDRATE_TO_OSPEED : <EOL> raise ValueError ( "<STR_LIT>" % baudrate ) <EOL> elif databits not in [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif parity . lower ( ) not in [ "<STR_LIT:none>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif stopbits not in [ <NUM_LIT:1> , <NUM_LIT:2> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> self . _fd = os . open ( devpath , os . O_RDWR | os . O_NOCTTY ) <EOL> except OSError as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> self . _devpath = devpath <EOL> parity = parity . lower ( ) <EOL> ( iflag , oflag , cflag , lflag , ispeed , ospeed , cc ) = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , [ <NUM_LIT:0> ] * <NUM_LIT:32> ) <EOL> iflag = termios . IGNBRK <EOL> if parity != "<STR_LIT:none>" : <EOL> iflag |= ( termios . INPCK | termios . ISTRIP ) <EOL> if xonxoff : <EOL> iflag |= ( termios . IXON | termios . IXOFF ) <EOL> oflag = <NUM_LIT:0> <EOL> lflag = <NUM_LIT:0> <EOL> cflag = ( termios . CREAD | termios . CLOCAL ) <EOL> cflag |= Serial . _DATABITS_TO_CFLAG [ databits ] <EOL> if parity == "<STR_LIT>" : <EOL> cflag |= termios . PARENB <EOL> elif parity == "<STR_LIT>" : <EOL> cflag |= ( termios . PARENB | termios . PARODD ) <EOL> if stopbits == <NUM_LIT:2> : <EOL> cflag |= termios . CSTOPB <EOL> if rtscts : <EOL> cflag |= termios . CRTSCTS <EOL> cflag |= Serial . _BAUDRATE_TO_OSPEED [ baudrate ] <EOL> ispeed = Serial . _BAUDRATE_TO_OSPEED [ baudrate ] <EOL> ospeed = Serial . _BAUDRATE_TO_OSPEED [ baudrate ] <EOL> try : <EOL> termios . tcsetattr ( self . _fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> def read ( self , length , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> data = b"<STR_LIT>" <EOL> while True : <EOL> if timeout is not None : <EOL> ( rlist , _ , _ ) = select . select ( [ self . _fd ] , [ ] , [ ] , timeout ) <EOL> if self . _fd not in rlist : <EOL> break <EOL> try : <EOL> data += os . read ( self . _fd , length - len ( data ) ) <EOL> except OSError as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> if len ( data ) == length : <EOL> break <EOL> return data <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( data , bytes ) and not isinstance ( data , bytearray ) and not isinstance ( data , list ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if isinstance ( data , list ) : <EOL> data = bytearray ( data ) <EOL> try : <EOL> return os . write ( self . _fd , data ) <EOL> except OSError as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> def poll ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> p = select . poll ( ) <EOL> p . register ( self . _fd , select . POLLIN | select . POLLPRI ) <EOL> events = p . poll ( int ( timeout * <NUM_LIT:1000> ) ) <EOL> if len ( events ) > <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> termios . tcdrain ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> def input_waiting ( self ) : <EOL> """<STR_LIT>""" <EOL> buf = array . array ( '<STR_LIT:I>' , [ <NUM_LIT:0> ] ) <EOL> try : <EOL> fcntl . ioctl ( self . _fd , termios . TIOCINQ , buf , True ) <EOL> except OSError as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> return buf [ <NUM_LIT:0> ] <EOL> def output_waiting ( self ) : <EOL> """<STR_LIT>""" <EOL> buf = array . array ( '<STR_LIT:I>' , [ <NUM_LIT:0> ] ) <EOL> try : <EOL> fcntl . ioctl ( self . _fd , termios . TIOCOUTQ , buf , True ) <EOL> except OSError as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> return buf [ <NUM_LIT:0> ] <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _fd is None : <EOL> return <EOL> try : <EOL> os . close ( self . _fd ) <EOL> except OSError as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> self . _fd = None <EOL> @ property <EOL> def fd ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _fd <EOL> @ property <EOL> def devpath ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _devpath <EOL> def _get_baudrate ( self ) : <EOL> try : <EOL> ( _ , _ , _ , _ , _ , ospeed , _ ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> if ospeed not in Serial . _OSPEED_TO_BAUDRATE : <EOL> raise SerialError ( None , "<STR_LIT>" % ospeed ) <EOL> return Serial . _OSPEED_TO_BAUDRATE [ ospeed ] <EOL> def _set_baudrate ( self , baudrate ) : <EOL> if not isinstance ( baudrate , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if baudrate not in Serial . _BAUDRATE_TO_OSPEED : <EOL> raise ValueError ( "<STR_LIT>" % baudrate ) <EOL> try : <EOL> ( iflag , oflag , cflag , lflag , ispeed , ospeed , cc ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> cflag &= ~ ( termios . CBAUD | termios . CBAUDEX ) <EOL> cflag |= Serial . _BAUDRATE_TO_OSPEED [ baudrate ] <EOL> ispeed = Serial . _BAUDRATE_TO_OSPEED [ baudrate ] <EOL> ospeed = Serial . _BAUDRATE_TO_OSPEED [ baudrate ] <EOL> try : <EOL> termios . tcsetattr ( self . _fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> baudrate = property ( _get_baudrate , _set_baudrate ) <EOL> """<STR_LIT>""" <EOL> def _get_databits ( self ) : <EOL> try : <EOL> ( _ , _ , cflag , _ , _ , _ , _ ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> cs = cflag & termios . CSIZE <EOL> if cs not in Serial . _CFLAG_TO_DATABITS : <EOL> raise SerialError ( None , "<STR_LIT>" % cs ) <EOL> return Serial . _CFLAG_TO_DATABITS [ cs ] <EOL> def _set_databits ( self , databits ) : <EOL> if not isinstance ( databits , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif databits not in [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> ( iflag , oflag , cflag , lflag , ispeed , ospeed , cc ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> cflag &= ~ termios . CSIZE <EOL> cflag |= Serial . _DATABITS_TO_CFLAG [ databits ] <EOL> try : <EOL> termios . tcsetattr ( self . _fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> databits = property ( _get_databits , _set_databits ) <EOL> """<STR_LIT>""" <EOL> def _get_parity ( self ) : <EOL> try : <EOL> ( _ , _ , cflag , _ , _ , _ , _ ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> if ( cflag & termios . PARENB ) == <NUM_LIT:0> : <EOL> return "<STR_LIT:none>" <EOL> elif ( cflag & termios . PARODD ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def _set_parity ( self , parity ) : <EOL> if not isinstance ( parity , str ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif parity . lower ( ) not in [ "<STR_LIT:none>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> parity = parity . lower ( ) <EOL> try : <EOL> ( iflag , oflag , cflag , lflag , ispeed , ospeed , cc ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> iflag &= ~ ( termios . INPCK | termios . ISTRIP ) <EOL> cflag &= ~ ( termios . PARENB | termios . PARODD ) <EOL> if parity != "<STR_LIT:none>" : <EOL> iflag |= ( termios . INPCK | termios . ISTRIP ) <EOL> cflag |= termios . PARENB <EOL> if parity == "<STR_LIT>" : <EOL> cflag |= termios . PARODD <EOL> try : <EOL> termios . tcsetattr ( self . _fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> parity = property ( _get_parity , _set_parity ) <EOL> """<STR_LIT>""" <EOL> def _get_stopbits ( self ) : <EOL> try : <EOL> ( _ , _ , cflag , _ , _ , _ , _ ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> if ( cflag & termios . CSTOPB ) != <NUM_LIT:0> : <EOL> return <NUM_LIT:2> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def _set_stopbits ( self , stopbits ) : <EOL> if not isinstance ( stopbits , int ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif stopbits not in [ <NUM_LIT:1> , <NUM_LIT:2> ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> ( iflag , oflag , cflag , lflag , ispeed , ospeed , cc ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> cflag &= ~ termios . CSTOPB <EOL> if stopbits == <NUM_LIT:2> : <EOL> cflag |= termios . CSTOPB <EOL> try : <EOL> termios . tcsetattr ( self . _fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> stopbits = property ( _get_stopbits , _set_stopbits ) <EOL> """<STR_LIT>""" <EOL> def _get_xonxoff ( self ) : <EOL> try : <EOL> ( iflag , _ , _ , _ , _ , _ , _ ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> if ( iflag & ( termios . IXON | termios . IXOFF ) ) != <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> return False <EOL> def _set_xonxoff ( self , enabled ) : <EOL> if not isinstance ( enabled , bool ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> try : <EOL> ( iflag , oflag , cflag , lflag , ispeed , ospeed , cc ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> iflag &= ~ ( termios . IXON | termios . IXOFF | termios . IXANY ) <EOL> if enabled : <EOL> iflag |= ( termios . IXON | termios . IXOFF ) <EOL> try : <EOL> termios . tcsetattr ( self . _fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> xonxoff = property ( _get_xonxoff , _set_xonxoff ) <EOL> """<STR_LIT>""" <EOL> def _get_rtscts ( self ) : <EOL> try : <EOL> ( _ , _ , cflag , _ , _ , _ , _ ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> if ( cflag & termios . CRTSCTS ) != <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> return False <EOL> def _set_rtscts ( self , enabled ) : <EOL> if not isinstance ( enabled , bool ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> try : <EOL> ( iflag , oflag , cflag , lflag , ispeed , ospeed , cc ) = termios . tcgetattr ( self . _fd ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> cflag = ~ termios . CRTSCTS <EOL> if enabled : <EOL> cflag |= termios . CRTSCTS <EOL> try : <EOL> termios . tcsetattr ( self . _fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) <EOL> except termios . error as e : <EOL> raise SerialError ( e . errno , "<STR_LIT>" + e . strerror ) <EOL> rtscts = property ( _get_rtscts , _set_rtscts ) <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . devpath , self . fd , self . baudrate , self . databits , self . parity , self . stopbits , str ( self . xonxoff ) , str ( self . rtscts ) ) </s>
