from threading import Lock
LPC_CMDS=dict(IDLE = 0x00,
SET_TRACE_VARIABLE = 0x04,
GET_TRACE_VARIABLES = 0x05,
SET_FORCED_VARIABLE = 0x06,
LPC_STATUS=dict(STARTED = 0x01,
class LPCError(exceptions.Exception):
return "LPC communication error ! " + str(self.msg)
def __init__(self, port, rate, timeout):
self.serialPort = serial.Serial( port, rate, timeout = timeout )
self.HandleTransaction(LPCTransaction("IDLE"))
self.TransactionLock = Lock()
def HandleTransaction(self, transaction):
self.TransactionLock.acquire()
transaction.SetPseudoFile(self.serialPort)
# send command, wait ack (timeout)
transaction.SendCommand()
current_plc_status = transaction.GetCommandAck()
if current_plc_status is not None:
res = transaction.ExchangeData()
raise LPCError("LPC transaction error - controller did not answer as expected")
self.TransactionLock.release()
return current_plc_status, res
def __init__(self, command, optdata):
self.Command = LPC_CMDS[command]
self.OptData = optdata[:]
def SetPseudoFile(pseudofile):
self.pseudofile = pseudofile
self.pseudofile.write(chr(self.Command))
comm_status, current_plc_status = map(ord, self.pseudofile.read(2))
# LPC returns command itself as an ack for command
if(comm_status == self.Command):
return current_plc_status
if self.Command & WAIT_DATA :
length = len(self.OptData)
# transform length into a byte string
# we presuppose endianess of LPC same as PC
lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4)
self.pseudofile.write(lengthstr + self.OptData)
lengthstr = self.pseudofile.read(4)
# transform a byte string into length
length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value
return self.pseudofile.read(length)
if __name__ == "__main__":
TestConnection = LPCProto()