LPCconnector/LPCProto.py
changeset 0 51f5a3138405
child 45 786b12887e91
equal deleted inserted replaced
-1:000000000000 0:51f5a3138405
       
     1 import serial
       
     2 import exceptions
       
     3 from threading import Lock
       
     4 import time
       
     5 
       
     6 class LPCProtoError(exceptions.Exception):
       
     7         """Exception class"""
       
     8         def __init__(self, msg):
       
     9                 self.msg = msg
       
    10 
       
    11         def __str__(self):
       
    12                 return "Exception in PLC protocol : " + str(self.msg)
       
    13 
       
    14 class LPCProto:
       
    15     def __init__(self, port, rate, timeout):
       
    16         # serialize access lock
       
    17         self.TransactionLock = Lock()
       
    18         if BMZ_DBG:
       
    19             # Debugging serial stuff
       
    20             self._serialPort = serial.Serial( port, rate, timeout = timeout, writeTimeout = timeout )
       
    21             class myser:
       
    22                 def readline(self_):
       
    23                     res = self._serialPort.readline() 
       
    24                     print 'Recv :"', res, '"' 
       
    25                     return res
       
    26 
       
    27                 def read(self_,cnt):
       
    28                     res = self._serialPort.read(cnt)
       
    29                     if len(res) > 16:
       
    30                         print "Recv :", map(hex,map(ord,res[:16])), "[...]"
       
    31                     else:
       
    32                         print "Recv :", map(hex,map(ord,res))
       
    33                         
       
    34                     return res
       
    35                 def write(self_, string):
       
    36                     lstr=len(string)
       
    37                     if lstr > 16:
       
    38                         print "Send :", map(hex,map(ord,string[:16])), "[...]"
       
    39                     else:
       
    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
       
    57         self.serialPort.flush()
       
    58     
       
    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