connectors/LPC/LPCProto.py
author Lolitech
Fri, 04 Jun 2010 15:11:27 +0200
changeset 557 b155ada1c14a
parent 545 627e5c636a4f
child 563 c74a37d156df
permissions -rw-r--r--
Removed PLCprint, not existing in LPC connector context.
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     1
import serial
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
     2
import exceptions
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     3
from threading import Lock
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     4
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
     5
class LPCProtoError(exceptions.Exception):
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     6
        """Exception class"""
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     7
        def __init__(self, msg):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     8
                self.msg = msg
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     9
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    10
        def __str__(self):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    11
                return "LPC communication error ! " + str(self.msg)
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    12
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    13
class LPCProto:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    14
    def __init__(self, port, rate, timeout):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    15
        # serialize access lock
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    16
        self.TransactionLock = Lock()
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    17
        # open serial port
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    18
#        self.serialPort = serial.Serial( port, rate, timeout = timeout )
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    19
        # Debugging serial stuff
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    20
        self._serialPort = serial.Serial( port, rate, timeout = timeout )
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    21
        class myser:
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    22
            def read(self_,cnt):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    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
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    29
                return res
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    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
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    35
                self._serialPort.write(str)
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    36
            def flush(self_):
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    37
                self._serialPort.flush()
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    38
        self.serialPort = myser()
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    39
        # start with empty
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    40
        self.serialPort.flush()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    41