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