author | Edouard Tisserant |
Fri, 22 Mar 2013 10:46:51 +0900 | |
changeset 36 | 73360d0bacbc |
parent 29 | 86fb7dc2b54e |
child 45 | 786b12887e91 |
permissions | -rw-r--r-- |
0
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
1 |
import ctypes |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
2 |
from LPCProto import * |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
3 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
4 |
LPC_STATUS={0xaa : "Started", |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
5 |
0x55 : "Stopped"} |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
6 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
7 |
class LPCAppProto(LPCProto): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
8 |
def HandleTransaction(self, transaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
9 |
self.TransactionLock.acquire() |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
10 |
try: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
11 |
transaction.SetPseudoFile(self.serialPort) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
12 |
# send command, wait ack (timeout) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
13 |
transaction.SendCommand() |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
14 |
current_plc_status = transaction.GetCommandAck() |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
15 |
if current_plc_status is not None: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
16 |
res = transaction.ExchangeData() |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
17 |
else: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
18 |
raise LPCProtoError("controller did not answer as expected") |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
19 |
except Exception, e: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
20 |
raise LPCProtoError("application mode transaction error : "+str(e)) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
21 |
finally: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
22 |
self.TransactionLock.release() |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
23 |
return LPC_STATUS.get(current_plc_status,"Broken"), res |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
24 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
25 |
class LPCAppTransaction: |
29 | 26 |
def __init__(self, command): |
0
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
27 |
self.Command = command |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
28 |
self.pseudofile = None |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
29 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
30 |
def SetPseudoFile(self, pseudofile): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
31 |
self.pseudofile = pseudofile |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
32 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
33 |
def SendCommand(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
34 |
# send command thread |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
35 |
self.pseudofile.write(chr(self.Command)) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
36 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
37 |
def GetCommandAck(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
38 |
res = self.pseudofile.read(2) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
39 |
if len(res) == 2: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
40 |
comm_status, current_plc_status = map(ord, res) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
41 |
else: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
42 |
raise LPCProtoError("LPC transaction error - controller did not ack order") |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
43 |
# LPC returns command itself as an ack for command |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
44 |
if(comm_status == self.Command): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
45 |
return current_plc_status |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
46 |
return None |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
47 |
|
29 | 48 |
def SendData(self, Data): |
36
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
49 |
return self.pseudofile.write(Data) |
0
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
50 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
51 |
def GetData(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
52 |
lengthstr = self.pseudofile.read(4) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
53 |
# transform a byte string into length |
36
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
54 |
length = ctypes.cast( |
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
55 |
ctypes.c_char_p(lengthstr), |
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
56 |
ctypes.POINTER(ctypes.c_uint32)).contents.value |
0
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
57 |
return self.pseudofile.read(length) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
58 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
59 |
def ExchangeData(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
60 |
pass |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
61 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
62 |
class IDLETransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
63 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
64 |
LPCAppTransaction.__init__(self, 0x07) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
65 |
ExchangeData = LPCAppTransaction.GetData |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
66 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
67 |
class STARTTransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
68 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
69 |
LPCAppTransaction.__init__(self, 0x01) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
70 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
71 |
class STOPTransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
72 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
73 |
LPCAppTransaction.__init__(self, 0x02) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
74 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
75 |
class RESETTransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
76 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
77 |
LPCAppTransaction.__init__(self, 0x03) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
78 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
79 |
class SET_TRACE_VARIABLETransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
80 |
def __init__(self, data): |
29 | 81 |
LPCAppTransaction.__init__(self, 0x04) |
36
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
82 |
length = len(data) |
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
83 |
# transform length into a byte string |
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
84 |
# we presuppose endianess of LPC same as PC |
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
85 |
lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_uint32(length)),4) |
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
86 |
self.Data = lengthstr + data |
73360d0bacbc
Serial protocol SendData only prefix with size in case of variable size content (i.e. SetTraceVariableList)
Edouard Tisserant
parents:
29
diff
changeset
|
87 |
|
29 | 88 |
def ExchangeData(self): |
89 |
self.SendData(self.Data) |
|
0
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
90 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
91 |
class GET_TRACE_VARIABLETransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
92 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
93 |
LPCAppTransaction.__init__(self, 0x05) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
94 |
ExchangeData = LPCAppTransaction.GetData |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
95 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
96 |
class GET_PLCIDTransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
97 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
98 |
LPCAppTransaction.__init__(self, 0x07) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
99 |
ExchangeData = LPCAppTransaction.GetData |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
100 |
|
29 | 101 |
class GET_LOGCOUNTSTransaction(LPCAppTransaction): |
102 |
def __init__(self): |
|
103 |
LPCAppTransaction.__init__(self, 0x0B) |
|
104 |
ExchangeData = LPCAppTransaction.GetData |
|
105 |
||
106 |
class GET_LOGMSGTransaction(LPCAppTransaction): |
|
107 |
def __init__(self,level,msgid): |
|
108 |
LPCAppTransaction.__init__(self, 0x0C) |
|
109 |
msgidstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(msgid)),4) |
|
110 |
self.Data = chr(level)+msgidstr |
|
111 |
||
112 |
def ExchangeData(self): |
|
113 |
self.SendData(self.Data) |
|
114 |
return self.GetData() |
|
115 |
||
0
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
116 |
if __name__ == "__main__": |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
117 |
__builtins__.BMZ_DBG = True |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
118 |
TestConnection = LPCAppProto(6,115200,2) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
119 |
# TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
120 |
TestConnection.HandleTransaction(STARTTransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
121 |
# TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
122 |
# "\x03\x00\x00\x00"*200)) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
123 |
# TestConnection.HandleTransaction(STARTTransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
124 |
while True: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
125 |
TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
126 |
"\x01\x00\x00\x00"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
127 |
"\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
128 |
"\x01\x02\x02\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
129 |
"\x01\x00\x00\x00"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
130 |
"\x08"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
131 |
"\x01\x02\x02\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
132 |
"\x01\x02\x02\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
133 |
"\x01\x00\x00\x00"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
134 |
"\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
135 |
"\x01\x02\x02\x04")) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
136 |
#status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
137 |
#print len(res) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
138 |
#print "GOT : ", map(hex, map(ord, res)) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
139 |
#TestConnection.HandleTransaction(STOPTransaction()) |