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info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   _portfrom_urlport_reconfigure_port_dsrdtr_update_dtr_state_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_nowaitFullr   r   closer-   r   r   r    r1   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numbers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 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   r7   parse_qsqueryitemsr9   basicConfig	getLoggerr   setLevelLOGGER_LEVELSr	   r6   )r   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	   r7   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   timeoutrN   zread timeout)r!   r   _timeouttime	bytearrayr   getEmptyr   r
   bytes)r   sizerP   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 timeoutrO   )r   r!   r   r   lenr3   _write_timeoutrR   sleepminr   r   r   put)r   rX   time_used_to_send	time_left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   rK   
get_nowaitrU   r-   r   r   r    r+      s   

zSerial.reset_input_bufferc                 C   rf   )zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zreset_output_buffer()Nrg   r-   r   r   r    r,      s   

zSerial.reset_output_bufferc                 C   rI   )z0Return how many bytes the in the outgoing bufferzout_waiting -> {:d}rJ   r-   r   r   r    out_waiting   rM   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
   r7   _break_stater-   r   r   r    _update_break_state   s   zSerial._update_break_statec                 C   rj   )z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)r   r
   r7   
_rts_stater-   r   r   r    r*         zSerial._update_rts_statec                 C   rj   )z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRNr   r
   r7   
_dtr_stater-   r   r   r    r(      rn   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
   r7   rm   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})ro   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   rs   )z)Read terminal status line: Carrier Detectzreturning dummy for CDTrt   r-   r   r   r    cd  rv   z	Serial.cd)rN   )__name__
__module____qualname____doc__	BAUDRATESr   r.   r1   r&   r$   propertyrL   rZ   r[   r\   re   r+   r,   ri   rl   r*   r(   rq   rr   ru   rw   __classcell__r   r   r   r    r   )   s:    	


!




r   __main__zloop://z{}
z	write...
zhello
zread: {!r}
   )!
__future__r   r9   r4   rR   r:   ImportErrorurllib.parseparser   r"   serial.serialutilr   r   r   r   r   r   DEBUGINFOWARNINGERRORrC   r   rx   syssstdoutre   r7   flushrZ   r1   r   r   r   r    <module>   sB      
