o
    Ȧ^_7                     @   s  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zd dlZW n ey/   d dl	m
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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PortNotOpenErrorSerialTimeoutExceptionTimeout)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d  Ze	d!d" Ze	d#d$ Ze	d%d& Zd'd( Zd)S ),Serialz-Serial port implementation for plain sockets.)2   K   n            i,  iX  i  i  i`	  i  i%  i K  i   i   i  c              
   C   s   d| _ | jdu rtd| jrtdztj| | jtd| _	W n t
y9 } zd| _	td| j|d}~ww | j	d |   d| _| jsN|   | jsU|   |   |   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 {}: {}FT)loggerZ_portr   is_opensocketZcreate_connectionfrom_urlZportstrPOLL_TIMEOUT_socket	ExceptionformatZsetblocking_reconfigure_portZ_dsrdtr_update_dtr_stateZ_rtscts_update_rts_statereset_input_bufferreset_output_buffer)selfmsg r$   C/usr/lib/python3/dist-packages/serial/urlhandler/protocol_socket.pyopen3   s*   
zSerial.openc                 C   s,   | j du r	td| jr| jd dS 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   Q   s
   
zSerial._reconfigure_portc                 C   sP   | j r&| jrz| jtj | j  W n   Y d| _d| _ td dS dS )z
Close portNFg333333?)r   r   Zshutdownr   Z	SHUT_RDWRclosetimesleepr'   r$   r$   r%   r(   [   s   zSerial.closec              
   C   s   t |}|jdkrtd|jzHt |jd D ])\}}|dkr?t	  t
d| _| jt|d   | jd qtd|d|j  krTd	k sYtd
 td
W n tym } ztd|d}~ww |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urlsplitZschemer   r   Zparse_qsZqueryitemsr+   ZbasicConfigZ	getLoggerr   ZsetLevelLOGGER_LEVELSr	   
ValueErrorZportZhostname)r"   Zurlpartsoptionvalueser$   r$   r%   r   j   s6   

zSerial.from_urlc                 C   s.   | j st t| 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   sR  | j st t }t| j}t||k rz-t| jgg g | \}}}|s+W t|S | j	|t| }|s;t
d|| W nT tyi } z|jtjtjtjtjtjfvr_t
d|W Y d}~n1d}~w tjtjfy } z|d tjtjtjtjtjfvrt
d|W Y d}~nd}~ww | r	 t|S t||k 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.
        zsocket disconnectedread failed: {}Nr   )r   r   	bytearrayr   Z_timeoutr5   r4   r   	time_leftrecvr   extendOSErrorerrnoEAGAINEALREADYEWOULDBLOCKEINPROGRESSEINTRr   r   r   expiredbytes)r"   sizereadr   ready_bufr3   r$   r$   r%   rG      s<   
 zSerial.readc           
   
   C   s  | j st t|}t| }}t| j}|dkrzT| j|}|jr&|W S |j	sG|
 r1tdtg | jgg | \}}}|sFtdn| du sOJ tg | jgg d\}}}|sbtd||d }||8 }W nX tyu     ty }	 z|	jtjtjtjtjtjfvrtd|	W Y d}	~	n.d}	~	w tjy }	 z|	d tjtjtjtjtjfvrtd|	W Y d}	~	nd}	~	ww |js|
 rtd|dks|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.
        r   zWrite timeoutNzwrite failed (select)zwrite failed: {})r   r   r   r5   r   Z_write_timeoutr   sendZis_non_blockingZis_infiniterD   r   r4   r:   r   r=   r>   r?   r@   rA   rB   rC   r   r   )
r"   datadZtx_lenlengthr   nrI   rH   r3   r$   r$   r%   write   sP   
 'zSerial.writec              
   C   s   | j st d}|rzt| jgg g d\}}}z
|r | jd}W nT tyI } z|jtjtjtj	tj
tjfvr?td|W Y d}~n1d}~w tjtjfyu } z|d tjtjtj	tj
tjfvrktd|W Y d}~nd}~ww |s
dS dS )z9Clear input buffer, discarding all that is in the buffer.Tr   i   r8   N)r   r   r4   r   r;   r=   r>   r?   r@   rA   rB   rC   r   r   r   r   )r"   rH   rI   r3   r$   r$   r%   r       s*    zSerial.reset_input_bufferc                 C   s&   | j st | jr| jd dS 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d| dS 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   "   | j r| j d| j dS dS )zJSet break: Controls TXD. When active, to transmitting is
        possible.z!ignored _update_break_state({!r})N)r   r
   r   Z_break_stater'   r$   r$   r%   _update_break_state  s   zSerial._update_break_statec                 C   rT   )z)Set terminal status line: Request To Sendzignored _update_rts_state({!r})N)r   r
   r   Z
_rts_stater'   r$   r$   r%   r   %     zSerial._update_rts_statec                 C   rT   )z-Set terminal status line: Data Terminal Readyzignored _update_dtr_state({!r})N)r   r
   r   Z
_dtr_stater'   r$   r$   r%   r   *  rV   zSerial._update_dtr_statec                 C   "   | j st | jr| jd dS )z(Read terminal status line: Clear To Sendzreturning dummy for ctsTrQ   r'   r$   r$   r%   cts/  
   z
Serial.ctsc                 C   rW   )z)Read terminal status line: Data Set Readyzreturning dummy for dsrTrQ   r'   r$   r$   r%   dsr8  rY   z
Serial.dsrc                 C   rW   )z)Read terminal status line: Ring Indicatorzreturning dummy for riFrQ   r'   r$   r$   r%   riA  rY   z	Serial.ric                 C   rW   )z)Read terminal status line: Carrier Detectzreturning dummy for cd)TrQ   r'   r$   r$   r%   cdJ  rY   z	Serial.cdc                 C   s
   | j  S )z@Get the file handle of the underlying socket for use with select)r   filenor'   r$   r$   r%   r]   V  s   
zSerial.filenoN)r7   )rR   )__name__
__module____qualname____doc__Z	BAUDRATESr&   r   r(   r   propertyr6   rG   rP   r    r!   rS   rU   r   r   rX   rZ   r[   r\   r]   r$   r$   r$   r%   r   -   s4    


)5






r   __main__zsocket://localhost:7000z{}
z	write...
s   hello
z	read: {}
)"Z
__future__r   r>   r+   r4   r   r)   r,   ImportErrorZurllib.parseparseZserial.serialutilr   r   r   r   r   r   DEBUGINFOZWARNINGZERRORr.   r   r   r^   syssstdoutrP   r   flushrG   r(   r$   r$   r$   r%   <module>   s>      2
