�
Ȧ^_�7 � � � d dl mZ d dlZd dlZd dlZd dlZd dlZ d dlZn# e$ r d dl m
Z Y nw xY wd dlmZm
Z
mZmZmZmZ ej ej ej ej d�ZdZ G d� de� � Zedk r�d dlZ ed � � Zej � d
� e� � � � ej � d� � e� d� � e� � � ej � d
� e� d� � � � � � e�! � � dS dS )� )�absolute_importN)�
SerialBase�SerialException�to_bytes�PortNotOpenError�SerialTimeoutException�Timeout)�debug�info�warning�error� c �� � e Zd ZdZdZd� Zd� Zd� Zd� Ze d� � � Z
dd �Zd
� Zd� Z
d� Zdd�Zd� Zd� Zd� Ze d� � � Ze d� � � Ze d� � � Ze d� � � Zd� ZdS )�Serialz-Serial port implementation for plain sockets.)�2 �K �n � � �� i, iX i� i i` i� i�% i K i � i � i � c � � d| _ | j �t d� � �| j rt d� � � t j | � | j � � t �� � | _ nA# t $ r4}d| _ t d� | j |� � � � �d}~ww xY w| j � d� � | �
� � d| _ | j s| � � � | j s| � � � | � � � | � � � 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)�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.open3 sJ � �
����:��!�"R�S�S�S��<� ;�!�"9�:�:�:� Z�!�3�D�M�M�$�,�4O�4O�Ye�f�f�f�D�L�L��� Z� Z� Z��D�L�!�">�"E�"E�d�l�TW�"X�"X�Y�Y�Y����� Z����
�� � ��'�'�'�
��� � � �����|� %��"�"�$�$�$��|� %��"�"�$�$�$����!�!�!�� � �"�"�"�"�"s �8A. �.
B,�8/B'�'B,c �x � | j �t d� � �| j r| j � d� � dS 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)r! r r r �r, s r. r% zSerial._reconfigure_portQ sO � �
�<��!�"B�C�C�C��;� B��K���@�A�A�A�A�A� B� B� c � � | j rq| j rM | j � t j � � | j � � � n# Y nxY wd| _ d| _ t
j d� � dS dS )z
Close portNFg333333�?)r r! �shutdownr � SHUT_RDWR�close�time�sleepr1 s r. r6 zSerial.close[ s� � ��<� ��|�
$���L�)�)�&�*:�;�;�;��L�&�&�(�(�(�(����D����#��� �D�L��J�s�O�O�O�O�O� � s �=A �Ac �� � t j |� � }|j dk r't d� |j � � � � � t j |j d� � � � � D ]�\ }}|dk rrt j � � t j
d� � | _ | j � t |d � � | j � d� � �}t d� |� � � � �d|j cxk rd k sn t d
� � �n4# t $ r'}t d� |� � � � �d}~ww xY w|j |j fS )
z(extract host and port from an URL stringr zexpected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": not starting with socket:// ({!r})T�loggingzpySerial.socketr zenabled loggingzunknown option: {!r}i zport not in range 0...65535z_expected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": {}N)�urlparse�urlsplit�schemer r# �parse_qs�query�itemsr: �basicConfig� getLoggerr �setLevel�
LOGGER_LEVELSr
�
ValueError�port�hostname)r, �url�parts�option�values�es r. r zSerial.from_urlj s� � ��!�#�&�&���<�8�#�#�!�5�5;�V�E�L�5I�5I�K� K�
K� _�"*�"3�E�K��"F�"F�"L�"L�"N�"N�
L�
L�����Y�&�&��'�)�)�)�")�"3�4E�"F�"F�D�K��K�(�(��v�a�y�)A�B�B�B��K�%�%�&7�8�8�8�8�$�%;�%B�%B�6�%J�%J�K�K�K���
�*�*�*�*�U�*�*�*�*� �!>�?�?�?� +��� _� _� _�!�T�TZ�TZ�[\�T]�T]�_� _�
_����� _����
���
�+�+s �C.D7 �7
E(�"E#�#E(c � � | j st � � �t j | j gg g d� � \ }}}t |� � S )z9Return the number of bytes currently in the input buffer.r )r r �selectr! �len)r, �lr�lw�lxs r. �
in_waitingzSerial.in_waiting� sF � � �|� %�"�$�$�$� �]�D�L�>�2�r�1�=�=�
��B���2�w�w�r2 � c � � | j st � � �t � � }t | j � � }t |� � |k �r� t
j | j gg g |� � � � � \ }}}|s�ny| j � |t |� � z
� � }|st d� � �|� |� � n�# t $ rl}|j
t j t j t j t j t j fvr"t d� |� � � � �Y d}~n�d}~wt j t* j f$ rm}|d t j t j t j t j t j fvr"t d� |� � � � �Y d}~nd}~ww xY w|� � � rnt |� � |k ���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.
zsocket disconnected�read failed: {}Nr )r r � bytearrayr �_timeoutrO rN r! � time_left�recvr �extend�OSError�errno�EAGAIN�EALREADY�EWOULDBLOCK�EINPROGRESS�EINTRr# r
r �expired�bytes)r, �size�readr �ready�_�bufrL s r. rf zSerial.read� s� � � �|� %�"�$�$�$��{�{���$�-�(�(���$�i�i�$���
G�$�m�T�\�N�B��G�DU�DU�DW�DW�X�X���q�!�
� ���l�'�'��s�4�y�y�(8�9�9�� � A�)�*?�@�@�@����C� � � � ���
G�
G�
G� �7�5�<����AR�TY�Te�gl�gr�"s�s�s�)�*;�*B�*B�1�*E�*E�F�F�F� t�s�s�s�s������L�&�,�/�
G�
G�
G� �Q�4���e�n�e�>O�QV�Qb�di�do�p�p�p�)�*;�*B�*B�1�*E�*E�F�F�F� q�p�p�p�p�����
G���� ��� � �
��9 �$�i�i�$���: �T�{�{�s- �
5C �AC �
G�A"E�G�$A#G�Gc � � | j st � � �t |� � }t |� � x}}t | j � � }|dk �r | j � |� � }|j r|S |j sh|�
� � rt d� � �t j g | j gg |�
� � � � \ }}}|st d� � �nH|�
� � �J �t j g | j gg d� � \ }}}|st d� � �||d� }||z }n�# t $ r � t $ rl} | j t j t j t j t j t j fvr"t d� | � � � � �Y d} ~ n�d} ~ wt j $ rm} | d t j t j t j t j t j fvr"t d� | � � � � �Y d} ~ nd} ~ ww xY w|j s#|�
� � rt d� � �|dk ��|t |� � z
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 timeoutNzwrite failed (select)zwrite failed: {})r r r rO r �_write_timeoutr! �send�is_non_blocking�is_infiniterc r rN rY r r\ r] r^ r_ r` ra rb r# r
)
r, �data�d�tx_len�lengthr �nrh rg rL s
r. �writezSerial.write� s� � � �|� %�"�$�$�$��T�N�N���a�&�&� ����$�-�.�.���q�j�j�#
H��L�%�%�a�(�(���*� G� �H� �,�
G� ���(�(� F�4�_�E�E�E�"(�-��T�\�N�B��HY�HY�H[�H[�"\�"\�K�A�u�a� � F�4�_�E�E�E�F� #�,�,�.�.�6�6�6�"(�-��T�\�N�B��"M�"M�K�A�u�a� � G�-�.E�F�F�F��a�b�b�E���!�����"�
�
�
���
H�
H�
H� �7�5�<����AR�TY�Te�gl�gr�"s�s�s�)�*<�*C�*C�A�*F�*F�G�G�G� t�s�s�s�s������<�
H�
H�
H� �Q�4���e�n�e�>O�QV�Qb�di�do�p�p�p�)�*<�*C�*C�A�*F�*F�G�G�G� q�p�p�p�p�����
H���� �*�
>�w���/@�/@�
>�,�_�=�=�=�M �q�j�j�N ��A����s- �"D< |