author | edouard |
Wed, 23 Feb 2011 16:54:29 +0100 | |
changeset 578 | 6f5795bdee49 |
parent 576 | 7fcdc0d3d8d9 |
permissions | -rw-r--r-- |
448 | 1 |
import serial |
453 | 2 |
import exceptions |
448 | 3 |
from threading import Lock |
571
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
4 |
import time |
448 | 5 |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
6 |
class LPCProtoError(exceptions.Exception): |
448 | 7 |
"""Exception class""" |
8 |
def __init__(self, msg): |
|
9 |
self.msg = msg |
|
10 |
||
11 |
def __str__(self): |
|
576
7fcdc0d3d8d9
Some typo fixes to make debug related methods in LPCAppOject stop throwing exceptions, less agressive error message when unplugging LPC
edouard
parents:
571
diff
changeset
|
12 |
return "Exception in PLC protocol : " + str(self.msg) |
448 | 13 |
|
14 |
class LPCProto: |
|
15 |
def __init__(self, port, rate, timeout): |
|
453 | 16 |
# serialize access lock |
17 |
self.TransactionLock = Lock() |
|
571
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
18 |
if BMZ_DBG: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
19 |
# Debugging serial stuff |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
20 |
self._serialPort = serial.Serial( port, rate, timeout = timeout, writeTimeout = timeout ) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
21 |
class myser: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
22 |
def readline(self_): |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
23 |
res = self._serialPort.readline() |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
24 |
print 'Recv :"', res, '"' |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
25 |
return res |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
26 |
|
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
27 |
def read(self_,cnt): |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
28 |
res = self._serialPort.read(cnt) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
29 |
if len(res) > 16: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
30 |
print "Recv :", map(hex,map(ord,res[:16])), "[...]" |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
31 |
else: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
32 |
print "Recv :", map(hex,map(ord,res)) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
33 |
|
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
34 |
return res |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
35 |
def write(self_, string): |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
36 |
lstr=len(string) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
37 |
if lstr > 16: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
38 |
print "Send :", map(hex,map(ord,string[:16])), "[...]" |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
39 |
else: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
40 |
print "Send :", map(hex,map(ord,string)) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
41 |
return self._serialPort.write(string) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
42 |
# while len(string)>0: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
43 |
# i = self._serialPort.write(string[:4096]) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
44 |
# print ".", |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
45 |
# string = string[i:] |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
46 |
|
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
47 |
#return lstr |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
48 |
def flush(self_): |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
49 |
return self._serialPort.flush() |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
50 |
def close(self_): |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
51 |
self._serialPort.close() |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
52 |
self.serialPort = myser() |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
53 |
else: |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
54 |
# open serial port |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
55 |
self.serialPort = serial.Serial( port, rate, timeout = timeout ) |
427bf9130d12
Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents:
563
diff
changeset
|
56 |
# start with empty buffer |
448 | 57 |
self.serialPort.flush() |
58 |
||
563 | 59 |
def __del__(self): |
60 |
if self.serialPort: |
|
61 |
self.serialPort.close() |
|
62 |
||
63 |
def close(self): |
|
64 |
self.serialPort.close() |
|
65 |
self.serialPort = None |