<s> import clr <EOL> import System <EOL> import System . IO . Ports <EOL> from serial . serialutil import * <EOL> def device ( portnum ) : <EOL> """<STR_LIT>""" <EOL> return System . IO . Ports . SerialPort . GetPortNames ( ) [ portnum ] <EOL> sab = System . Array [ System . Byte ] <EOL> def as_byte_array ( string ) : <EOL> return sab ( [ ord ( x ) for x in string ] ) <EOL> class IronSerial ( SerialBase ) : <EOL> """<STR_LIT>""" <EOL> BAUDRATES = ( <NUM_LIT:50> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> def open ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _port is None : <EOL> raise SerialException ( "<STR_LIT>" ) <EOL> if self . _isOpen : <EOL> raise SerialException ( "<STR_LIT>" ) <EOL> try : <EOL> self . _port_handle = System . IO . Ports . SerialPort ( self . portstr ) <EOL> except Exception , msg : <EOL> self . _port_handle = None <EOL> raise SerialException ( "<STR_LIT>" % ( self . portstr , msg ) ) <EOL> self . _reconfigurePort ( ) <EOL> self . _port_handle . Open ( ) <EOL> self . _isOpen = True <EOL> if not self . _rtscts : <EOL> self . setRTS ( True ) <EOL> self . setDTR ( True ) <EOL> self . flushInput ( ) <EOL> self . flushOutput ( ) <EOL> def _reconfigurePort ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : <EOL> raise SerialException ( "<STR_LIT>" ) <EOL> if self . _timeout is None : <EOL> self . _port_handle . ReadTimeout = System . IO . Ports . SerialPort . InfiniteTimeout <EOL> else : <EOL> self . _port_handle . ReadTimeout = int ( self . _timeout * <NUM_LIT:1000> ) <EOL> if self . _writeTimeout is None : <EOL> self . _port_handle . WriteTimeout = System . IO . Ports . SerialPort . InfiniteTimeout <EOL> else : <EOL> self . _port_handle . WriteTimeout = int ( self . _writeTimeout * <NUM_LIT:1000> ) <EOL> try : <EOL> self . _port_handle . BaudRate = self . _baudrate <EOL> except IOError , e : <EOL> raise ValueError ( str ( e ) ) <EOL> if self . _bytesize == FIVEBITS : <EOL> self . _port_handle . DataBits = <NUM_LIT:5> <EOL> elif self . _bytesize == SIXBITS : <EOL> self . _port_handle . DataBits = <NUM_LIT:6> <EOL> elif self . _bytesize == SEVENBITS : <EOL> self . _port_handle . DataBits = <NUM_LIT:7> <EOL> elif self . _bytesize == EIGHTBITS : <EOL> self . _port_handle . DataBits = <NUM_LIT:8> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % self . _bytesize ) <EOL> if self . _parity == PARITY_NONE : <EOL> self . _port_handle . Parity = getattr ( System . IO . Ports . Parity , '<STR_LIT:None>' ) <EOL> elif self . _parity == PARITY_EVEN : <EOL> self . _port_handle . Parity = System . IO . Ports . Parity . Even <EOL> elif self . _parity == PARITY_ODD : <EOL> self . _port_handle . Parity = System . IO . Ports . Parity . Odd <EOL> elif self . _parity == PARITY_MARK : <EOL> self . _port_handle . Parity = System . IO . Ports . Parity . Mark <EOL> elif self . _parity == PARITY_SPACE : <EOL> self . _port_handle . Parity = System . IO . Ports . Parity . Space <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % self . _parity ) <EOL> if self . _stopbits == STOPBITS_ONE : <EOL> self . _port_handle . StopBits = System . IO . Ports . StopBits . One <EOL> elif self . _stopbits == STOPBITS_ONE_POINT_FIVE : <EOL> self . _port_handle . StopBits = System . IO . Ports . StopBits . OnePointFive <EOL> elif self . _stopbits == STOPBITS_TWO : <EOL> self . _port_handle . StopBits = System . IO . Ports . StopBits . Two <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % self . _stopbits ) <EOL> if self . _rtscts and self . _xonxoff : <EOL> self . _port_handle . Handshake = System . IO . Ports . Handshake . RequestToSendXOnXOff <EOL> elif self . _rtscts : <EOL> self . _port_handle . Handshake = System . IO . Ports . Handshake . RequestToSend <EOL> elif self . _xonxoff : <EOL> self . _port_handle . Handshake = System . IO . Ports . Handshake . XOnXOff <EOL> else : <EOL> self . _port_handle . Handshake = getattr ( System . IO . Ports . Handshake , '<STR_LIT:None>' ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _isOpen : <EOL> if self . _port_handle : <EOL> try : <EOL> self . _port_handle . Close ( ) <EOL> except System . IO . Ports . InvalidOperationException : <EOL> pass <EOL> self . _port_handle = None <EOL> self . _isOpen = False <EOL> def makeDeviceName ( self , port ) : <EOL> try : <EOL> return device ( port ) <EOL> except TypeError , e : <EOL> raise SerialException ( str ( e ) ) <EOL> def inWaiting ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> return self . _port_handle . BytesToRead <EOL> def read ( self , size = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> data = bytearray ( ) <EOL> while size : <EOL> try : <EOL> data . append ( self . _port_handle . ReadByte ( ) ) <EOL> except System . TimeoutException , e : <EOL> break <EOL> else : <EOL> size -= <NUM_LIT:1> <EOL> return bytes ( data ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> if not isinstance ( data , ( bytes , bytearray ) ) : <EOL> raise TypeError ( '<STR_LIT>' % ( bytes , type ( data ) ) ) <EOL> try : <EOL> self . _port_handle . Write ( as_byte_array ( data ) , <NUM_LIT:0> , len ( data ) ) <EOL> except System . TimeoutException , e : <EOL> raise writeTimeoutError <EOL> return len ( data ) <EOL> def flushInput ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> self . _port_handle . DiscardInBuffer ( ) <EOL> def flushOutput ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> self . _port_handle . DiscardOutBuffer ( ) <EOL> def sendBreak ( self , duration = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> import time <EOL> self . _port_handle . BreakState = True <EOL> time . sleep ( duration ) <EOL> self . _port_handle . BreakState = False <EOL> def setBreak ( self , level = True ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> self . _port_handle . BreakState = bool ( level ) <EOL> def setRTS ( self , level = True ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> self . _port_handle . RtsEnable = bool ( level ) <EOL> def setDTR ( self , level = True ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> self . _port_handle . DtrEnable = bool ( level ) <EOL> def getCTS ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> return self . _port_handle . CtsHolding <EOL> def getDSR ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> return self . _port_handle . DsrHolding <EOL> def getRI ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> return False <EOL> def getCD ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _port_handle : raise portNotOpenError <EOL> return self . _port_handle . CDHolding <EOL> try : <EOL> import io <EOL> except ImportError : <EOL> class Serial ( IronSerial , FileLike ) : <EOL> pass <EOL> else : <EOL> class Serial ( IronSerial , io . RawIOBase ) : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> s = Serial ( <NUM_LIT:0> ) <EOL> sys . stdio . write ( '<STR_LIT>' % s ) <EOL> s = Serial ( ) <EOL> sys . stdio . write ( '<STR_LIT>' % s ) <EOL> s . baudrate = <NUM_LIT> <EOL> s . databits = <NUM_LIT:7> <EOL> s . close ( ) <EOL> s . port = <NUM_LIT:0> <EOL> s . open ( ) <EOL> sys . stdio . write ( '<STR_LIT>' % s ) </s>
