name : protocol_socket.cpython-39.pyc
a

=Y27�@sddlZddlZddlZddlZddlZzddlZWneyRddlmZYn0ddl	m
Z
mZmZm
Z
mZmZejejejejd�ZdZGdd�de
�Zedk�rddlZed�Zej�d	�e��ej�d
�e�d�e��ej�d�e�d���e��dS)
�N)�
SerialBase�SerialException�to_bytes�portNotOpenError�writeTimeoutError�Timeout)�debug�infoZwarning�error�c@s�eZdZdZdZdd�Zdd�Zdd�Zd	d
�Ze	dd��Z
d*dd�Zdd�Zdd�Z
dd�Zd+dd�Zdd�Zdd�Zdd�Ze	dd ��Ze	d!d"��Ze	d#d$��Ze	d%d&��Zd'd(�Zd)S),�Serialz-Serial port implementation for plain sockets.)�2�K�n����i,iXi�ii`	i�i�%iKi�i�i�c
Cs�d|_|jdurtd��|jr&td��ztj|�|j�td�|_	Wn<t
y~}z$d|_	td�|j|���WYd}~n
d}~00|j	�d�|�
�d|_|js�|��|js�|��|��|��dS)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.zPort is already open.)�timeoutzCould not open port {}: {}FT)�loggerZ_portr�is_open�socketZcreate_connection�from_urlZportstr�POLL_TIMEOUT�_socket�	Exception�formatZsetblocking�_reconfigure_portZ_dsrdtr�_update_dtr_stateZ_rtscts�_update_rts_state�reset_input_buffer�reset_output_buffer)�self�msg�r#�E/usr/lib/python3.9/site-packages/serial/urlhandler/protocol_socket.py�open1s&
(zSerial.opencCs(|jdurtd��|jr$|j�d�dS)zz        Set communication parameters on opened port. For the socket://
        protocol all settings are ignored!
        NzCan only operate on open portsz!ignored port configuration change)rrrr	�r!r#r#r$rOs
zSerial._reconfigure_portcCsP|jrL|jr<z|j�tj�|j��WnYn0d|_d|_t�d�dS)z
Close portNFg333333�?)rrZshutdownrZ	SHUT_RDWR�close�time�sleepr&r#r#r$r'YszSerial.closec
Cs�t�|�}|jdkr$td�|j���z�t�|jd���D]T\}}|dkr~t�	�t�
d�|_|j�t
|d�|j�d�q8td�|���q8d|jkr�d	ks�ntd
��Wn2ty�}ztd�|���WYd}~n
d}~00|j|jfS)
z(extract host and port from an URL stringrzexpected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": not starting with socket:// ({!r})T�loggingzpySerial.socketrzenabled loggingzunknown option: {!r}izport not in range 0...65535z_expected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": {}N)�urlparse�urlsplit�schemerr�parse_qs�query�itemsr*ZbasicConfigZ	getLoggerrZsetLevel�
LOGGER_LEVELSr�
ValueError�port�hostname)r!�url�parts�option�values�er#r#r$rhs0

����zSerial.from_urlcCs,|js
t�t�|jgggd�\}}}t|�S)z9Return the number of bytes currently in the input buffer.r)rr�selectr�len)r!ZlrZlwZlxr#r#r$�
in_waiting�szSerial.in_waiting�c
CsB|js
t�t�}t|j�}t|�|k�r:zVt�|jggg|���\}}}|sRW�q:|j�	|t|��}|srt
d��|�|�Wn�ty�}z8|j
t
jt
jt
jt
jt
jfvr�t
d�|���WYd}~nfd}~0tjtjf�y*}z<|dt
jt
jt
jt
jt
jfv�rt
d�|���WYd}~n
d}~00|��r�q:qt|�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.
        zsocket disconnected�read failed: {}Nr)rr�	bytearrayr�_timeoutr;r:r�	time_left�recvr�extend�OSError�errno�EAGAIN�EALREADY�EWOULDBLOCK�EINPROGRESS�EINTRrr
r�expired�bytes)r!�size�readr�ready�_�bufr9r#r#r$rN�s,
""$zSerial.readc

Cs�|js
t�t|�}t|�}}t|j�}|dk�r�z�|j�|�}|jrL|WS|j	s�|�
�r^t�t�g|jgg|�
��\}}}|s�t�n6|�
�dus�J�t�g|jggd�\}}}|s�td��||d�}||8}Wn�ty��Yn�t�y8}	z:|	jtjtjtjtjtjfv�r$td�|	���WYd}	~	n`d}	~	0tj�y�}	z<|	dtjtjtjtjtjfv�rztd�|	���WYd}	~	n
d}	~	00|js(|�
�r(t�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.
        rNzwrite failed (select)zwrite failed: {})rrrr;rZ_write_timeoutr�sendZis_non_blockingZis_infiniterKrr:rArrDrErFrGrHrIrJrr
)
r!�data�dZtx_len�lengthr�nrPrOr9r#r#r$�write�sB

 ""$zSerial.writec
Cs�|js
t�d}|r�t�|jgggd�\}}}z|j�d�Wqty�}z8|jtjtjtj	tj
tjfvrxtd�
|���WYd}~qd}~0tjtjfy�}z:|dtjtjtj	tj
tjfvr�td�
|���WYd}~qd}~00qdS)z9Clear input buffer, discarding all that is in the buffer.Trir>N)rrr:rrBrDrErFrGrHrIrJrrr
r)r!rOrPr9r#r#r$r�s" zSerial.reset_input_buffercCs |js
t�|jr|j�d�dS)zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zignored reset_output_bufferN�rrrr	r&r#r#r$r szSerial.reset_output_buffer��?cCs&|js
t�|jr"|j�d�|��dS)za        Send break condition. Timed, returns to idle state after given
        duration.
        zignored send_break({!r})N)rrrr	r)r!Zdurationr#r#r$�
send_breakszSerial.send_breakcCs|jr|j�d�|j��dS)zJSet break: Controls TXD. When active, to transmitting is
        possible.z!ignored _update_break_state({!r})N)rr	rZ_break_stater&r#r#r$�_update_break_stateszSerial._update_break_statecCs|jr|j�d�|j��dS)z)Set terminal status line: Request To Sendzignored _update_rts_state({!r})N)rr	rZ
_rts_stater&r#r#r$r"szSerial._update_rts_statecCs|jr|j�d�|j��dS)z-Set terminal status line: Data Terminal Readyzignored _update_dtr_state({!r})N)rr	rZ
_dtr_stater&r#r#r$r'szSerial._update_dtr_statecCs |js
t�|jr|j�d�dS)z(Read terminal status line: Clear To Sendzreturning dummy for ctsTrXr&r#r#r$�cts,s
z
Serial.ctscCs |js
t�|jr|j�d�dS)z)Read terminal status line: Data Set Readyzreturning dummy for dsrTrXr&r#r#r$�dsr5s
z
Serial.dsrcCs |js
t�|jr|j�d�dS)z)Read terminal status line: Ring Indicatorzreturning dummy for riFrXr&r#r#r$�ri>s
z	Serial.ricCs |js
t�|jr|j�d�dS)z)Read terminal status line: Carrier Detectzreturning dummy for cd)TrXr&r#r#r$�cdGs
z	Serial.cdcCs
|j��S)z@Get the file handle of the underlying socket for use with select)r�filenor&r#r#r$r`Ssz
Serial.filenoN)r=)rY)�__name__�
__module__�__qualname__�__doc__Z	BAUDRATESr%rr'r�propertyr<rNrWrr rZr[rrr\r]r^r_r`r#r#r#r$r+s2


)5






r�__main__zsocket://localhost:7000z{}
z	write...
shello
z	read: {}
) rEr*r:rr(r+�ImportError�urllib.parse�parseZserial.serialutilrrrrrr�DEBUG�INFOZWARNINGZERRORr1rrra�sys�s�stdoutrWr�flushrNr'r#r#r#r$�<module>s6 �1



© 2025 Cubjrnet7