Current File : //usr/lib/python3/dist-packages/serial/urlhandler/__pycache__/protocol_socket.cpython-312.pyc
�

Ȧ^_�7���ddlmZddlZddlZddlZddlZddlZ	ddlZddlmZm
Z
mZmZmZmZej$ej&ej(ej*d�ZdZGd�de�Zedk(r�ddlZed	�Zej8j;d
j=e��ej8j;d�ej;d�ej?�ej8j;d
j=ejAd���ejC�yy#e$r
ddl	m
ZY��wxYw)�)�absolute_importN)�
SerialBase�SerialException�to_bytes�PortNotOpenError�SerialTimeoutException�Timeout)�debug�info�warning�error�c��eZdZdZdZd�Zd�Zd�Zd�Ze	d��Z
dd�Zd	�Zd
�Z
d�Zdd�Zd
�Zd�Zd�Ze	d��Ze	d��Ze	d��Ze	d��Zd�Zy)�Serialz-Serial port implementation for plain sockets.)�2�K�n����i,iXi�ii`	i�i�%iKi�i�i�c�p�d|_|j�td��|jrtd��	t	j
|j
|j�t��|_	|jjd�|j�d|_|js|j�|j s|j#�|j%�|j'�y#t$r1}d|_	tdj|j|���d}~wwxYw)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)�logger�_portr�is_open�socket�create_connection�from_url�portstr�POLL_TIMEOUT�_socket�	Exception�format�setblocking�_reconfigure_port�_dsrdtr�_update_dtr_state�_rtscts�_update_rts_state�reset_input_buffer�reset_output_buffer)�self�msgs  �C/usr/lib/python3/dist-packages/serial/urlhandler/protocol_socket.py�openzSerial.open3s���
����:�:��!�"R�S�S��<�<�!�"9�:�:�	Z�!�3�3�D�M�M�$�,�,�4O�Ye�f�D�L�
	
��� � ��'�	
��� �����|�|��"�"�$��|�|��"�"�$����!�� � �"���	Z��D�L�!�">�"E�"E�d�l�l�TW�"X�Y�Y��	Z�s�9C;�;	D5�,D0�0D5c��|j�td��|jr|jjd�yy)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)r!rrr�r,s r.r%zSerial._reconfigure_portQs9��
�<�<��!�"B�C�C��;�;��K�K���@�A��c��|jrt|jrK	|jjtj�|jj�d|_d|_t
jd�yy#Y�)xYw)z
Close portNFg333333�?)rr!�shutdownr�	SHUT_RDWR�close�time�sleepr1s r.r6zSerial.close[sg���<�<��|�|���L�L�)�)�&�*:�*:�;��L�L�&�&�(� $��� �D�L��J�J�s�O���
��s�AB�Bc��tj|�}|jdk7r$tdj	|j���	tj
|jd�j�D]�\}}|dk(rotj�tjd�|_|jjt|d�|jjd��ztdj	|���d|j cxkrd	kstd
��td
��	|j"|j fS#t$r}tdj	|���d}~wwxYw)
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:�basicConfig�	getLoggerr�setLevel�
LOGGER_LEVELSr
�
ValueError�port�hostname)r,�url�parts�option�values�es      r.rzSerial.from_urljs`���!�!�#�&���<�<�8�#�!�5�5;�V�E�L�L�5I�K�
K�	_�"*�"3�"3�E�K�K��"F�"L�"L�"N�
L�����Y�&��'�'�)�")�"3�"3�4E�"F�D�K��K�K�(�(��v�a�y�)A�B��K�K�%�%�&7�8�$�%;�%B�%B�6�%J�K�K�
L���
�
�*�U�*� �!>�?�?�+� �!>�?�?�+�����
�
�+�+���	_�!�T�TZ�TZ�[\�T]�_�
_��	_�s�
C/E�	E:�E5�5E:c��|js
t��tj|jgggd�\}}}t	|�S)z9Return the number of bytes currently in the input buffer.r)rr�selectr!�len)r,�lr�lw�lxs    r.�
in_waitingzSerial.in_waiting�s?���|�|�"�$�$��]�]�D�L�L�>�2�r�1�=�
��B���2�w�r2c�H�|js
t��t�}t|j�}t|�|kr�	t
j|jggg|j��\}}}|s	t/|�S|jj|t|�z
�}|std��|j|�|j-�r	t/|�St|�|kr��t/|�S#t$r}}|jtjtjtj tj"tj$fvrtdj'|���Yd}~��d}~wtj(t*j(f$rw}|dtjtjtj tj"tj$fvrtdj'|���Yd}~��Od}~wwxYw)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	�_timeoutrOrNr!�	time_left�recvr�extend�OSError�errno�EAGAIN�EALREADY�EWOULDBLOCK�EINPROGRESS�EINTRr#r
r�expired�bytes)r,�size�readr�ready�_�bufrLs        r.rezSerial.read�s����|�|�"�$�$��{���$�-�-�(���$�i�$��
G�$�m�m�T�\�\�N�B��G�DU�DU�DW�X���q�!�
��*�T�{��)�l�l�'�'��s�4�y�(8�9���)�*?�@�@����C� ���� ���T�{��;�$�i�$��:�T�{����
G��7�7�5�<�<�����AR�AR�TY�Te�Te�gl�gr�gr�"s�s�)�*;�*B�*B�1�*E�F�F�t���L�L�&�,�,�/�
G��Q�4����e�n�n�e�>O�>O�QV�Qb�Qb�di�do�do�p�p�)�*;�*B�*B�1�*E�F�F�q��	
G�s-�7D�AD�	H!�A3F�&H!�*A,H�H!c��|js
t��t|�}t|�x}}t	|j
�}|dkD�r	|jj|�}|jr|S|js]|j�rtd��tjg|jgg|j��\}}}|sQtd��|j��J�tjg|jggd�\}}}|std��||d}||z}|js|j�rtd��|dkDr��|t|�z
S#t$r�t$r}}	|	j t j"t j$t j&t j(t j*fvrtdj-|	���Yd}	~	��d}	~	wtj.$rw}	|	dt j"t j$t j&t j(t j*fvrtdj-|	���Yd}	~	��Nd}	~	wwxYw)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.
        rz
Write timeoutNzwrite failed (select)zwrite failed: {})rrrrOr	�_write_timeoutr!�send�is_non_blocking�is_infiniterbrrNrXrr[r\r]r^r_r`rar#r
)
r,�data�d�tx_len�lengthr�nrgrfrLs
          r.�writezSerial.write�s&���|�|�"�$�$��T�N���a�&� ����$�-�-�.���q�j�#
H��L�L�%�%�a�(���*�*��H� �,�,����(�4�_�E�E�"(�-�-��T�\�\�N�B��HY�HY�H[�"\�K�A�u�a� �4�_�E�E�"�,�,�.�6�6�6�"(�-�-��T�\�\�N�B��"M�K�A�u�a� �-�.E�F�F��a�b�E���!����*�*�w���/@�,�_�=�=�M�q�j�N��A�����!#�
���
H��7�7�5�<�<�����AR�AR�TY�Te�Te�gl�gr�gr�"s�s�)�*<�*C�*C�A�*F�G�G�t���<�<�
H��Q�4����e�n�n�e�>O�>O�QV�Qb�Qb�di�do�do�p�p�)�*<�*C�*C�A�*F�G�G�q��	
H�s-�(E)�4B9E)�)J�;A3G3�3J�	A,I;�;Jc��|js
t��d}|rItj|jgggd�\}}}	|r|jj	d�}|r�Hyy#t
$r}}|jtjtjtjtjtjfvrtdj|���Yd}~��d}~wtjtjf$rw}|dtjtjtjtjtjfvrtdj|���Yd}~��d}~wwxYw)z9Clear input buffer, discarding all that is in the buffer.TrirUN)rrrNr!rYr[r\r]r^r_r`rarr#r
r)r,rfrgrLs    r.r*zSerial.reset_input_buffer�s-���|�|�"�$�$���� �-�-������B��B�K�E�1�a�
G�� �L�L�-�-�d�3�E�	��
�
G��7�7�5�<�<�����AR�AR�TY�Te�Te�gl�gr�gr�"s�s�)�*;�*B�*B�1�*E�F�F�t���L�L�&�,�,�/�
G��Q�4����e�n�n�e�>O�>O�QV�Qb�Qb�di�do�do�p�p�)�*;�*B�*B�1�*E�F�F�q��	
G�s&�A%�%	F�.A3C&�&&F�A,E>�>Fc��|js
t��|jr|jjd�yy)zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zignored reset_output_bufferN�rrrrr1s r.r+zSerial.reset_output_buffers3��
�|�|�"�$�$��;�;��K�K���:�;�r2c��|js
t��|jr+|jjdj	|��yy)za        Send break condition. Timed, returns to idle state after given
        duration.
        zignored send_break({!r})N)rrrrr#)r,�durations  r.�
send_breakzSerial.send_breaks>��
�|�|�"�$�$��;�;��K�K���7�>�>�x�H�I�r2c��|jr5|jjdj|j��yy)zJSet break: Controls TXD. When active, to transmitting is
        possible.z!ignored _update_break_state({!r})N)rrr#�_break_stater1s r.�_update_break_statezSerial._update_break_states4���;�;��K�K���@�G�G��HY�HY�Z�[�r2c��|jr5|jjdj|j��yy)z)Set terminal status line: Request To Sendzignored _update_rts_state({!r})N)rrr#�
_rts_stater1s r.r)zSerial._update_rts_state%�0���;�;��K�K���>�E�E�d�o�o�V�W�r2c��|jr5|jjdj|j��yy)z-Set terminal status line: Data Terminal Readyzignored _update_dtr_state({!r})N)rrr#�
_dtr_stater1s r.r'zSerial._update_dtr_state*rr2c�~�|js
t��|jr|jjd�y)z(Read terminal status line: Clear To Sendzreturning dummy for ctsTrvr1s r.�ctsz
Serial.cts/�1���|�|�"�$�$��;�;��K�K���6�7�r2c�~�|js
t��|jr|jjd�y)z)Read terminal status line: Data Set Readyzreturning dummy for dsrTrvr1s r.�dsrz
Serial.dsr8r�r2c�~�|js
t��|jr|jjd�y)z)Read terminal status line: Ring Indicatorzreturning dummy for riFrvr1s r.�riz	Serial.riAs1���|�|�"�$�$��;�;��K�K���5�6�r2c�~�|js
t��|jr|jjd�y)z)Read terminal status line: Carrier Detectzreturning dummy for cd)Trvr1s r.�cdz	Serial.cdJr�r2c�6�|jj�S)z@Get the file handle of the underlying socket for use with select)r!�filenor1s r.r�z
Serial.filenoVs���|�|�"�"�$�$r2N)�)g�?)�__name__�
__module__�__qualname__�__doc__�	BAUDRATESr/r%r6r�propertyrSrersr*r+ryr|r)r'r�r�r�r�r��r2r.rr-s���7�4�I�#�<B�
�,�:����'�R3�jG�2<�J�\�X�
X�
����������������%r2r�__main__zsocket://localhost:7000z{}
z	write...
shello
z	read: {}
)"�
__future__rr\r:rNrr7r;�ImportError�urllib.parse�parse�serial.serialutilrrrrrr	�DEBUG�INFO�WARNING�ERRORrDr rr��sys�s�stdoutrsr#�flushrer6r�r2r.�<module>r�s��&'���
�
��$��6�6�
�]�]��L�L����
�]�]�	�
���k%�Z�k%�`	�z����(�)�A��J�J���V�]�]�1�%�&��J�J���\�"��G�G�J���G�G�I��J�J���\�(�(������3�4��G�G�I���C
�$�#�#�$�s�D.�.D=�<D=