connectors/LPC/LPCProto.py
changeset 453 923d036dfa90
parent 448 8ef035de86de
child 482 7c83eb6a55bd
equal deleted inserted replaced
452:2d0718a05cc7 453:923d036dfa90
     1 import serial
     1 import serial
       
     2 import exceptions
       
     3 import ctypes
       
     4 import time
     2 from threading import Lock
     5 from threading import Lock
     3 
       
     4 LPC_CMDS=dict(IDLE = 0x00,
       
     5               START = 0x01,
       
     6               STOP = 0x02,
       
     7               SET_TRACE_VARIABLE = 0x04,
       
     8               GET_TRACE_VARIABLES = 0x05,
       
     9               SET_FORCED_VARIABLE = 0x06,
       
    10               GET_PLCID = 0x07)
       
    11 
       
    12 WAIT_DATA = 0x04
       
    13 
     6 
    14 LPC_STATUS=dict(STARTED = 0x01,
     7 LPC_STATUS=dict(STARTED = 0x01,
    15                 STOPPED = 0x02,
     8                 STOPPED = 0x02,
    16                 DEBUG = 0x03)
     9                 DEBUG = 0x03)
    17 
    10 
    24         def __str__(self):
    17         def __str__(self):
    25                 return "LPC communication error ! " + str(self.msg)
    18                 return "LPC communication error ! " + str(self.msg)
    26 
    19 
    27 class LPCProto:
    20 class LPCProto:
    28     def __init__(self, port, rate, timeout):
    21     def __init__(self, port, rate, timeout):
       
    22         # serialize access lock
       
    23         self.TransactionLock = Lock()
    29         # open serial port
    24         # open serial port
    30         self.serialPort = serial.Serial( port, rate, timeout = timeout )
    25 #        self.serialPort = serial.Serial( port, rate, timeout = timeout )
       
    26         # Debugging serial stuff
       
    27         self._serialPort = serial.Serial( port, rate, timeout = timeout )
       
    28         class myser:
       
    29             def read(self_,cnt):
       
    30                 res = self._serialPort.read(cnt)
       
    31                 print "Recv :", map(hex,map(ord,res))
       
    32                 return res
       
    33             def write(self_, str):
       
    34                 print "Send :", map(hex,map(ord,str))
       
    35                 self._serialPort.write(str)
       
    36             def flush(self_):
       
    37                 self._serialPort.flush()
       
    38         self.serialPort = myser()
       
    39         # start with empty
    31         self.serialPort.flush()
    40         self.serialPort.flush()
    32         # handshake
    41         # handshake
    33         self.HandleTransaction(LPCTransaction("IDLE"))
    42         self.HandleTransaction(IDLETransaction())
    34         # serialize access lock
       
    35         self.TransactionLock = Lock()
       
    36     
    43     
    37     def HandleTransaction(self, transaction):
    44     def HandleTransaction(self, transaction):
    38         self.TransactionLock.acquire()
    45         self.TransactionLock.acquire()
    39         try:
    46         try:
    40             transaction.SetPseudoFile(self.serialPort)
    47             transaction.SetPseudoFile(self.serialPort)
    48         finally:
    55         finally:
    49             self.TransactionLock.release()
    56             self.TransactionLock.release()
    50         return current_plc_status, res
    57         return current_plc_status, res
    51     
    58     
    52 class LPCTransaction:
    59 class LPCTransaction:
    53     def __init__(self, command, optdata):
    60     def __init__(self, command, optdata = ""):
    54         self.Command =  LPC_CMDS[command]
    61         self.Command = command
    55         self.OptData = optdata[:]
    62         self.OptData = optdata
    56         self.serialPort = None
    63         self.pseudofile = None
    57         
    64         
    58     def SetPseudoFile(pseudofile):
    65     def SetPseudoFile(self, pseudofile):
    59         self.pseudofile = pseudofile
    66         self.pseudofile = pseudofile
    60         
    67         
    61     def SendCommand(self):
    68     def SendCommand(self):
    62         # send command thread
    69         # send command thread
    63         self.pseudofile.write(chr(self.Command))
    70         self.pseudofile.write(chr(self.Command))
    64         
    71         
    65     def GetCommandAck(self):
    72     def GetCommandAck(self):
    66         comm_status, current_plc_status = map(ord, self.pseudofile.read(2))
    73         res = self.pseudofile.read(2)
       
    74         if len(res) == 2:
       
    75             comm_status, current_plc_status = map(ord, res)
       
    76         else:
       
    77             raise LPCError("LPC transaction error - controller did not ack order")
    67         # LPC returns command itself as an ack for command
    78         # LPC returns command itself as an ack for command
    68         if(comm_status == self.Command):
    79         if(comm_status == self.Command):
    69             return current_plc_status
    80             return current_plc_status
    70         return None 
    81         return None 
    71         
    82         
    72     def ExchangeData(self):
    83     def SendData(self):
    73         if self.Command & WAIT_DATA :
    84         length = len(self.OptData)
    74             length = len(self.OptData)
    85         # transform length into a byte string
    75             # transform length into a byte string
    86         # we presuppose endianess of LPC same as PC
    76             # we presuppose endianess of LPC same as PC
    87         lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) 
    77             lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) 
    88         self.pseudofile.write(lengthstr + self.OptData)
    78             self.pseudofile.write(lengthstr + self.OptData)
    89 
    79             
    90     def GetData(self):
    80             lengthstr = self.pseudofile.read(4)
    91         lengthstr = self.pseudofile.read(4)
    81             # transform a byte string into length 
    92         # transform a byte string into length 
    82             length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value
    93         length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value
    83             return self.pseudofile.read(length)
    94         return self.pseudofile.read(length)
    84         return None
    95 
    85         
    96     def ExchangeData(self): 
       
    97         pass
       
    98 
       
    99 class IDLETransaction(LPCTransaction):
       
   100     def __init__(self):
       
   101         LPCTransaction.__init__(self, 0x00)
       
   102 
       
   103 class STARTTransaction(LPCTransaction):
       
   104     def __init__(self):
       
   105         LPCTransaction.__init__(self, 0x01)
       
   106     
       
   107 class STOPTransaction(LPCTransaction):
       
   108     def __init__(self):
       
   109         LPCTransaction.__init__(self, 0x02)
       
   110 
       
   111 class SET_TRACE_VARIABLETransaction(LPCTransaction):
       
   112     def __init__(self, data):
       
   113         LPCTransaction.__init__(self, 0x04, data)
       
   114     ExchangeData = LPCTransaction.SendData
       
   115 
       
   116 class GET_TRACE_VARIABLETransaction(LPCTransaction):
       
   117     def __init__(self):
       
   118         LPCTransaction.__init__(self, 0x05)
       
   119     ExchangeData = LPCTransaction.GetData
       
   120 
       
   121 class SET_FORCED_VARIABLETransaction(LPCTransaction):
       
   122     def __init__(self, data):
       
   123         LPCTransaction.__init__(self, 0x06, data)
       
   124     ExchangeData = LPCTransaction.SendData
       
   125 
       
   126 class GET_PLCIDTransaction(LPCTransaction):
       
   127     def __init__(self):
       
   128         LPCTransaction.__init__(self, 0x07)
       
   129     ExchangeData = LPCTransaction.GetData
       
   130 
    86 if __name__ == "__main__":
   131 if __name__ == "__main__":
    87     TestConnection = LPCProto()
   132     TestConnection = LPCProto(6,115200,2)
    88     
   133     #TestConnection.HandleTransaction(GET_PLCIDTransaction())
       
   134     TestConnection.HandleTransaction(STARTTransaction())
       
   135     TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
       
   136            "\x03\x00\x00\x00"))
       
   137     TestConnection.HandleTransaction(STARTTransaction())
       
   138     while True:
       
   139         time.sleep(0.5)
       
   140         TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction(
       
   141            "\x01\x00\x00\x00"*31))
       
   142    #print map(hex,map(ord,TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction())))
       
   143     #TestConnection.HandleTransaction(STOPTransaction())