Source code for decocare.link
#
# TODO: move all constants to config module.
#
import serial
import logging
import lib
io = logging.getLogger( )
log = io.getChild(__name__)
class Link( object ):
[docs] __timeout__ = .500
port = None
def __init__( self, port, timeout=None ):
if timeout is not None:
self.__timeout__ = timeout
self.open( port )
def open( self, newPort=False, **kwds ):
[docs] if newPort:
self.port = newPort
if 'timeout' not in kwds:
kwds['timeout'] = self.__timeout__
self.serial = serial.Serial( self.port, **kwds )
if self.serial.isOpen( ):
log.info( '{agent} opened serial port: {serial}'\
.format( serial = repr( self.serial ),
agent =self.__class__.__name__ ) )
def close( self ):
[docs] io.info( 'closing serial port' )
return self.serial.close( )
def write( self, string ):
[docs] r = self.serial.write( string )
io.info( 'usb.write.len: %s\n%s' % ( len( string ),
lib.hexdump( bytearray( string ) ) ) )
return r
def read( self, c ):
[docs] r = self.serial.read( c )
io.info( 'usb.read.len: %s' % ( len( r ) ) )
io.info( 'usb.read.raw:\n%s' % ( lib.hexdump( bytearray( r ) ) ) )
return r
def readline( self ):
[docs] r = self.serial.readline( )
io.info( 'usb.read.len: %s\n%s' % ( len( r ),
lib.hexdump( bytearray( r ) ) ) )
return r
def readlines( self ):
[docs] r = self.serial.readlines( )
io.info( 'usb.read.len: %s\n%s' % ( len( r ),
lib.hexdump( bytearray( ''.join( r ) ) ) ) )
return r
if __name__ == '__main__':
import doctest
doctest.testmost( )
#####
# EOF