author | Laurent Bessard |
Fri, 08 Jun 2012 12:40:31 +0200 | |
changeset 2 | 0c697b8fc443 |
parent 0 | 51f5a3138405 |
child 29 | 86fb7dc2b54e |
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: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
26 |
def __init__(self, command, optdata = ""): |
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.OptData = optdata |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
29 |
self.pseudofile = None |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
30 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
31 |
def SetPseudoFile(self, pseudofile): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
32 |
self.pseudofile = pseudofile |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
33 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
34 |
def SendCommand(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
35 |
# send command thread |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
36 |
self.pseudofile.write(chr(self.Command)) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
37 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
38 |
def GetCommandAck(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
39 |
res = self.pseudofile.read(2) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
40 |
if len(res) == 2: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
41 |
comm_status, current_plc_status = map(ord, res) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
42 |
else: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
43 |
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
|
44 |
# 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
|
45 |
if(comm_status == self.Command): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
46 |
return current_plc_status |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
47 |
return None |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
48 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
49 |
def SendData(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
50 |
length = len(self.OptData) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
51 |
# transform length into a byte string |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
52 |
# we presuppose endianess of LPC same as PC |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
53 |
lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
54 |
buffer = lengthstr + self.OptData |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
55 |
return self.pseudofile.write(buffer) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
56 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
57 |
def GetData(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
58 |
lengthstr = self.pseudofile.read(4) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
59 |
# transform a byte string into length |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
60 |
length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
61 |
return self.pseudofile.read(length) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
62 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
63 |
def ExchangeData(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
64 |
pass |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
65 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
66 |
class IDLETransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
67 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
68 |
LPCAppTransaction.__init__(self, 0x07) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
69 |
ExchangeData = LPCAppTransaction.GetData |
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 STARTTransaction(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, 0x01) |
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 STOPTransaction(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, 0x02) |
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 RESETTransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
80 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
81 |
LPCAppTransaction.__init__(self, 0x03) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
82 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
83 |
class SET_TRACE_VARIABLETransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
84 |
def __init__(self, data): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
85 |
LPCAppTransaction.__init__(self, 0x04, data) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
86 |
ExchangeData = LPCAppTransaction.SendData |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
87 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
88 |
class GET_TRACE_VARIABLETransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
89 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
90 |
LPCAppTransaction.__init__(self, 0x05) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
91 |
ExchangeData = LPCAppTransaction.GetData |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
92 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
93 |
class GET_PLCIDTransaction(LPCAppTransaction): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
94 |
def __init__(self): |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
95 |
LPCAppTransaction.__init__(self, 0x07) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
96 |
ExchangeData = LPCAppTransaction.GetData |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
97 |
|
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
98 |
if __name__ == "__main__": |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
99 |
__builtins__.BMZ_DBG = True |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
100 |
TestConnection = LPCAppProto(6,115200,2) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
101 |
# TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
102 |
TestConnection.HandleTransaction(STARTTransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
103 |
# TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
104 |
# "\x03\x00\x00\x00"*200)) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
105 |
# TestConnection.HandleTransaction(STARTTransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
106 |
while True: |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
107 |
TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
108 |
"\x01\x00\x00\x00"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
109 |
"\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
110 |
"\x01\x02\x02\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
111 |
"\x01\x00\x00\x00"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
112 |
"\x08"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
113 |
"\x01\x02\x02\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
114 |
"\x01\x02\x02\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
115 |
"\x01\x00\x00\x00"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
116 |
"\x04"+ |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
117 |
"\x01\x02\x02\x04")) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
118 |
#status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
119 |
#print len(res) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
120 |
#print "GOT : ", map(hex, map(ord, res)) |
51f5a3138405
Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff
changeset
|
121 |
#TestConnection.HandleTransaction(STOPTransaction()) |