
    a_)                        d dl mZ d dlZd dlZd dlZ	 d dlZ	 d dl	Z	d dlmZmZmZmZmZmZ ej$                  ej&                  ej(                  ej*                  dZ G d de      Zedk(  rd dlZ ed      Zej6                  j9                  d	j;                  e             ej6                  j9                  d
       ej9                  d       ej=                          ej6                  j9                  dj;                  ej?                  d                   ejA                          yy# e$ r
 d dlmZ Y w xY w# e$ r d dl
Z	Y *w xY w)    )absolute_importN)
SerialBaseSerialExceptionto_bytes	iterbytesSerialTimeoutExceptionPortNotOpenError)debuginfowarningerrorc                        e Zd ZdZdZ fdZd Z fdZd Zd Z	e
d        Zdd	Zd
 Zd Zd Zd Zd Ze
d        Zd Zd Zd Ze
d        Ze
d        Ze
d        Ze
d        Z xZS )SerialzSSerial port implementation that simulates a loop back connection in plain software.)2   K   n            i,  iX  i  i  i`	  i  i%  i K  i   i   i  c                 d    d| _         d | _        d | _        d| _        t	        t
        |   |i | y )Ni   F)buffer_sizequeuelogger_cancel_writesuperr   __init__)selfargskwargs	__class__s      A/usr/lib/python3/dist-packages/serial/urlhandler/protocol_loop.pyr   zSerial.__init__/   s5    
"fd$d5f5    c                    | j                   rt        d      d| _        t        j                  | j
                        | _        | j                  t        d      | j                  | j                         | j                          d| _         | j                  s| j                          | j                  s| j                          | j                          | j                          y)zx        Open port with current settings. This may throw a SerialException
        if the port cannot be opened.
        zPort is already open.Nz.Port must be configured before it can be used.T)is_openr   r   r   Queuer   _portfrom_urlport_reconfigure_port_dsrdtr_update_dtr_state_rtscts_update_rts_statereset_input_bufferreset_output_bufferr   s    r!   openzSerial.open6   s    
 <<!"9::[[!1!12
::!"RSS 	dii  	 ||""$||""$!  "r"   c                     | j                   r#d| _         	 | j                  j                  d        t        t
        |           y # t        j                  $ r Y (w xY w)NF)r$   r   
put_nowaitFullr   r   close)r   r    s    r!   r5   zSerial.closeQ   sO    << DL

%%d+ 	fd!# :: s   A AAc                    t        | j                  t        j                        rd| j                  cxk  rdk  s&n t	        dj                  | j                              | j                  r| j                  j                  d       yy)zx        Set communication parameters on opened port. For the loop://
        protocol all settings are ignored!
        r   l        zinvalid baudrate: {!r}z_reconfigure_port()N)
isinstance	_baudratenumbersIntegral
ValueErrorformatr   r   r0   s    r!   r)   zSerial._reconfigure_portZ   sc     $..'*:*:;1t~~C_X_C_5<<T^^LMM;;KK23 r"   c                 r   t        j                  |      }|j                  dk7  r$t        dj	                  |j                              	 t        j
                  |j                  d      j                         D ]  \  }}|dk(  rot        j                          t        j                  d      | _        | j                  j                  t        |d             | j                  j                  d       zt        dj	                  |             y
# t        $ r}t        d	j	                  |            d
}~ww xY w)z(extract host and port from an URL stringloopznexpected a string in the form "loop://[?logging={debug|info|warning|error}]": not starting with loop:// ({!r})TloggingzpySerial.loopr   zenabled loggingzunknown option: {!r}zPexpected a string in the form "loop://[?logging={debug|info|warning|error}]": {}N)urlparseurlsplitschemer   r<   parse_qsqueryitemsr?   basicConfig	getLoggerr   setLevelLOGGER_LEVELSr
   r;   )r   urlpartsoptionvalueses         r!   r'   zSerial.from_urle   s   !!#&<<6!!&&,fU\\&:< <	P"*"3"3EKK"F"L"L"N LY&'')")"3"3O"DDKKK((vay)ABKK%%&78$%;%B%B6%JKKL  	P!EEKVAYP P	Ps   
CD 	D6D11D6c                     | j                   s
t               | j                  rB| j                  j                  dj	                  | j
                  j                                      | j
                  j                         S )z9Return the number of bytes currently in the input buffer.zin_waiting -> {:d}r$   r	   r   r
   r<   r   qsizer0   s    r!   
in_waitingzSerial.in_waiting~   s[     ||"$$;; KK299$**:J:J:LMNzz!!r"   c                    | j                   s
t               | j                  1| j                  dk7  r"t        j                         | j                  z   }nd}t	               }|dkD  r| j                   r	 | j
                  j                  | j                        }|||z  }|dz  }n	 t        |      S |rJt        j                         |kD  r3| j                  r| j                  j                  d       	 t        |      S |dkD  r| j                   rt        |      S # t
        j                  $ r | j                  dk(  rY t        |      S Y w xY w)z        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.
        Nr   timeout   zread timeout)r$   r	   _timeouttime	bytearrayr   getEmptyr   r   bytes)r   sizerU   databs        r!   readzSerial.read   s#    ||"$$==$!);iikDMM1GG{Qh4<<
JJNN4==N9
 =AIDAID T{	 499;0;;KK$$^4T{% Qh4<<$ T{ ;; ==A% T{ &s   2&D "E
Ec                 :    | j                   j                  d        y )N)r   r3   r0   s    r!   cancel_readzSerial.cancel_read   s    

d#r"   c                     d| _         y )NT)r   r0   s    r!   cancel_writezSerial.cancel_write   s
    !r"   c                    d| _         | j                  s
t               t        |      }dt	        |      z  | j
                  z  }| j                  z|| j                  kD  rk| j                  }|dkD  rB| j                   s6t        j                  t        |d             |dz  }|dkD  r| j                   s6| j                   ryt        d      t        |      D ])  }| j                  j                  || j                         + t	        |      S )z        Output the given byte string over the serial port. Can block if the
        connection is blocked. May raise SerialException if the connection is
        closed.
        Fg      $@r   g      ?zWrite timeoutrT   )r   r$   r	   r   lenr8   _write_timeoutrX   sleepminr   r   r   put)r   r^   time_used_to_send	time_leftbytes        r!   writezSerial.write   s     #||"$$~ 3t9,t~~= */@4CVCV/V++Ia-(:(:

3y#./S 	 a-(:(: !!(99dO 	>DJJNN4)<)<N=	>4yr"   c                 R   | j                   s
t               | j                  r| j                  j                  d       	 | j                  j                         r6| j                  j                          | j                  j                         r5yy# t        j                  $ r Y yw xY w)z9Clear input buffer, discarding all that is in the buffer.zreset_input_buffer()Nr$   r	   r   r   r   rQ   
get_nowaitr[   r0   s    r!   r.   zSerial.reset_input_buffer   sy    ||"$$;;KK34	**""$

%%' **""${{ 		   AB B&%B&c                 R   | j                   s
t               | j                  r| j                  j                  d       	 | j                  j                         r6| j                  j                          | j                  j                         r5yy# t        j                  $ r Y yw xY w)zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zreset_output_buffer()Nrp   r0   s    r!   r/   zSerial.reset_output_buffer   s{    
 ||"$$;;KK45	**""$

%%' **""${{ 		rr   c                     | j                   s
t               | j                  rB| j                  j                  dj	                  | j
                  j                                      | j
                  j                         S )z0Return how many bytes the in the outgoing bufferzout_waiting -> {:d}rP   r0   s    r!   out_waitingzSerial.out_waiting   s[     ||"$$;; KK3::4::;K;K;MNOzz!!r"   c                     | j                   r5| j                   j                  dj                  | j                               yy)z[        Set break: Controls TXD. When active, to transmitting is
        possible.
        z_update_break_state({!r})N)r   r   r<   _break_stater0   s    r!   _update_break_statezSerial._update_break_state   s4    
 ;;KK8??@Q@QRS r"   c                     | j                   r5| j                   j                  dj                  | j                               yy)z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)r   r   r<   
_rts_stater0   s    r!   r-   zSerial._update_rts_state   0    ;;KKFMMdoo^_ r"   c                     | j                   r5| j                   j                  dj                  | j                               yy)z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRNr   r   r<   
_dtr_stater0   s    r!   r+   zSerial._update_dtr_state   r{   r"   c                     | j                   s
t               | j                  r4| j                  j                  dj	                  | j
                               | j
                  S )z(Read terminal status line: Clear To SendzCTS -> state of RTS ({!r}))r$   r	   r   r   r<   rz   r0   s    r!   ctsz
Serial.cts  sG     ||"$$;;KK9@@QRr"   c                     | j                   r4| j                   j                  dj                  | j                               | j                  S )z)Read terminal status line: Data Set ReadyzDSR -> state of DTR ({!r})r}   r0   s    r!   dsrz
Serial.dsr  s7     ;;KK9@@QRr"   c                 ~    | j                   s
t               | j                  r| j                  j                  d       y)z)Read terminal status line: Ring Indicatorzreturning dummy for RIFr$   r	   r   r   r0   s    r!   riz	Serial.ri  s1     ||"$$;;KK56r"   c                 ~    | j                   s
t               | j                  r| j                  j                  d       y)z)Read terminal status line: Carrier Detectzreturning dummy for CDTr   r0   s    r!   cdz	Serial.cd  s1     ||"$$;;KK56r"   )rV   )__name__
__module____qualname____doc__	BAUDRATESr   r1   r5   r)   r'   propertyrR   r`   rb   rd   rn   r.   r/   ru   rx   r-   r+   r   r   r   r   __classcell__)r    s   @r!   r   r   )   s    ]4I6#6$	4P2 " "B$"6
 " "T`
`
        r"   r   __main__zloop://z{}
z	write...
zhello
zread: {!r}
   )!
__future__r   r?   r9   rX   r@   ImportErrorurllib.parseparser   r%   serial.serialutilr   r   r   r   r   r	   DEBUGINFOWARNINGERRORrI   r   r   syssstdoutrn   r<   flushr`   r5    r"   r!   <module>r      s    '   $ y x ]]LL]]	zZ zB zyAJJV]]1%&JJ\"GGIGGIJJ^**166!956GGI g  $##$  s"   D) D; )D87D8;	EE