connectors/LPC/LPCProto.py
changeset 571 427bf9130d12
parent 563 c74a37d156df
child 576 7fcdc0d3d8d9
equal deleted inserted replaced
570:46abd6b2f639 571:427bf9130d12
     1 import serial
     1 import serial
     2 import exceptions
     2 import exceptions
     3 from threading import Lock
     3 from threading import Lock
       
     4 import time
     4 
     5 
     5 class LPCProtoError(exceptions.Exception):
     6 class LPCProtoError(exceptions.Exception):
     6         """Exception class"""
     7         """Exception class"""
     7         def __init__(self, msg):
     8         def __init__(self, msg):
     8                 self.msg = msg
     9                 self.msg = msg
    12 
    13 
    13 class LPCProto:
    14 class LPCProto:
    14     def __init__(self, port, rate, timeout):
    15     def __init__(self, port, rate, timeout):
    15         # serialize access lock
    16         # serialize access lock
    16         self.TransactionLock = Lock()
    17         self.TransactionLock = Lock()
    17         # open serial port
    18         if BMZ_DBG:
    18 #        self.serialPort = serial.Serial( port, rate, timeout = timeout )
    19             # Debugging serial stuff
    19         # Debugging serial stuff
    20             self._serialPort = serial.Serial( port, rate, timeout = timeout, writeTimeout = timeout )
    20         self.serialPort = serial.Serial( port, rate, timeout = timeout )
    21             class myser:
    21 #        class myser:
    22                 def readline(self_):
    22 #            def read(self_,cnt):
    23                     res = self._serialPort.readline() 
    23 #                res = self._serialPort.read(cnt)
    24                     print 'Recv :"', res, '"' 
    24 #                if len(res) > 16:
    25                     return res
    25 #                    print "Recv :", map(hex,map(ord,res[:16])), "[...]"
    26 
    26 #                else:
    27                 def read(self_,cnt):
    27 #                    print "Recv :", map(hex,map(ord,res))
    28                     res = self._serialPort.read(cnt)
    28 #                    
    29                     if len(res) > 16:
    29 #                return res
    30                         print "Recv :", map(hex,map(ord,res[:16])), "[...]"
    30 #            def write(self_, str):
    31                     else:
    31 #                if len(str) > 16:
    32                         print "Recv :", map(hex,map(ord,res))
    32 #                    print "Send :", map(hex,map(ord,str[:16])), "[...]"
    33                         
    33 #                else:
    34                     return res
    34 #                    print "Send :", map(hex,map(ord,str))
    35                 def write(self_, string):
    35 #                self._serialPort.write(str)
    36                     lstr=len(string)
    36 #            def flush(self_):
    37                     if lstr > 16:
    37 #                self._serialPort.flush()
    38                         print "Send :", map(hex,map(ord,string[:16])), "[...]"
    38 #        self.serialPort = myser()
    39                     else:
    39         # start with empty
    40                         print "Send :", map(hex,map(ord,string))
       
    41                     return self._serialPort.write(string)
       
    42                     # while len(string)>0:
       
    43                     #     i = self._serialPort.write(string[:4096])
       
    44                     #     print ".",
       
    45                     #     string = string[i:]
       
    46                     # print
       
    47                     #return lstr
       
    48                 def flush(self_):
       
    49                     return self._serialPort.flush()
       
    50                 def close(self_):
       
    51                     self._serialPort.close()
       
    52             self.serialPort = myser()
       
    53         else:
       
    54             # open serial port
       
    55             self.serialPort = serial.Serial( port, rate, timeout = timeout )
       
    56         # start with empty buffer
    40         self.serialPort.flush()
    57         self.serialPort.flush()
    41     
    58     
    42     def __del__(self):
    59     def __del__(self):
    43         if self.serialPort:
    60         if self.serialPort:
    44             self.serialPort.close()
    61             self.serialPort.close()