21 finally: |
21 finally: |
22 self.TransactionLock.release() |
22 self.TransactionLock.release() |
23 return LPC_STATUS.get(current_plc_status,"Broken"), res |
23 return LPC_STATUS.get(current_plc_status,"Broken"), res |
24 |
24 |
25 class LPCAppTransaction: |
25 class LPCAppTransaction: |
26 def __init__(self, command, optdata = ""): |
26 def __init__(self, command): |
27 self.Command = command |
27 self.Command = command |
28 self.OptData = optdata |
|
29 self.pseudofile = None |
28 self.pseudofile = None |
30 |
29 |
31 def SetPseudoFile(self, pseudofile): |
30 def SetPseudoFile(self, pseudofile): |
32 self.pseudofile = pseudofile |
31 self.pseudofile = pseudofile |
33 |
32 |
44 # LPC returns command itself as an ack for command |
43 # LPC returns command itself as an ack for command |
45 if(comm_status == self.Command): |
44 if(comm_status == self.Command): |
46 return current_plc_status |
45 return current_plc_status |
47 return None |
46 return None |
48 |
47 |
49 def SendData(self): |
48 def SendData(self, Data): |
50 length = len(self.OptData) |
49 length = len(Data) |
51 # transform length into a byte string |
50 # transform length into a byte string |
52 # we presuppose endianess of LPC same as PC |
51 # we presuppose endianess of LPC same as PC |
53 lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) |
52 lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) |
54 buffer = lengthstr + self.OptData |
53 buffer = lengthstr + Data |
55 return self.pseudofile.write(buffer) |
54 return self.pseudofile.write(buffer) |
56 |
55 |
57 def GetData(self): |
56 def GetData(self): |
58 lengthstr = self.pseudofile.read(4) |
57 lengthstr = self.pseudofile.read(4) |
59 # transform a byte string into length |
58 # transform a byte string into length |
80 def __init__(self): |
79 def __init__(self): |
81 LPCAppTransaction.__init__(self, 0x03) |
80 LPCAppTransaction.__init__(self, 0x03) |
82 |
81 |
83 class SET_TRACE_VARIABLETransaction(LPCAppTransaction): |
82 class SET_TRACE_VARIABLETransaction(LPCAppTransaction): |
84 def __init__(self, data): |
83 def __init__(self, data): |
85 LPCAppTransaction.__init__(self, 0x04, data) |
84 LPCAppTransaction.__init__(self, 0x04) |
86 ExchangeData = LPCAppTransaction.SendData |
85 self.Data = data |
|
86 def ExchangeData(self): |
|
87 self.SendData(self.Data) |
87 |
88 |
88 class GET_TRACE_VARIABLETransaction(LPCAppTransaction): |
89 class GET_TRACE_VARIABLETransaction(LPCAppTransaction): |
89 def __init__(self): |
90 def __init__(self): |
90 LPCAppTransaction.__init__(self, 0x05) |
91 LPCAppTransaction.__init__(self, 0x05) |
91 ExchangeData = LPCAppTransaction.GetData |
92 ExchangeData = LPCAppTransaction.GetData |
92 |
93 |
93 class GET_PLCIDTransaction(LPCAppTransaction): |
94 class GET_PLCIDTransaction(LPCAppTransaction): |
94 def __init__(self): |
95 def __init__(self): |
95 LPCAppTransaction.__init__(self, 0x07) |
96 LPCAppTransaction.__init__(self, 0x07) |
96 ExchangeData = LPCAppTransaction.GetData |
97 ExchangeData = LPCAppTransaction.GetData |
|
98 |
|
99 class GET_LOGCOUNTSTransaction(LPCAppTransaction): |
|
100 def __init__(self): |
|
101 LPCAppTransaction.__init__(self, 0x0B) |
|
102 ExchangeData = LPCAppTransaction.GetData |
|
103 |
|
104 class GET_LOGMSGTransaction(LPCAppTransaction): |
|
105 def __init__(self,level,msgid): |
|
106 LPCAppTransaction.__init__(self, 0x0C) |
|
107 msgidstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(msgid)),4) |
|
108 self.Data = chr(level)+msgidstr |
|
109 |
|
110 def ExchangeData(self): |
|
111 self.SendData(self.Data) |
|
112 return self.GetData() |
97 |
113 |
98 if __name__ == "__main__": |
114 if __name__ == "__main__": |
99 __builtins__.BMZ_DBG = True |
115 __builtins__.BMZ_DBG = True |
100 TestConnection = LPCAppProto(6,115200,2) |
116 TestConnection = LPCAppProto(6,115200,2) |
101 # TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
117 # TestConnection.HandleTransaction(GET_PLCIDTransaction()) |