o
    a_)                     @   s.  d dl mZ d dlZd dlZd dlZzd dlZW n ey'   d dlmZ Y nw zd dl	Z	W n ey;   d dl
Z	Y nw 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 deZedkrd dlZedZejd	e ejd
 ed e  ejded e   dS dS )    )absolute_importN)
SerialBaseSerialExceptionto_bytes	iterbytesSerialTimeoutExceptionPortNotOpenError)debuginfoZwarningerrorc                       s   e Zd ZdZdZ fddZdd Z fddZd	d
 Zdd Z	e
dd Zd,ddZdd Zdd Zdd Zdd Zdd Ze
dd Zdd Zd d! Zd"d# Ze
d$d% Ze
d&d' Ze
d(d) Ze
d*d+ Z  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                    s2   d| _ d | _d | _d| _tt| j|i | d S )Ni   F)buffer_sizequeuelogger_cancel_writesuperr   __init__)selfargskwargs	__class__ A/usr/lib/python3/dist-packages/serial/urlhandler/protocol_loop.pyr   /   s
   zSerial.__init__c                 C   s~   | j rtdd| _t| j| _| jdu rtd| | j | 	  d| _ | j
s.|   | js5|   |   |   dS )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   Z_portfrom_urlZport_reconfigure_portZ_dsrdtr_update_dtr_stateZ_rtscts_update_rts_statereset_input_bufferreset_output_bufferr   r   r   r   open6   s   
zSerial.openc                    sD   | j rd| _ z| jd  W n
 tjy   Y nw tt|   d S )NF)r    r   
put_nowaitZFullr   r   closer(   r   r   r   r+   Q   s   zSerial.closec                 C   sP   t | jtjrd| j  k rdk sn td| j| jr&| jd dS dS )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numbersZIntegral
ValueErrorformatr   r
   r(   r   r   r   r#   Z   s
   &zSerial._reconfigure_portc              
   C   s   t |}|jdkrtd|jz6t |jd D ])\}}|dkr?t	  t
d| _| jt|d   | jd qtd|W d
S  ty[ } ztd	|d
}~ww )z(extract host and port from an URL stringZ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)urlparseZurlsplitZschemer   r0   Zparse_qsZqueryitemsr1   ZbasicConfigZ	getLoggerr   ZsetLevelLOGGER_LEVELSr	   r/   )r   Zurlpartsoptionvalueser   r   r   r"   e   s,   

zSerial.from_urlc                 C   4   | j st | jr| jd| j  | j S )z9Return the number of bytes currently in the input buffer.zin_waiting -> {:d}r    r   r   r	   r0   r   qsizer(   r   r   r   
in_waiting~   
   
zSerial.in_waiting   c                 C   s   | j st | jdur| jdkrt | j }nd}t }|dkrr| j rrz
| jj| jd}W n tjyB   | jdkr@Y t
|S Y nw |durP||7 }|d8 }n	 t
|S |rkt |krk| jrf| j	d 	 t
|S |dkrr| j s$t
|S )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   timeoutr>   zread timeout)r    r   Z_timeouttime	bytearrayr   getEmptyr   r
   bytes)r   sizer@   databr   r   r   read   s6   

zSerial.readc                 C   s   | j d  d S )N)r   r*   r(   r   r   r   cancel_read   s   zSerial.cancel_readc                 C   s
   d| _ d S )NT)r   r(   r   r   r   cancel_write   s   
zSerial.cancel_writec                 C   s   d| _ | js	t t|}dt| | j }| jdurF|| jkrF| j}|dkr=| j s=tt	|d |d8 }|dkr=| j r*| j rBdS t
dt|D ]}| jj|| jd qJt|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      $@Nr   g      ?zWrite timeoutr?   )r   r    r   r   lenr-   Z_write_timeoutrA   sleepminr   r   r   Zput)r   rG   Ztime_used_to_sendZ	time_leftZbyter   r   r   write   s"   zSerial.writec                 C   `   | j st | jr| jd z| j r"| j  | j sW dS W dS  tjy/   Y dS w )z9Clear input buffer, discarding all that is in the buffer.zreset_input_buffer()Nr    r   r   r
   r   r;   Z
get_nowaitrD   r(   r   r   r   r&      s   

zSerial.reset_input_bufferc                 C   rP   )zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zreset_output_buffer()NrQ   r(   r   r   r   r'      s   

zSerial.reset_output_bufferc                 C   r9   )z0Return how many bytes the in the outgoing bufferzout_waiting -> {:d}r:   r(   r   r   r   out_waiting   r=   zSerial.out_waitingc                 C   "   | j r| j d| j dS dS )z[        Set break: Controls TXD. When active, to transmitting is
        possible.
        z_update_break_state({!r})N)r   r
   r0   Z_break_stater(   r   r   r   _update_break_state   s   zSerial._update_break_statec                 C   rS   )z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)r   r
   r0   
_rts_stater(   r   r   r   r%         zSerial._update_rts_statec                 C   rS   )z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRNr   r
   r0   Z
_dtr_stater(   r   r   r   r$      rV   zSerial._update_dtr_statec                 C   s,   | j st | jr| jd| j | jS )z(Read terminal status line: Clear To SendzCTS -> state of RTS ({!r}))r    r   r   r
   r0   rU   r(   r   r   r   cts  s
   z
Serial.ctsc                 C   s    | j r| j d| j | jS )z)Read terminal status line: Data Set ReadyzDSR -> state of DTR ({!r})rW   r(   r   r   r   dsr  s   z
Serial.dsrc                 C   "   | j st | jr| jd dS )z)Read terminal status line: Ring Indicatorzreturning dummy for RIFr    r   r   r
   r(   r   r   r   ri  
   z	Serial.ric                 C   rZ   )z)Read terminal status line: Carrier Detectzreturning dummy for CDTr[   r(   r   r   r   cd  r]   z	Serial.cd)r>   )__name__
__module____qualname____doc__Z	BAUDRATESr   r)   r+   r#   r"   propertyr<   rI   rJ   rK   rO   r&   r'   rR   rT   r%   r$   rX   rY   r\   r^   __classcell__r   r   r   r   r   )   s:    	


!




r   __main__zloop://z{}
z	write...
zhello
zread: {!r}
   )!Z
__future__r   r1   r.   rA   r2   ImportErrorZurllib.parseparser   r!   Zserial.serialutilr   r   r   r   r   r   DEBUGINFOZWARNINGZERRORr4   r   r_   syssstdoutrO   r0   flushrI   r+   r   r   r   r   <module>   sB      
