author | Lolitech |
Fri, 04 Jun 2010 15:11:27 +0200 | |
changeset 557 | b155ada1c14a |
parent 545 | 627e5c636a4f |
child 563 | c74a37d156df |
permissions | -rw-r--r-- |
448 | 1 |
import serial |
453 | 2 |
import exceptions |
448 | 3 |
from threading import Lock |
4 |
||
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
5 |
class LPCProtoError(exceptions.Exception): |
448 | 6 |
"""Exception class""" |
7 |
def __init__(self, msg): |
|
8 |
self.msg = msg |
|
9 |
||
10 |
def __str__(self): |
|
11 |
return "LPC communication error ! " + str(self.msg) |
|
12 |
||
13 |
class LPCProto: |
|
14 |
def __init__(self, port, rate, timeout): |
|
453 | 15 |
# serialize access lock |
16 |
self.TransactionLock = Lock() |
|
448 | 17 |
# open serial port |
453 | 18 |
# self.serialPort = serial.Serial( port, rate, timeout = timeout ) |
19 |
# Debugging serial stuff |
|
20 |
self._serialPort = serial.Serial( port, rate, timeout = timeout ) |
|
21 |
class myser: |
|
22 |
def read(self_,cnt): |
|
23 |
res = self._serialPort.read(cnt) |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
24 |
if len(res) > 16: |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
25 |
print "Recv :", map(hex,map(ord,res[:16])), "[...]" |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
26 |
else: |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
27 |
print "Recv :", map(hex,map(ord,res)) |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
28 |
|
453 | 29 |
return res |
30 |
def write(self_, str): |
|
545
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
31 |
if len(str) > 16: |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
32 |
print "Send :", map(hex,map(ord,str[:16])), "[...]" |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
33 |
else: |
627e5c636a4f
Refactored LPC connector for new bootloader and application modes
Lolitech
parents:
536
diff
changeset
|
34 |
print "Send :", map(hex,map(ord,str)) |
453 | 35 |
self._serialPort.write(str) |
36 |
def flush(self_): |
|
37 |
self._serialPort.flush() |
|
38 |
self.serialPort = myser() |
|
39 |
# start with empty |
|
448 | 40 |
self.serialPort.flush() |
41 |