connectors/LPC/LPCProto.py
author laurent
Wed, 21 Dec 2011 19:42:49 +0100
changeset 657 340c0b9caeca
parent 576 7fcdc0d3d8d9
permissions -rw-r--r--
Adding support for integrating Python code Editor into Beremiz frame.
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
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
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     5
545
627e5c636a4f Refactored LPC connector for new bootloader and application modes
Lolitech
parents: 536
diff changeset
     6
class LPCProtoError(exceptions.Exception):
448
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     7
        """Exception class"""
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     8
        def __init__(self, msg):
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
     9
                self.msg = msg
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    10
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    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
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    13
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    14
class LPCProto:
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    15
    def __init__(self, port, rate, timeout):
453
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    16
        # serialize access lock
923d036dfa90 LPC connector enhancements
edouard
parents: 448
diff changeset
    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
                    # 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
    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
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    57
        self.serialPort.flush()
8ef035de86de Some rough copy'n'paste to pave path for an LPC connector
ed
parents:
diff changeset
    58
    
563
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 545
diff changeset
    59
    def __del__(self):
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 545
diff changeset
    60
        if self.serialPort:
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 545
diff changeset
    61
            self.serialPort.close()
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 545
diff changeset
    62
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 545
diff changeset
    63
    def close(self):
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 545
diff changeset
    64
        self.serialPort.close()
c74a37d156df Better serial comm handling in LPC connector
Lolitech
parents: 545
diff changeset
    65
        self.serialPort = None