
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
M]W%                 @   s"  d dl Z d dlZd dlZyd dlZW n ek
rB   d dljZY nX yd dlZW n ek
rl   d dlZY nX 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rd dlZedZejjdje ejjd	 ejd
 ej  ejjdjejd ej  dS )    N)
SerialBaseSerialExceptionto_bytes	iterbyteswriteTimeoutErrorportNotOpenError)debuginfoZwarningerrorc                   s   e Zd ZdZd6Z fddZdd Z fddZdd Zdd Z	e
dd Zd7d d!Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Ze
d.d/ Ze
d0d1 Ze
d2d3 Ze
d4d5 Z  ZS )8SerialzSSerial port implementation that simulates a loop back connection in plain software.2   K   n            ,  X      `	    %   K          c                s(   t t| j|| d| _d | _d | _d S )Ni   )superr   __init__buffer_sizequeuelogger)selfargskwargs)	__class__ #/usr/lib/python3.6/protocol_loop.pyr   -   s    zSerial.__init__c             C   s~   | j rtdd| _tj| j| _| jdkr4td| j| j | j	  d| _ | j
s\| j  | jsj| j  | j  | j  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'   open3   s    
zSerial.openc                sF   | j r4d| _ y| jjd  W n tjk
r2   Y nX tt| j  d S )NF)r(   r    Z
put_nowaitZFullr   r   close)r"   )r%   r&   r'   r1   N   s    zSerial.closec             C   sR   t | jtj s,d| j  k o$dk n   r<tdj| j| jrN| jjd dS )zx        Set communication parameters on opened port. For the loop://
        protocol all settings are ignored!
        r          zinvalid baudrate: {!r}z_reconfigure_port()Nl        )
isinstance	_baudratenumbersZIntegral
ValueErrorformatr!   r	   )r"   r&   r&   r'   r+   W   s    ,zSerial._reconfigure_portc             C   s   t j|}|jdkr$tdj|jypxjt j|jdj D ]T\}}|dkrtj	  tj
d| _| jjt|d   | jjd q:tdj|q:W W n0 tk
r } ztd	j|W Y d
d
}~X nX d
S )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urlsplitschemer   r8   Zparse_qsZqueryitemsr9   ZbasicConfigZ	getLoggerr!   ZsetLevelLOGGER_LEVELSr   r7   )r"   ZurlpartsZoptionvalueser&   r&   r'   r*   b   s"    


zSerial.from_urlc             C   s2   | j s
t| jr(| 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   r8   r    qsize)r"   r&   r&   r'   
in_waiting{   s
    zSerial.in_waiting   c             C   s   | j s
t| jdk	r.| jdkr.tj | j }nd}t }x|dkr| j ry| jj| jd}W n" tjk
r~   | jdkrzP Y nX |dk	r||7 }|d8 }nP |r:tj |kr:| jr| jj	d P q:W 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   )timeoutrC   zread timeout)r(   r   Z_timeouttime	bytearrayr    getEmptyr!   r	   bytes)r"   sizerD   databr&   r&   r'   read   s*    

zSerial.readc             C   st   | j s
tt|}dt| | j }| jdk	rH|| jkrHtj| j tx"t	|D ]}| j
j|| jd qRW 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.
        g      $@N)rD   )r(   r   r   lenr5   Z_write_timeoutrE   Zsleepr   r   r    Zput)r"   rK   Ztime_used_to_sendZbyter&   r&   r'   write   s    zSerial.writec             C   sV   | j s
t| jr| jjd yx| jj r6| jj  q W W n tjk
rP   Y nX dS )z9Clear input buffer, discarding all that is in the buffer.zreset_input_buffer()N)r(   r   r!   r	   r    rA   
get_nowaitrH   )r"   r&   r&   r'   r.      s    zSerial.reset_input_bufferc             C   sV   | j s
t| jr| jjd yx| jj r6| jj  q W W n tjk
rP   Y nX dS )zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zreset_output_buffer()N)r(   r   r!   r	   r    rA   rP   rH   )r"   r&   r&   r'   r/      s    zSerial.reset_output_bufferc             C   s   | j r| j jdj| j dS )z[        Set break: Controls TXD. When active, to transmitting is
        possible.
        z_update_break_state({!r})N)r!   r	   r8   Z_break_state)r"   r&   r&   r'   _update_break_state   s    zSerial._update_break_statec             C   s   | j r| j jdj| j dS )z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)r!   r	   r8   
_rts_state)r"   r&   r&   r'   r-      s    zSerial._update_rts_statec             C   s   | j r| j jdj| j dS )z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRN)r!   r	   r8   
_dtr_state)r"   r&   r&   r'   r,      s    zSerial._update_dtr_statec             C   s*   | j s
t| jr$| jjdj| j | jS )z(Read terminal status line: Clear To SendzCTS -> state of RTS ({!r}))r(   r   r!   r	   r8   rR   )r"   r&   r&   r'   cts   s
    z
Serial.ctsc             C   s    | j r| j jdj| j | jS )z)Read terminal status line: Data Set ReadyzDSR -> state of DTR ({!r}))r!   r	   r8   rS   )r"   r&   r&   r'   dsr   s    z
Serial.dsrc             C   s    | j s
t| jr| jjd dS )z)Read terminal status line: Ring Indicatorzreturning dummy for RIF)r(   r   r!   r	   )r"   r&   r&   r'   ri   s
    z	Serial.ric             C   s    | j s
t| jr| jjd dS )z)Read terminal status line: Carrier Detectzreturning dummy for CDT)r(   r   r!   r	   )r"   r&   r&   r'   cd  s
    z	Serial.cd)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )rC   )__name__
__module____qualname____doc__Z	BAUDRATESr   r0   r1   r+   r*   propertyrB   rM   rO   r.   r/   rQ   r-   r,   rT   rU   rV   rW   __classcell__r&   r&   )r%   r'   r   '   s(    	
!		r   __main__zloop://z{}
z	write...
zhello
zread: {!r}
   )r9   r6   rE   r:   ImportErrorZurllib.parseparser    r)   Zserial.serialutilr   r   r   r   r   r   DEBUGINFOZWARNINGZERRORr=   r   rX   syssstdoutrO   r8   flushrM   r1   r&   r&   r&   r'   <module>   s4    
 i

