author | laurent |
Wed, 16 Dec 2009 13:26:59 +0100 | |
changeset 523 | 27903000e4ea |
parent 508 | 73ecb803d8af |
child 536 | 9b77aabf3d36 |
permissions | -rw-r--r-- |
448 | 1 |
import serial |
453 | 2 |
import exceptions |
3 |
import ctypes |
|
4 |
import time |
|
448 | 5 |
from threading import Lock |
6 |
||
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
7 |
MAX_PACKET_SIZE=64 |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
8 |
|
508 | 9 |
LPC_STATUS={0x01 : "Started", |
10 |
0x02 : "Stopped"} |
|
448 | 11 |
|
12 |
class LPCError(exceptions.Exception): |
|
13 |
"""Exception class""" |
|
14 |
def __init__(self, msg): |
|
15 |
self.msg = msg |
|
16 |
return |
|
17 |
||
18 |
def __str__(self): |
|
19 |
return "LPC communication error ! " + str(self.msg) |
|
20 |
||
21 |
class LPCProto: |
|
22 |
def __init__(self, port, rate, timeout): |
|
453 | 23 |
# serialize access lock |
24 |
self.TransactionLock = Lock() |
|
448 | 25 |
# open serial port |
453 | 26 |
# self.serialPort = serial.Serial( port, rate, timeout = timeout ) |
27 |
# Debugging serial stuff |
|
28 |
self._serialPort = serial.Serial( port, rate, timeout = timeout ) |
|
29 |
class myser: |
|
30 |
def read(self_,cnt): |
|
31 |
res = self._serialPort.read(cnt) |
|
32 |
print "Recv :", map(hex,map(ord,res)) |
|
33 |
return res |
|
34 |
def write(self_, str): |
|
35 |
print "Send :", map(hex,map(ord,str)) |
|
36 |
self._serialPort.write(str) |
|
37 |
def flush(self_): |
|
38 |
self._serialPort.flush() |
|
39 |
self.serialPort = myser() |
|
40 |
# start with empty |
|
448 | 41 |
self.serialPort.flush() |
42 |
||
43 |
def HandleTransaction(self, transaction): |
|
44 |
self.TransactionLock.acquire() |
|
45 |
try: |
|
46 |
transaction.SetPseudoFile(self.serialPort) |
|
47 |
# send command, wait ack (timeout) |
|
48 |
transaction.SendCommand() |
|
49 |
current_plc_status = transaction.GetCommandAck() |
|
50 |
if current_plc_status is not None: |
|
51 |
res = transaction.ExchangeData() |
|
52 |
else: |
|
53 |
raise LPCError("LPC transaction error - controller did not answer as expected") |
|
54 |
finally: |
|
55 |
self.TransactionLock.release() |
|
508 | 56 |
return LPC_STATUS.get(current_plc_status,"Broken"), res |
448 | 57 |
|
58 |
class LPCTransaction: |
|
453 | 59 |
def __init__(self, command, optdata = ""): |
60 |
self.Command = command |
|
61 |
self.OptData = optdata |
|
62 |
self.pseudofile = None |
|
448 | 63 |
|
453 | 64 |
def SetPseudoFile(self, pseudofile): |
448 | 65 |
self.pseudofile = pseudofile |
66 |
||
67 |
def SendCommand(self): |
|
68 |
# send command thread |
|
69 |
self.pseudofile.write(chr(self.Command)) |
|
70 |
||
71 |
def GetCommandAck(self): |
|
453 | 72 |
res = self.pseudofile.read(2) |
73 |
if len(res) == 2: |
|
74 |
comm_status, current_plc_status = map(ord, res) |
|
75 |
else: |
|
76 |
raise LPCError("LPC transaction error - controller did not ack order") |
|
448 | 77 |
# LPC returns command itself as an ack for command |
78 |
if(comm_status == self.Command): |
|
79 |
return current_plc_status |
|
80 |
return None |
|
81 |
||
453 | 82 |
def SendData(self): |
83 |
length = len(self.OptData) |
|
84 |
# transform length into a byte string |
|
85 |
# we presuppose endianess of LPC same as PC |
|
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
86 |
lengthstr = ctypes.string_at(ctypes.pointer(ctypes.c_int(length)),4) |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
87 |
buffer = lengthstr + self.OptData |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
88 |
################################################################### |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
89 |
# TO BE REMOVED AS SOON AS USB IS FIXED IN CONTROLLER |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
90 |
################################################################### |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
91 |
length += 4 |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
92 |
cursor = 0 |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
93 |
while cursor < length: |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
94 |
next_cursor = cursor + MAX_PACKET_SIZE |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
95 |
# sent just enough bytes to not crash controller |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
96 |
self.pseudofile.write(buffer[cursor:next_cursor]) |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
97 |
# if sent quantity was 128 |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
98 |
if next_cursor <= length: |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
99 |
self.GetCommandAck() |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
100 |
cursor = next_cursor |
453 | 101 |
|
102 |
def GetData(self): |
|
103 |
lengthstr = self.pseudofile.read(4) |
|
104 |
# transform a byte string into length |
|
105 |
length = ctypes.cast(ctypes.c_char_p(lengthstr), ctypes.POINTER(ctypes.c_int)).contents.value |
|
106 |
return self.pseudofile.read(length) |
|
107 |
||
108 |
def ExchangeData(self): |
|
109 |
pass |
|
110 |
||
111 |
class IDLETransaction(LPCTransaction): |
|
112 |
def __init__(self): |
|
113 |
LPCTransaction.__init__(self, 0x00) |
|
114 |
||
115 |
class STARTTransaction(LPCTransaction): |
|
116 |
def __init__(self): |
|
117 |
LPCTransaction.__init__(self, 0x01) |
|
118 |
||
119 |
class STOPTransaction(LPCTransaction): |
|
120 |
def __init__(self): |
|
121 |
LPCTransaction.__init__(self, 0x02) |
|
122 |
||
123 |
class SET_TRACE_VARIABLETransaction(LPCTransaction): |
|
124 |
def __init__(self, data): |
|
125 |
LPCTransaction.__init__(self, 0x04, data) |
|
126 |
ExchangeData = LPCTransaction.SendData |
|
127 |
||
128 |
class GET_TRACE_VARIABLETransaction(LPCTransaction): |
|
129 |
def __init__(self): |
|
130 |
LPCTransaction.__init__(self, 0x05) |
|
131 |
ExchangeData = LPCTransaction.GetData |
|
132 |
||
133 |
class GET_PLCIDTransaction(LPCTransaction): |
|
134 |
def __init__(self): |
|
135 |
LPCTransaction.__init__(self, 0x07) |
|
136 |
ExchangeData = LPCTransaction.GetData |
|
137 |
||
448 | 138 |
if __name__ == "__main__": |
453 | 139 |
TestConnection = LPCProto(6,115200,2) |
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
140 |
# TestConnection.HandleTransaction(GET_PLCIDTransaction()) |
453 | 141 |
TestConnection.HandleTransaction(STARTTransaction()) |
482
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
142 |
# TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
143 |
# "\x03\x00\x00\x00"*200)) |
7c83eb6a55bd
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
453
diff
changeset
|
144 |
# TestConnection.HandleTransaction(STARTTransaction()) |
508 | 145 |
while True: |
146 |
TestConnection.HandleTransaction(SET_TRACE_VARIABLETransaction( |
|
147 |
"\x01\x00\x00\x00"+ |
|
148 |
"\x04"+ |
|
149 |
"\x01\x02\x02\x04"+ |
|
150 |
"\x01\x00\x00\x00"+ |
|
151 |
"\x08"+ |
|
152 |
"\x01\x02\x02\x04"+ |
|
153 |
"\x01\x02\x02\x04"+ |
|
154 |
"\x01\x00\x00\x00"+ |
|
155 |
"\x04"+ |
|
156 |
"\x01\x02\x02\x04")) |
|
502 | 157 |
#status,res = TestConnection.HandleTransaction(GET_TRACE_VARIABLETransaction()) |
158 |
#print len(res) |
|
159 |
#print "GOT : ", map(hex, map(ord, res)) |
|
453 | 160 |
#TestConnection.HandleTransaction(STOPTransaction()) |