
    #)di)                        d dl mZ d dlZd dlZd dlZ	 d dlZn# e$ r	 d dlmZ Y nw xY w	 d dl	Z	n# e$ r d dl
Z	Y nw xY 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 G 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	iterbytesSerialTimeoutExceptionPortNotOpenError)debuginfowarningerrorc                        e Zd ZdZdZ fdZd Z fdZd Zd Z	e
d             Zdd
Zd Zd Zd Zd Zd Ze
d             Zd Zd Zd Ze
d             Ze
d             Ze
d             Ze
d             Z xZS )SerialzSSerial port implementation that simulates a loop back connection in plain software.)2   K   n            i,  iX  i  i  i`	  i  i%  i K  i   i   i  c                     d| _         d | _        d | _        d| _         t	          t
          |           j        |i | d S )Ni   F)buffer_sizequeuelogger_cancel_writesuperr   __init__)selfargskwargs	__class__s      f/var/www/menimich/repos/protonApp/venv/lib/python3.11/site-packages/serial/urlhandler/protocol_loop.pyr   zSerial.__init__/   sI    
"$fd$d5f55555    c                    | j         rt          d          d| _        t          j        | j                  | _        | j        t          d          |                     | j                   | 	                                 d| _         | j
        s|                                  | j        s|                                  |                                  |                                  dS )zx        Open port with current settings. This may throw a SerialException
        if the port cannot be opened.
        zPort is already open.Nz.Port must be configured before it can be used.T)is_openr   r   r   Queuer   _portfrom_urlport_reconfigure_port_dsrdtr_update_dtr_state_rtscts_update_rts_statereset_input_bufferreset_output_bufferr   s    r!   openzSerial.open6   s    
 < 	;!"9:::[!122
:!"RSSS 	di    	   | 	%""$$$| 	%""$$$!!!  """""r"   c                     | j         r8d| _         	 | j                            d            n# t          j        $ r Y nw xY wt	          t
          |                                            d S )NF)r$   r   
put_nowaitFullr   r   close)r   r    s    r!   r5   zSerial.closeQ   sv    < 	 DL
%%d++++:   fd!!#####s   , >>c                     t          | j        t          j                  rd| j        cxk     rdk     s)n t	          d                    | j                            | j        r| j                            d           dS dS )zx        Set communication parameters on opened port. For the loop://
        protocol all settings are ignored!
        r   l        zinvalid baudrate: {!r}z_reconfigure_port()N)
isinstance	_baudratenumbersIntegral
ValueErrorformatr   r   r0   s    r!   r)   zSerial._reconfigure_portZ   s     $.'*:;; 	N1t~C_C_C_C_X_C_C_C_C_5<<T^LLMMM; 	4K233333	4 	4r"   c                    t          j        |          }|j        dk    r't          d                    |j                            	 t          j        |j        d                                          D ]\  }}|dk    rrt          j	                     t          j
        d          | _        | j                            t          |d                             | j                            d           }t          d                    |                    d
S # t          $ r'}t          d	                    |                    d
}~ww xY w)z(extract host and port from an URL stringloopznexpected a string in the form "loop://[?logging={debug|info|warning|error}]": not starting with loop:// ({!r})TloggingzpySerial.loopr   zenabled loggingzunknown option: {!r}zPexpected a string in the form "loop://[?logging={debug|info|warning|error}]": {}N)urlparseurlsplitschemer   r<   parse_qsqueryitemsr?   basicConfig	getLoggerr   setLevelLOGGER_LEVELSr
   r;   )r   urlpartsoptionvalueses         r!   r'   zSerial.from_urle   sT   !#&&<6!!!&&,fU\&:&:< < <	P"*"3EK"F"F"L"L"N"N L LY&&')))")"3O"D"DDKK((vay)ABBBK%%&78888$%;%B%B6%J%JKKKL L  	P 	P 	P!EEKVAYYP P P	Ps   CD 
E"EEc                     | j         st                      | j        rD| j                            d                    | j                                                             | j                                        S )z9Return the number of bytes currently in the input buffer.zin_waiting -> {:d}r$   r	   r   r
   r<   r   qsizer0   s    r!   
in_waitingzSerial.in_waiting~   sn     | 	%"$$$; 	O K299$*:J:J:L:LMMNNNz!!!r"      c                    | j         st                      | j        '| j        dk    rt          j                    | j        z   }nd}t	                      }|dk    r| j         r	 | j                            | j                  }|||z  }|dz  }n#nj# t
          j        $ r | j        dk    rY nNY nw xY w|r9t          j                    |k    r"| j        r| j        	                    d           n|dk    r| j         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.
        Nr   timeoutrS   zread timeout)r$   r	   _timeouttime	bytearrayr   getEmptyr   r   bytes)r   sizerV   databs        r!   readzSerial.read   s.    | 	%"$$$=$!););ikkDM1GGG{{Qhh4<h
JNN4=N99
 =AIDAIDD ;   =A%%E &%  49;;00; 5K$$^444# Qhh4<h$ T{{s   " B B/.B/c                 :    | j                             d            d S )N)r   r3   r0   s    r!   cancel_readzSerial.cancel_read   s    
d#####r"   c                     d| _         d S )NT)r   r0   s    r!   cancel_writezSerial.cancel_write   s    !r"   c                    d| _         | j        st                      t          |          }dt	          |          z  | j        z  }| j        k|| j        k    r`| j        }|dk    r;| j         s4t          j        t          |d                     |dz  }|dk    r| j         4| j         rdS t          d          t          |          D ]#}| j                            || j                   $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      $@Nr   g      ?zWrite timeoutrU   )r   r$   r	   r   lenr8   _write_timeoutrX   sleepminr   r   r   put)r   r^   time_used_to_send	time_leftbytes        r!   writezSerial.write   s    #| 	%"$$$~~ 3t99,t~= */@4CV/V/V+Ia--(:-
3y#..///S 	 a--(:- ! q(999dOO 	> 	>DJNN4)<N====4yyr"   c                 :   | j         st                      | j        r| j                            d           	 | j                                        r4| j                                         | j                                        2dS dS # t          j        $ r Y dS w xY w)z9Clear input buffer, discarding all that is in the buffer.zreset_input_buffer()Nr$   r	   r   r   r   rQ   
get_nowaitr[   r0   s    r!   r.   zSerial.reset_input_buffer   s    | 	%"$$$; 	5K3444	*""$$ (
%%''' *""$$ ( ( ( ( ({ 	 	 	DD	   AB BBc                 :   | j         st                      | j        r| j                            d           	 | j                                        r4| j                                         | j                                        2dS dS # t          j        $ r Y dS w xY w)zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zreset_output_buffer()Nrp   r0   s    r!   r/   zSerial.reset_output_buffer   s    
 | 	%"$$$; 	6K4555	*""$$ (
%%''' *""$$ ( ( ( ( ({ 	 	 	DD	rr   c                     | j         st                      | j        rD| j                            d                    | j                                                             | j                                        S )z0Return how many bytes the in the outgoing bufferzout_waiting -> {:d}rP   r0   s    r!   out_waitingzSerial.out_waiting   sn     | 	%"$$$; 	P K3::4:;K;K;M;MNNOOOz!!!r"   c                 |    | j         r4| j                             d                    | j                             dS dS )z[        Set break: Controls TXD. When active, to transmitting is
        possible.
        z_update_break_state({!r})N)r   r   r<   _break_stater0   s    r!   _update_break_statezSerial._update_break_state   sI    
 ; 	TK8??@QRRSSSSS	T 	Tr"   c                 |    | j         r4| j                             d                    | j                             dS dS )z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)r   r   r<   
_rts_stater0   s    r!   r-   zSerial._update_rts_state   F    ; 	`KFMMdo^^_____	` 	`r"   c                 |    | j         r4| j                             d                    | j                             dS dS )z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRNr   r   r<   
_dtr_stater0   s    r!   r+   zSerial._update_dtr_state   r{   r"   c                     | j         st                      | j        r2| j                            d                    | j                             | j        S )z(Read terminal status line: Clear To SendzCTS -> state of RTS ({!r}))r$   r	   r   r   r<   rz   r0   s    r!   ctsz
Serial.cts  sT     | 	%"$$$; 	SK9@@QQRRRr"   c                     | j         r2| j                             d                    | j                             | j        S )z)Read terminal status line: Data Set ReadyzDSR -> state of DTR ({!r})r}   r0   s    r!   dsrz
Serial.dsr  s>     ; 	SK9@@QQRRRr"   c                 r    | j         st                      | j        r| j                            d           dS )z)Read terminal status line: Ring Indicatorzreturning dummy for RIFr$   r	   r   r   r0   s    r!   riz	Serial.ri  s@     | 	%"$$$; 	7K5666ur"   c                 r    | j         st                      | j        r| j                            d           dS )z)Read terminal status line: Carrier Detectzreturning dummy for CDTr   r0   s    r!   cdz	Serial.cd  s@     | 	%"$$$; 	7K5666tr"   )rS   )__name__
__module____qualname____doc__	BAUDRATESr   r1   r5   r)   r'   propertyrR   r`   rb   rd   rn   r.   r/   ru   rx   r-   r+   r   r   r   r   __classcell__)r    s   @r!   r   r   )   s       ]]4I6 6 6 6 6# # #6$ $ $ $ $	4 	4 	4P P P2 " " X"   B$ $ $" " "  6
 
 
   " " X"T T T` ` `
` ` `
   X   X   X   X    r"   r   __main__zloop://z{}
z	write...
zhello
zread: {!r}
   )!
__future__r   r?   r9   rX   r@   ImportErrorurllib.parseparser   r%   serial.serialutilr   r   r   r   r   r	   DEBUGINFOWARNINGERRORrI   r   r   syssstdoutrn   r<   flushr`   r5    r"   r!   <module>r      s    ' & & & & &   $OOOO $ $ $########$LLLL    y x x x x x x x x x x x x x x x ]L]	 z z z z zZ z z zB zJJJyAJV]]1%%&&&J\"""GGIGGIIIJ^**166!9955666GGIIIII s    ''0 	<<