ed@448: import serial edouard@453: import exceptions ed@448: from threading import Lock edouard@571: import time ed@448: Lolitech@545: class LPCProtoError(exceptions.Exception): ed@448: """Exception class""" ed@448: def __init__(self, msg): ed@448: self.msg = msg ed@448: ed@448: def __str__(self): ed@448: return "LPC communication error ! " + str(self.msg) ed@448: ed@448: class LPCProto: ed@448: def __init__(self, port, rate, timeout): edouard@453: # serialize access lock edouard@453: self.TransactionLock = Lock() edouard@571: if BMZ_DBG: edouard@571: # Debugging serial stuff edouard@571: self._serialPort = serial.Serial( port, rate, timeout = timeout, writeTimeout = timeout ) edouard@571: class myser: edouard@571: def readline(self_): edouard@571: res = self._serialPort.readline() edouard@571: print 'Recv :"', res, '"' edouard@571: return res edouard@571: edouard@571: def read(self_,cnt): edouard@571: res = self._serialPort.read(cnt) edouard@571: if len(res) > 16: edouard@571: print "Recv :", map(hex,map(ord,res[:16])), "[...]" edouard@571: else: edouard@571: print "Recv :", map(hex,map(ord,res)) edouard@571: edouard@571: return res edouard@571: def write(self_, string): edouard@571: lstr=len(string) edouard@571: if lstr > 16: edouard@571: print "Send :", map(hex,map(ord,string[:16])), "[...]" edouard@571: else: edouard@571: print "Send :", map(hex,map(ord,string)) edouard@571: return self._serialPort.write(string) edouard@571: # while len(string)>0: edouard@571: # i = self._serialPort.write(string[:4096]) edouard@571: # print ".", edouard@571: # string = string[i:] edouard@571: # print edouard@571: #return lstr edouard@571: def flush(self_): edouard@571: return self._serialPort.flush() edouard@571: def close(self_): edouard@571: self._serialPort.close() edouard@571: self.serialPort = myser() edouard@571: else: edouard@571: # open serial port edouard@571: self.serialPort = serial.Serial( port, rate, timeout = timeout ) edouard@571: # start with empty buffer ed@448: self.serialPort.flush() ed@448: Lolitech@563: def __del__(self): Lolitech@563: if self.serialPort: Lolitech@563: self.serialPort.close() Lolitech@563: Lolitech@563: def close(self): Lolitech@563: self.serialPort.close() Lolitech@563: self.serialPort = None