--- a/connectors/LPC/LPCProto.py Fri Dec 04 15:15:57 2009 +0100
+++ b/connectors/LPC/LPCProto.py Fri Dec 04 15:16:37 2009 +0100
@@ -1,16 +1,9 @@
import serial
+import exceptions
+import ctypes
+import time
from threading import Lock
-LPC_CMDS=dict(IDLE = 0x00,
- START = 0x01,
- STOP = 0x02,
- SET_TRACE_VARIABLE = 0x04,
- GET_TRACE_VARIABLES = 0x05,
- SET_FORCED_VARIABLE = 0x06,
- GET_PLCID = 0x07)
-
-WAIT_DATA = 0x04
-
LPC_STATUS=dict(STARTED = 0x01,
STOPPED = 0x02,
DEBUG = 0x03)
@@ -26,13 +19,27 @@
class LPCProto:
def __init__(self, port, rate, timeout):
+ # serialize access lock
+ self.TransactionLock = Lock()
# open serial port
- self.serialPort = serial.Serial( port, rate, timeout = timeout )
+# self.serialPort = serial.Serial( port, rate, timeout = timeout )
+ # Debugging serial stuff
+ self._serialPort = serial.Serial( port, rate, timeout = timeout )
+ class myser:
+ def read(self_,cnt):
+ res = self._serialPort.read(cnt)
+ print "Recv :", map(hex,map(ord,res))
+ return res
+ def write(self_, str):
+ print "Send :", map(hex,map(ord,str))
+ self._serialPort.write(str)
+ def flush(self_):
+ self._serialPort.flush()
+ self.serialPort = myser()
+ # start with empty
self.serialPort.flush()
# handshake
- self.HandleTransaction(LPCTransaction("IDLE"))
- # serialize access lock
- self.TransactionLock = Lock()
+ self.HandleTransaction(IDLETransaction())
def HandleTransaction(self, transaction):
self.TransactionLock.acquire()
@@ -50,12 +57,12 @@
return current_plc_status, res
class LPCTransaction:
- def __init__(self, command, optdata):
- self.Command = LPC_CMDS[command]
- self.OptData = optdata[:]
- self.serialPort = None
+ def __init__(self, command, optdata = ""):
+ self.Command = command
+ self.OptData = optdata
+ self.pseudofile = None
- def SetPseudoFile(pseudofile):
+ def SetPseudoFile(self, pseudofile):
self.pseudofile = pseudofile
def SendCommand(self):
@@ -63,26 +70,74 @@
self.pseudofile.write(chr(self.Command))
def GetCommandAck(self):
- comm_status, current_plc_status = map(ord, self.pseudofile.read(2))
+ res = self.pseudofile.read(2)
+ if len(res) == 2:
+ comm_status, current_plc_status = map(ord, res)
+ else:
+ raise LPCError("LPC transaction error - controller did not ack order")
# LPC returns command itself as an ack for command
if(comm_status == self.Command):
return current_plc_status
return None
- def ExchangeData(self):
- 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)
- return None
-
+ def SendData(self):
+ 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)
+
+ def GetData(self):
+ 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)
+
+ def ExchangeData(self):
+ pass
+
+class IDLETransaction(LPCTransaction):
+ def __init__(self):
+ LPCTransaction.__init__(self, 0x00)
+
+class STARTTransaction(LPCTransaction):
+ def __init__(self):
+ LPCTransaction.__init__(self, 0x01)
+
+class STOPTransaction(LPCTransaction):
+ def __init__(self):
+ LPCTransaction.__init__(self, 0x02)
+
+class SET_TRACE_VARIABLETransaction(LPCTransaction):
+ def __init__(self, data):
+ LPCTransaction.__init__(self, 0x04, data)
+ ExchangeData = LPCTransaction.SendData
+
+class GET_TRACE_VARIABLETransaction(LPCTransaction):
+ def __init__(self):
+ LPCTransaction.__init__(self, 0x05)
+ ExchangeData = LPCTransaction.GetData
+
+class SET_FORCED_VARIABLETransaction(LPCTransaction):
+ def __init__(self, data):
+ LPCTransaction.__init__(self, 0x06, data)
+ ExchangeData = LPCTransaction.SendData
+
+class GET_PLCIDTransaction(LPCTransaction):
+ def __init__(self):
+ LPCTransaction.__init__(self, 0x07)
+ ExchangeData = LPCTransaction.GetData
+
if __name__ == "__main__":
- TestConnection = LPCProto()
-
\ No newline at end of file
+ TestConnection = LPCProto(6,115200,2)
+ #TestConnection.HandleTransaction(GET_PLCIDTransaction())
+ TestConnection.HandleTransaction(STARTTransaction())
+ TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
+ "\x03\x00\x00\x00"))
+ TestConnection.HandleTransaction(STARTTransaction())
+ while True:
+ time.sleep(0.5)
+ TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
+ "\x01\x00\x00\x00"*31))
+ #print map(hex,map(ord,TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction())))
+ #TestConnection.HandleTransaction(STOPTransaction())