name : protocol_loop.cpython-39.pyc
a

wW�W�'�@s ddlZddlZddlZzddlZWneyBddlmZYn0zddlZWneyjddlZYn0ddl	m
Z
mZmZm
Z
mZmZejejejejd�ZGdd�de
�Zedk�rddlZed�Zej�d�e��ej�d	�e�d
�e��ej�d�e�d���e��dS)
�N)�
SerialBase�SerialException�to_bytes�	iterbytes�writeTimeoutError�portNotOpenError)�debug�infoZwarning�errorcs�eZdZdZdZ�fdd�Zdd�Z�fdd�Zd	d
�Zdd�Z	e
d
d��Zd*dd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Ze
d"d#��Ze
d$d%��Ze
d&d'��Ze
d(d)��Z�ZS)+�SerialzSSerial port implementation that simulates a loop back connection in plain software.)�2�K�n����i,iXi�ii`	i�i�%iKi�i�i�cs2d|_d|_d|_d|_tt|�j|i|��dS)NiF)�buffer_size�queue�logger�
_cancel_write�superr�__init__)�self�args�kwargs��	__class__��C/usr/lib/python3.9/site-packages/serial/urlhandler/protocol_loop.pyr-s
zSerial.__init__cCs~|jrtd��d|_t�|j�|_|jdur4td��|�|j�|�	�d|_|j
s\|��|jsj|�
�|��|��dS)zx        Open port with current settings. This may throw a SerialException
        if the port cannot be opened.
        zPort is already open.Nz.Port must be configured before it can be used.T)�is_openrrr�QueuerZ_port�from_url�port�_reconfigure_portZ_dsrdtr�_update_dtr_stateZ_rtscts�_update_rts_state�reset_input_buffer�reset_output_buffer�rrrr�open4s
zSerial.opencsD|jr2d|_z|j�d�Wntjy0Yn0tt|���dS)NF)rr�
put_nowaitZFullrr�closer(rrrr+OszSerial.closecCsLt|jtj�r&d|jkr$dks6ntd�|j���|jrH|j�d�dS)zx        Set communication parameters on opened port. For the loop://
        protocol all settings are ignored!
        rlzinvalid baudrate: {!r}z_reconfigure_port()N)�
isinstance�	_baudrate�numbersZIntegral�
ValueError�formatrr	r(rrrr#Xs&zSerial._reconfigure_portc
Cs�t�|�}|jdkr$td�|j���zlt�|jd���D]T\}}|dkr~t�	�t�
d�|_|j�t
|d�|j�d�q8td�|���q8Wn2ty�}ztd	�|���WYd
}~n
d
}~00d
S)z(extract host and port from an URL stringZloopznexpected a string in the form "loop://[?logging={debug|info|warning|error}]": not starting with loop:// ({!r})T�loggingz
pySerial.looprzenabled loggingzunknown option: {!r}zPexpected a string in the form "loop://[?logging={debug|info|warning|error}]": {}N)�urlparse�urlsplit�schemerr0�parse_qs�query�itemsr1ZbasicConfigZ	getLoggerrZsetLevel�
LOGGER_LEVELSrr/)r�url�parts�option�values�errrr!cs*

����zSerial.from_urlcCs2|js
t�|jr(|j�d�|j����|j��S)z9Return the number of bytes currently in the input buffer.zin_waiting -> {:d})rrrrr0r�qsizer(rrr�
in_waiting|s
zSerial.in_waiting�cCs�|js
t�|jdur.|jdkr.t��|j}nd}t�}|dkr�|jr�z|jj|jd�}Wn"tjy||jdkrxYq�Yn0|dur�||7}|d8}nq�|r8t��|kr8|jr�|j�	d�q�q8t
|�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)rr�_timeout�time�	bytearrayr�get�Emptyrr	�bytes)r�sizerB�data�brrr�read�s*


zSerial.readcCs|j�d�dS)N)rr*r(rrr�cancel_read�szSerial.cancel_readcCs
d|_dS)NT)rr(rrr�cancel_write�szSerial.cancel_writecCs�d|_|jst�t|�}dt|�|j}|jdurz||jkrz|j}|dkrl|jslt�t	|d��|d8}qD|jrvdSt
�t|�D]}|jj
||jd�q�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.
        Fg$@Nrg�?rA)rrrr�lenr-Z_write_timeoutrD�sleep�minrrrZput)rrJZtime_used_to_sendZ	time_leftZbyterrr�write�s 
zSerial.writecCsP|js
t�|jr|j�d�z|j��r4|j��qWntjyJYn0dS)z9Clear input buffer, discarding all that is in the buffer.zreset_input_buffer()N�rrrr	rr>Z
get_nowaitrGr(rrrr&�s
zSerial.reset_input_buffercCsP|js
t�|jr|j�d�z|j��r4|j��qWntjyJYn0dS)zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zreset_output_buffer()NrSr(rrrr'�s
zSerial.reset_output_buffercCs|jr|j�d�|j��dS)z[        Set break: Controls TXD. When active, to transmitting is
        possible.
        z_update_break_state({!r})N)rr	r0Z_break_stater(rrr�_update_break_state�szSerial._update_break_statecCs|jr|j�d�|j��dS)z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)rr	r0�
_rts_stater(rrrr%�szSerial._update_rts_statecCs|jr|j�d�|j��dS)z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRN�rr	r0Z
_dtr_stater(rrrr$�szSerial._update_dtr_statecCs*|js
t�|jr$|j�d�|j��|jS)z(Read terminal status line: Clear To SendzCTS -> state of RTS ({!r}))rrrr	r0rUr(rrr�cts�s
z
Serial.ctscCs |jr|j�d�|j��|jS)z)Read terminal status line: Data Set ReadyzDSR -> state of DTR ({!r})rVr(rrr�dsr�sz
Serial.dsrcCs |js
t�|jr|j�d�dS)z)Read terminal status line: Ring Indicatorzreturning dummy for RIF�rrrr	r(rrr�ris
z	Serial.ricCs |js
t�|jr|j�d�dS)z)Read terminal status line: Carrier Detectzreturning dummy for CDTrYr(rrr�cds
z	Serial.cd)r@)�__name__�
__module__�__qualname__�__doc__Z	BAUDRATESrr)r+r#r!�propertyr?rLrMrNrRr&r'rTr%r$rWrXrZr[�
__classcell__rrrrr's4	


!


r�__main__zloop://z{}
z	write...
zhello
zread: {!r}
�)r1r.rDr2�ImportError�urllib.parse�parserr Zserial.serialutilrrrrrr�DEBUG�INFOZWARNINGZERRORr8rr\�sys�s�stdoutrRr0�flushrLr+rrrr�<module>s6 �w



© 2025 Cubjrnet7