
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
M]W+                 @   s  d dl Z d dlZd dlZd dlZd dlZyd dlZW n ek
rR   d dljZY nX d dl	m
Z
mZmZmZ ejejejejdZdZG dd de
Zedkrd 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portNotOpenErrorto_bytes)debuginfoZwarningerror   c               @   s   e Zd ZdZd:Zdd Z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'd(Zd)d* Zd+d, Zd-d. Ze	d/d0 Ze	d1d2 Ze	d3d4 Ze	d5d6 Zd7d8 Zd9S )=Serialz-Serial port implementation for plain sockets.2   K   n            ,  X      `	    %   K          c             C   s   d| _ | jdkrtd| jr&tdytj| j| jtd| _	W n: t
k
r| } zd| _	tdj| j|W Y dd}~X nX | j  d| _| js| j  | js| j  | j  | j  dS )zx        Open port with current settings. This may throw a SerialException
        if the port cannot be opened.
        Nz.Port must be configured before it can be used.zPort is already open.)timeoutzCould not open port {}: {}T)loggerZ_portr   is_opensocketZcreate_connectionfrom_urlZportstrPOLL_TIMEOUT_socket	Exceptionformat_reconfigure_portZ_dsrdtr_update_dtr_stateZ_rtscts_update_rts_statereset_input_bufferreset_output_buffer)selfmsg r,   %/usr/lib/python3.6/protocol_socket.pyopen0   s$    
$zSerial.openc             C   s(   | j dkrtd| jr$| jjd dS )zz        Set communication parameters on opened port. For the socket://
        protocol all settings are ignored!
        NzCan only operate on open portsz!ignored port configuration change)r"   r   r   r   )r*   r,   r,   r-   r%   L   s    
zSerial._reconfigure_portc          	   C   sP   | j rL| jr<y| jjtj | jj  W n   Y nX d| _d| _ tjd dS )z
Close portNFg333333?)r   r"   Zshutdownr   Z	SHUT_RDWRclosetimeZsleep)r*   r,   r,   r-   r/   V   s    zSerial.closec             C   s   t j|}|jdkr$tdj|jyxjt 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 d|j  kod	k n  std
W n0 tk
r } ztdj|W Y dd}~X nX |j|jfS )z(extract host and port from an URL stringr   zexpected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": not starting with socket:// ({!r})TloggingzpySerial.socketr   zenabled loggingzunknown option: {!r}i   zport not in range 0...65535z_expected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": {}N)urlparseZurlsplitschemer   r$   Zparse_qsZqueryitemsr1   ZbasicConfigZ	getLoggerr   ZsetLevelLOGGER_LEVELSr   
ValueErrorZportZhostname)r*   ZurlpartsZoptionvalueser,   r,   r-   r    e   s(    


zSerial.from_urlc             C   s,   | j s
ttj| jgg g d\}}}t|S )z9Return the number of bytes currently in the input buffer.r   )r   r   selectr"   len)r*   ZlrZlwZlxr,   r,   r-   
in_waiting   s    zSerial.in_waiting   c       	      C   st  | j s
tt }| j}xRt||k rjyxtj }tj| jgg g |\}}}|sRP | jj|t| }|srt	d|j
| |dk	r|tj | 8 }|dkrP W q tjk
r   Y q tjk
r } zt	dj|W Y dd}~X q tk
r$ } z"|jtjkrt	dj|W Y dd}~X q tjk
rf } z$|d tjkrVt	dj|W Y dd}~X qX 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.
        zsocket disconnectedNr   zconnection failed ({})zread failed: {})r   r   	bytearrayZ_timeoutr;   r0   r:   r"   Zrecvr   extendr   r   r   r$   OSErrorerrnoZEAGAINbytes)	r*   sizereadr   Z
start_timeZready_Zbufr9   r,   r,   r-   rD      s:    
$zSerial.readc             C   sZ   | j s
ty| jjt| W n2 tjk
rP } ztdj|W Y dd}~X nX 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.
        zsocket connection failed: {}N)
r   r   r"   Zsendallr   r   r   r   r$   r;   )r*   datar9   r,   r,   r-   write   s     zSerial.writec             C   s    | j s
t| jr| jjd dS )z9Clear input buffer, discarding all that is in the buffer.zignored reset_input_bufferN)r   r   r   r   )r*   r,   r,   r-   r(      s    zSerial.reset_input_bufferc             C   s    | j s
t| jr| jjd dS )zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zignored reset_output_bufferN)r   r   r   r   )r*   r,   r,   r-   r)      s    zSerial.reset_output_buffer      ?c             C   s&   | j s
t| jr"| jjdj| dS )za        Send break condition. Timed, returns to idle state after given
        duration.
        zignored send_break({!r})N)r   r   r   r   r$   )r*   Zdurationr,   r,   r-   
send_break   s    zSerial.send_breakc             C   s   | j r| j jdj| j dS )zJSet break: Controls TXD. When active, to transmitting is
        possible.z!ignored _update_break_state({!r})N)r   r   r$   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ignored _update_rts_state({!r})N)r   r   r$   Z
_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ignored _update_dtr_state({!r})N)r   r   r$   Z
_dtr_state)r*   r,   r,   r-   r&      s    zSerial._update_dtr_statec             C   s    | j s
t| jr| jjd dS )z(Read terminal status line: Clear To Sendzreturning dummy for ctsT)r   r   r   r   )r*   r,   r,   r-   cts   s
    z
Serial.ctsc             C   s    | j s
t| jr| jjd dS )z)Read terminal status line: Data Set Readyzreturning dummy for dsrT)r   r   r   r   )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 cd)T)r   r   r   r   )r*   r,   r,   r-   cd  s
    z	Serial.cdc             C   s
   | j j S )z@Get the file handle of the underlying socket for use with select)r"   fileno)r*   r,   r,   r-   rO   !  s    zSerial.filenoN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r=   )rH   )__name__
__module____qualname____doc__Z	BAUDRATESr.   r%   r/   r    propertyr<   rD   rG   r(   r)   rI   rJ   r'   r&   rK   rL   rM   rN   rO   r,   r,   r,   r-   r
   *   s*    

1


			r
   __main__zsocket://localhost:7000z{}
z	write...
s   hello
z	read: {}
)rA   r1   r:   r   r0   r2   ImportErrorZurllib.parseparseZserial.serialutilr   r   r   r   DEBUGINFOZWARNINGZERRORr5   r!   r
   rP   syssstdoutrG   r$   flushrD   r/   r,   r,   r,   r-   <module>   s4   
   
