Package Gnumed :: Package pycommon :: Module gmSerialTools
[frames] | no frames]

Source Code for Module Gnumed.pycommon.gmSerialTools

  1  """GNUmed serial port tools. 
  2   
  3  These functions are complementing pySerial. 
  4   
  5  @license: GPL v2 or later (details at http://www.gnu.org) 
  6  @copyright: author 
  7  """ 
  8  #=========================================================================== 
  9  # $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/pycommon/gmSerialTools.py,v $ 
 10  # $Id: gmSerialTools.py,v 1.3 2008-01-30 14:05:31 ncq Exp $ 
 11  __version__ = "$Revision: 1.3 $" 
 12  __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>" 
 13  __licence__ = "GPL v2 or later (details at http://www.gnu.org)" 
 14   
 15  import time, string, logging 
 16   
 17   
 18  _log = logging.getLogger('gm.serial') 
 19  _log.info(__version__) 
 20  #---------------------------------------------------- 
 21  # general utility 
 22  #---------------------------------------------------- 
23 -def wait_for_str(aDrv = None, aString = '', aTimeout = 2500, max_bytes = 2048):
24 """Wait for a particular string with timeout. 25 26 - timeout in milliseconds, please 27 """ 28 if aString == '': 29 return (1, '') 30 31 if aDrv is None: 32 _log.error("need source for incoming data") 33 return (0, '') 34 35 if max_bytes < len(aString): 36 max_bytes = len(aString) + 1 37 38 rxd = '' 39 loop = 0 40 slice = 100 41 # how many loops ? 42 max_loops = abs(aTimeout / slice) 43 # wait for data 44 while loop < max_loops: 45 loop += 1 46 # something there 47 if aDrv.inWaiting() > 0: 48 # get all there is 49 while aDrv.inWaiting() > 0: 50 rxd = rxd + aDrv.read(size = 1) 51 # did this contain our expected string already ? 52 if string.find(rxd, aString) > -1: 53 return (1, rxd) 54 # did we exceed our character buffer limit ? 55 # this stops runaway serial ports 56 if len(rxd) >= max_bytes: 57 _log.error('exceeded maximum # of bytes (%s) to receive' % max_bytes) 58 return (0, rxd) 59 # nothing there, wait a slice 60 else: 61 if len(rxd) >= max_bytes: 62 _log.error('exceeded maximum # of bytes to receive') 63 return (0, rxd) 64 time.sleep(float(slice) / 1000) 65 66 # hm, waited for aTimeout but expected string not received 67 _log.warning('wait for [%s] timed out after %s ms', aString, aTimeout) 68 _log.debug(rxd) 69 return (0, rxd)
70 #--------------------------------------------------------
71 -def wait_for_data(aDrv = None, aTimeout = 2500):
72 """Wait for any incoming with timeout. 73 74 - timeout in milliseconds, please 75 """ 76 if aDrv is None: 77 _log.error("Need source for incoming data !") 78 return 0 79 80 loop = 0 81 slice = 100 82 # how many loops ? 83 max_loops = abs(aTimeout / slice) 84 # wait for data 85 while loop < max_loops: 86 # nothing there, wait a slice 87 if aDrv.inWaiting() == 0: 88 loop += 1 89 time.sleep(float(slice) / 1000) 90 else: 91 return 1 92 93 # hm, waited for aTimeout but expected string not received 94 _log.warning('Timed out after %s ms while waiting for data.' % aTimeout) 95 return 0
96 #======================================================== 97 # $Log: gmSerialTools.py,v $ 98 # Revision 1.3 2008-01-30 14:05:31 ncq 99 # - std lib logging 100 # 101 # Revision 1.2 2004/12/23 16:19:34 ncq 102 # - add licence 103 # 104 # Revision 1.1 2004/02/25 09:30:13 ncq 105 # - moved here from python-common 106 # 107 # Revision 1.3 2003/11/21 15:59:47 ncq 108 # - some cleanup 109 # 110 # Revision 1.2 2003/11/19 17:59:49 ncq 111 # - slice must be float()ed to support sub-second slices 112 # 113 # Revision 1.1 2003/11/19 17:35:28 ncq 114 # - initial check in 115 # 116