ó
*­„Jc           @   sX  d  d l  Td  d l Z d  d l Z d  d l Z i e j d 6e j d 6e j d 6e j d 6Z d e	 f d „  ƒ  YZ
 y d  d l Z Wn* e k
 r® d	 e
 e f d
 „  ƒ  YZ n Xd	 e
 e j f d „  ƒ  YZ e d k rTd  d l Z e d ƒ Z e j j d e ƒ e j j d ƒ e j d ƒ e j ƒ  e j j d e j d ƒ ƒ e j ƒ  n  d S(   iÿÿÿÿ(   t   *Nt   debugt   infot   warningt   errort   LoopbackSerialc           B   sÅ   e  Z d  Z d& Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d „ Z
 d „  Z d „  Z d „  Z d d „ Z e d „ Z e d  „ Z e d! „ Z d" „  Z d# „  Z d$ „  Z d% „  Z RS('   s-   Serial port implementation for plain sockets.i2   iK   in   i†   i–   iÈ   i,  iX  i°  i  i`	  iÀ  i€%  i K  i –  i á  i Â c         C   sµ   d |  _ t j ƒ  |  _ t ƒ  |  _ t |  _ t |  _	 |  j
 d k rT t d ƒ ‚ n  |  j |  j ƒ |  j ƒ  t |  _ |  j s |  j t ƒ |  j t ƒ n  |  j ƒ  |  j ƒ  d S(   sj   Open port with current settings. This may throw a SerialException
           if the port cannot be opened.s.   Port must be configured before it can be used.N(   t   Nonet   loggert	   threadingt   Lockt   buffer_lockt	   bytearrayt   loop_buffert   Falset   ctst   dsrt   _portt   SerialExceptiont   fromURLt   portt   _reconfigurePortt   Truet   _isOpent   _rtsctst   setRTSt   setDTRt
   flushInputt   flushOutput(   t   self(    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   open&   s    			
		
c         C   so   t  |  j t t f ƒ s9 d |  j k  o3 d k  n rO t d |  j ƒ ‚ n  |  j rk |  j j d ƒ n  d S(   sg   Set communication parameters on opened port. for the loop://
        protocol all settings are ignored!i    i   i    s   invalid baudrate: %rs   _reconfigurePort()Nl        (   t
   isinstancet	   _baudratet   intt   longt
   ValueErrorR   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyR   ?   s    9	c         C   s&   |  j  r" t |  _  t j d ƒ n  d S(   s
   Close portg333333Ó?N(   R   R   t   timet   sleep(   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   closeH   s    		c         C   s   t  d ƒ ‚ d  S(   Ns2   there is no sensible way to turn numbers into URLs(   R   (   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   makeDeviceNameO   s    c         C   s  | j  ƒ  j d ƒ r" | d } n  y¶ x¯ | j d ƒ D]ž } d | k rb | j d d ƒ \ } } n d } | sq q5 | d k rÀ t j ƒ  t j d ƒ |  _ |  j j t	 | ƒ |  j j
 d ƒ q5 t d	 | f ƒ ‚ q5 WWn# t k
 rý } t d
 | ƒ ‚ n Xd S(   s(   extract host and port from an URL strings   loop://i   t   /t   =i   t   loggings   pySerial.loops   enabled loggings   unknown option: %rsA   expected a string in the form "[loop://][option[/option...]]": %sN(   t   lowert
   startswitht   splitR   R)   t   basicConfigt	   getLoggerR   t   setLevelt   LOGGER_LEVELSR   R"   R   (   R   t   urlt   optiont   valuet   e(    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyR   R   s"     
c         C   sK   |  j  s t ‚ n  |  j r> |  j j d t |  j ƒ f ƒ n  t |  j ƒ S(   s>   Return the number of characters currently in the input buffer.s   inWaiting() -> %d(   R   t   portNotOpenErrorR   R   t   lenR   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt	   inWaitingj   s
    	 		#i   c         C   sÎ   |  j  s t ‚ n  |  j d k	 r7 t j ƒ  |  j } n d } t ƒ  } x{ t | ƒ | k  rÃ |  j j ƒ  z! t	 |  j
 |  ƒ } |  j
 | 4Wd |  j j ƒ  X| | 7} | rI t j ƒ  | k rI PqI qI Wt | ƒ S(   sÀ   Read size bytes from the serial port. If a timeout is set it may
        return less characters as requested. With no timeout it will block
        until the requested number of bytes is read.N(   R   R5   t   _timeoutR   R#   R   R6   R
   t   acquiret   to_bytesR   t   releaset   bytes(   R   t   sizet   timeoutt   datat   block(    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   reads   s     	 		
c         C   s¡   |  j  s t ‚ n  d t | ƒ |  j } |  j d k	 r` | |  j k r` t j |  j ƒ t ‚ n  |  j	 j
 ƒ  z |  j t | ƒ 7_ Wd |  j	 j ƒ  Xt | ƒ S(   sœ   Output the given string over the serial port. Can block if the
        connection is blocked. May raise SerialException if the connection is
        closed.g      $@N(   R   R5   R6   R   t   _writeTimeoutR   R#   R$   t   writeTimeoutErrorR
   R9   R   R<   R;   (   R   R?   t   time_used_to_send(    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   write‹   s    	 		c         C   s[   |  j  s t ‚ n  |  j r. |  j j d ƒ n  |  j j ƒ  z |  j 2Wd |  j j ƒ  Xd S(   s9   Clear input buffer, discarding all that is in the buffer.s   flushInput()N(   R   R5   R   R   R
   R9   R   R;   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyR   ž   s    	 		c         C   s2   |  j  s t ‚ n  |  j r. |  j j d ƒ n  d S(   sb   Clear output buffer, aborting the current output and
        discarding all that is in the buffer.s   flushOutput()N(   R   R5   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyR   ©   s    	 		g      Ð?c         C   s   |  j  s t ‚ n  d S(   sP   Send break condition. Timed, returns to idle state after given
        duration.N(   R   R5   (   R   t   duration(    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt	   sendBreak°   s    	 c         C   s9   |  j  s t ‚ n  |  j r5 |  j j d | f ƒ n  d S(   sJ   Set break: Controls TXD. When active, to transmitting is
        possible.s   setBreak(%r)N(   R   R5   R   R   (   R   t   level(    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   setBreakµ   s    	 		c         C   sB   |  j  s t ‚ n  |  j r5 |  j j d | f ƒ n  | |  _ d S(   s)   Set terminal status line: Request To Sends   setRTS(%r) -> state of CTSN(   R   R5   R   R   R   (   R   RH   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyR   ¼   s
    	 		c         C   sB   |  j  s t ‚ n  |  j r5 |  j j d | f ƒ n  | |  _ d S(   s-   Set terminal status line: Data Terminal Readys   setDTR(%r) -> state of DSRN(   R   R5   R   R   R   (   R   RH   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyR   Ã   s
    	 		c         C   s?   |  j  s t ‚ n  |  j r8 |  j j d |  j f ƒ n  |  j S(   s(   Read terminal status line: Clear To Sends   getCTS() -> state of RTS (%r)(   R   R5   R   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   getCTSÊ   s
    	 		c         C   s?   |  j  s t ‚ n  |  j r8 |  j j d |  j f ƒ n  |  j S(   s)   Read terminal status line: Data Set Readys   getDSR() -> state of DTR (%r)(   R   R5   R   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   getDSRÑ   s
    	 		c         C   s2   |  j  s t ‚ n  |  j r. |  j j d ƒ n  t S(   s)   Read terminal status line: Ring Indicators   returning dummy for getRI()(   R   R5   R   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   getRIØ   s
    	 		c         C   s2   |  j  s t ‚ n  |  j r. |  j j d ƒ n  t S(   s)   Read terminal status line: Carrier Detects   returning dummy for getCD()(   R   R5   R   R   R   (   R   (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   getCDß   s
    	 		(   i2   iK   in   i†   i–   iÈ   i,  iX  i°  i  i`	  iÀ  i€%  i K  i –  i á  i Â (   t   __name__t
   __module__t   __doc__t	   BAUDRATESR   R   R%   R&   R   R7   RA   RE   R   R   RG   R   RI   R   R   RJ   RK   RL   RM   (    (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyR       s*    														t   Serialc           B   s   e  Z RS(    (   RN   RO   (    (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyRR   ñ   s   c           B   s   e  Z RS(    (   RN   RO   (    (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyRR   õ   s   t   __main__s   socket://localhost:7000s   %s
s	   write...
s   hello
s	   read: %s
i   (   t
   serialutilR   R#   R)   t   DEBUGt   INFOt   WARNINGt   ERRORR0   t
   SerialBaseR   t   iot   ImportErrort   FileLikeRR   t	   RawIOBaseRN   t   syst   st   stdoutRE   t   flushRA   R%   (    (    (    s>   /usr/lib/python2.7/dist-packages/serial/loopback_connection.pyt   <module>   s.   



Í
