|
1 import ctypes |
|
2 from LPCProto import * |
|
3 |
|
4 LPC_STATUS={0xaa : "Started", |
|
5 0x55 : "Stopped"} |
|
6 |
|
7 class LPCAppProto(LPCProto): |
|
8 def HandleTransaction(self, transaction): |
|
9 self.TransactionLock.acquire() |
|
10 try: |
|
11 transaction.SetPseudoFile(self.serialPort) |
|
12 # send command, wait ack (timeout) |
|
13 transaction.SendCommand() |
|
14 current_plc_status = transaction.GetCommandAck() |
|
15 if current_plc_status is not None: |
|
16 res = transaction.ExchangeData() |
|
17 else: |
|
18 raise LPCProtoError("controller did not answer as expected") |
|
19 except Exception, e: |
|
20 raise LPCProtoError("application mode transaction error : "+str(e)) |
|
21 finally: |
|
22 self.TransactionLock.release() |
|
23 return LPC_STATUS.get(current_plc_status,"Broken"), res |
|
24 |
|
25 class LPCAppTransaction: |
|
26 def __init__(self, command, optdata = ""): |
|
27 self.Command = command |
|
28 self.OptData = optdata |
|
29 self.pseudofile = None |
|
30 |
|
31 def SetPseudoFile(self, pseudofile): |
|
32 self.pseudofile = pseudofile |
|
33 |
|
34 def SendCommand(self): |
|
35 # send command thread |
|
36 self.pseudofile.write(chr(self.Command)) |
|
37 |
|
38 def GetCommandAck(self): |
|
39 res = self.pseudofile.read(2) |
|
40 if len(res) == 2: |
|
41 comm_status, current_plc_status = map(ord, res) |
|
42 else: |
|
43 raise LPCProtoError("LPC transaction error - controller did not ack order") |
|
44 # LPC returns command itself as an ack for command |
|
45 if(comm_status == self.Command): |
|
46 return current_plc_status |
|
47 return None |
|
48 |
|
49 def SendData(self): |
|
50 length = len(self.OptData) |
|
51 # transform length into a byte string |
|
52 # we presuppose endianess of LPC same as PC |
|
53 lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) |
|
54 buffer = lengthstr + self.OptData |
|
55 return self.pseudofile.write(buffer) |
|
56 |
|
57 def GetData(self): |
|
58 lengthstr = self.pseudofile.read(4) |
|
59 # transform a byte string into length |
|
60 length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value |
|
61 return self.pseudofile.read(length) |
|
62 |
|
63 def ExchangeData(self): |
|
64 pass |
|
65 |
|
66 class IDLETransaction(LPCAppTransaction): |
|
67 def __init__(self): |
|
68 LPCAppTransaction.__init__(self, 0x07) |
|
69 ExchangeData = LPCAppTransaction.GetData |
|
70 |
|
71 class STARTTransaction(LPCAppTransaction): |
|
72 def __init__(self): |
|
73 LPCAppTransaction.__init__(self, 0x01) |
|
74 |
|
75 class STOPTransaction(LPCAppTransaction): |
|
76 def __init__(self): |
|
77 LPCAppTransaction.__init__(self, 0x02) |
|
78 |
|
79 class RESETTransaction(LPCAppTransaction): |
|
80 def __init__(self): |
|
81 LPCAppTransaction.__init__(self, 0x03) |
|
82 |
|
83 class SET_TRACE_VARIABLETransaction(LPCAppTransaction): |
|
84 def __init__(self, data): |
|
85 LPCAppTransaction.__init__(self, 0x04, data) |
|
86 ExchangeData = LPCAppTransaction.SendData |
|
87 |
|
88 class GET_TRACE_VARIABLETransaction(LPCAppTransaction): |
|
89 def __init__(self): |
|
90 LPCAppTransaction.__init__(self, 0x05) |
|
91 ExchangeData = LPCAppTransaction.GetData |
|
92 |
|
93 class GET_PLCIDTransaction(LPCAppTransaction): |
|
94 def __init__(self): |
|
95 LPCAppTransaction.__init__(self, 0x07) |
|
96 ExchangeData = LPCAppTransaction.GetData |
|
97 |
|
98 if __name__ == "__main__": |
|
99 __builtins__.BMZ_DBG = True |
|
100 TestConnection = LPCAppProto(6,115200,2) |
|
101 # TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
|
102 TestConnection.HandleTransaction(STARTTransaction()) |
|
103 # TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
|
104 # "\x03\x00\x00\x00"*200)) |
|
105 # TestConnection.HandleTransaction(STARTTransaction()) |
|
106 while True: |
|
107 TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
|
108 "\x01\x00\x00\x00"+ |
|
109 "\x04"+ |
|
110 "\x01\x02\x02\x04"+ |
|
111 "\x01\x00\x00\x00"+ |
|
112 "\x08"+ |
|
113 "\x01\x02\x02\x04"+ |
|
114 "\x01\x02\x02\x04"+ |
|
115 "\x01\x00\x00\x00"+ |
|
116 "\x04"+ |
|
117 "\x01\x02\x02\x04")) |
|
118 #status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
|
119 #print len(res) |
|
120 #print "GOT : ", map(hex, map(ord, res)) |
|
121 #TestConnection.HandleTransaction(STOPTransaction()) |