
    Ȧ^_7                        d dl mZ d dlZd dlZd dlZd dlZd dlZ	 d dlZ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dZ G d de      Zedk(  rd dlZ ed	      Zej8                  j;                  d
j=                  e             ej8                  j;                  d       ej;                  d       ej?                          ej8                  j;                  dj=                  ejA                  d                   ejC                          yy# e$ r
 d dl	m
Z Y w xY w)    )absolute_importN)
SerialBaseSerialExceptionto_bytesPortNotOpenErrorSerialTimeoutExceptionTimeout)debuginfowarningerror   c                       e Zd ZdZdZd Zd Zd Zd Ze	d        Z
ddZd	 Zd
 Zd ZddZd Zd Zd Ze	d        Ze	d        Ze	d        Ze	d        Zd Zy)Serialz-Serial port implementation for plain sockets.)2   K   n            i,  iX  i  i  i`	  i  i%  i K  i   i   i  c                 p   d| _         | j                  t        d      | j                  rt        d      	 t	        j
                  | j                  | j                        t              | _	        | j                  j                  d       | j                          d| _        | j                  s| j                          | j                   s| j#                          | j%                          | j'                          y# t        $ r1}d| _	        t        dj                  | j                  |            d}~ww xY w)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.zPort is already open.)timeoutzCould 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openzSerial.open3   s    
 ::!"RSS<<!"9::	Z!33DMM$,,4OYefDL
 	  ' 	 ||""$||""$!  "  	ZDL!">"E"EdllTW"XYY	Zs   9C; ;	D5,D00D5c                     | j                   t        d      | j                  r| j                  j                  d       yy)zz        Set communication parameters on opened port. For the socket://
        protocol all settings are ignored!
        NzCan only operate on open portsz!ignored port configuration change)r!   r   r   r   r,   s    r.   r%   zSerial._reconfigure_portQ   s9    
 <<!"BCC;;KK@A     c                    | j                   rt| j                  rK	 | j                  j                  t        j                         | j                  j                          d| _        d| _         t        j                  d       yy#  Y )xY w)z
Close portNFg333333?)r   r!   shutdownr   	SHUT_RDWRclosetimesleepr1   s    r.   r6   zSerial.close[   sg    <<||LL))&*:*:;LL&&(  $ DLJJsO 
s   AB Bc                    t        j                  |      }|j                  dk7  r$t        dj	                  |j                              	 t        j
                  |j                  d      j                         D ]  \  }}|dk(  rot        j                          t        j                  d      | _        | j                  j                  t        |d             | j                  j                  d       zt        dj	                  |             d|j                   cxk  rd	k  st        d
       t        d
      	 |j"                  |j                   fS # t        $ r}t        dj	                  |            d}~ww xY w)z(extract host and port from an URL stringr   zexpected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": not starting with socket:// ({!r})TloggingzpySerial.socketr   zenabled loggingzunknown option: {!r}i   zport 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   zSerial.from_urlj   s`   !!#&<<8#!55;VELL5IK K	_"*"3"3EKK"F"L"L"N LY&'')")"3"34E"FDKKK((vay)ABKK%%&78$%;%B%B6%JKKL 

*U* !>?? + !>?? + 

++  	_!TTZTZ[\T]_ _	_s   
C/E 	E:E55E:c                     | j                   s
t               t        j                  | j                  gg 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_waitingzSerial.in_waiting   s?     ||"$$ ]]DLL>2r1=
B2wr2   c                 H   | j                   s
t               t               }t        | j                        }t        |      |k  r	 t        j                  | j                  gg g |j                               \  }}}|s	 t/        |      S | j                  j                  |t        |      z
        }|st        d      |j                  |       |j-                         r	 t/        |      S t        |      |k  rt/        |      S # t        $ r}}|j                  t        j                  t        j                  t        j                   t        j"                  t        j$                  fvrt        dj'                  |            Y d}~d}~wt        j(                  t*        j(                  f$ rw}|d   t        j                  t        j                  t        j                   t        j"                  t        j$                  fvrt        dj'                  |            Y d}~Od}~ww xY w)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.
        zsocket 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.   re   zSerial.read   s    ||"$${$--($i$G$mmT\\NBGDUDUDWXq!
 * T{) ll''s4y(89 )*?@@C   T{; $i$: T{  G 775<<ARARTYTeTeglgrgr"ss)*;*B*B1*EFF tLL&,,/ G Q4enne>O>OQVQbQbdidodopp)*;*B*B1*EFF q	Gs-   7D AD 	H!A3F&H!*A,HH!c                    | j                   s
t               t        |      }t        |      x}}t	        | j
                        }|dkD  r	 | j                  j                  |      }|j                  r|S |j                  s]|j                         rt        d      t        j                  g | j                  gg |j                               \  }}}|sQt        d      |j                         J t        j                  g | j                  gg d      \  }}}|st        d      ||d }||z  }|j                  s|j                         rt        d      |dkD  r|t        |      z
  S # t        $ r  t        $ r}}	|	j                   t         j"                  t         j$                  t         j&                  t         j(                  t         j*                  fvrt        dj-                  |	            Y d}	~	d}	~	wt        j.                  $ rw}	|	d   t         j"                  t         j$                  t         j&                  t         j(                  t         j*                  fvrt        dj-                  |	            Y d}	~	Nd}	~	ww xY w)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 timeoutNzwrite failed (select)zwrite failed: {})r   r   r   rO   r	   _write_timeoutr!   sendis_non_blockingis_infiniterb   r   rN   rX   r   r[   r\   r]   r^   r_   r`   ra   r#   r   )
r,   datadtx_lenlengthr   nrg   rf   rL   s
             r.   writezSerial.write   s&    ||"$$TNa& $--.qj#HLL%%a(** H ,, (4_EE"(--T\\NBHYHYH["\KAua 4_EE",,.666"(--T\\NB"MKAua -.EFFabE! **w/@,_==M qjN A! #  H 775<<ARARTYTeTeglgrgr"ss)*<*C*CA*FGG t<< H Q4enne>O>OQVQbQbdidodopp)*<*C*CA*FGG q	Hs-   (E) 4B9E) )J ;A3G33J 	A,I;;J c                    | j                   s
t               d}|rIt        j                  | j                  gg g d      \  }}}	 |r| j                  j	                  d      }|rHyy# t
        $ r}}|j                  t        j                  t        j                  t        j                  t        j                  t        j                  fvrt        dj                  |            Y d}~d}~wt        j                  t        j                  f$ rw}|d   t        j                  t        j                  t        j                  t        j                  t        j                  fvrt        dj                  |            Y d}~d}~ww xY w)z9Clear input buffer, discarding all that is in the buffer.Tr   i   rU   N)r   r   rN   r!   rY   r[   r\   r]   r^   r_   r`   ra   r   r#   r   r   )r,   rf   rg   rL   s       r.   r*   zSerial.reset_input_buffer   s-   ||"$$  --BBKE1aG LL--d3E	 
  G 775<<ARARTYTeTeglgrgr"ss)*;*B*B1*EFF tLL&,,/ G Q4enne>O>OQVQbQbdidodopp)*;*B*B1*EFF q	Gs&   A% %	F.A3C&&&FA,E>>Fc                     | j                   s
t               | j                  r| j                  j                  d       yy)zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        zignored reset_output_bufferNr   r   r   r   r1   s    r.   r+   zSerial.reset_output_buffer  s3    
 ||"$$;;KK:; r2   c                     | j                   s
t               | j                  r+| j                  j                  dj	                  |             yy)za        Send break condition. Timed, returns to idle state after given
        duration.
        zignored send_break({!r})N)r   r   r   r   r#   )r,   durations     r.   
send_breakzSerial.send_break  s>    
 ||"$$;;KK7>>xHI r2   c                     | j                   r5| j                   j                  dj                  | j                               yy)zJSet break: Controls TXD. When active, to transmitting is
        possible.z!ignored _update_break_state({!r})N)r   r   r#   _break_stater1   s    r.   _update_break_statezSerial._update_break_state  s4     ;;KK@GGHYHYZ[ r2   c                     | j                   r5| j                   j                  dj                  | j                               yy)z)Set terminal status line: Request To Sendzignored _update_rts_state({!r})N)r   r   r#   
_rts_stater1   s    r.   r)   zSerial._update_rts_state%  0    ;;KK>EEdooVW r2   c                     | j                   r5| j                   j                  dj                  | j                               yy)z-Set terminal status line: Data Terminal Readyzignored _update_dtr_state({!r})N)r   r   r#   
_dtr_stater1   s    r.   r'   zSerial._update_dtr_state*  r   r2   c                 ~    | j                   s
t               | j                  r| j                  j                  d       y)z(Read terminal status line: Clear To Sendzreturning dummy for ctsTrv   r1   s    r.   ctsz
Serial.cts/  1     ||"$$;;KK67r2   c                 ~    | j                   s
t               | j                  r| j                  j                  d       y)z)Read terminal status line: Data Set Readyzreturning dummy for dsrTrv   r1   s    r.   dsrz
Serial.dsr8  r   r2   c                 ~    | j                   s
t               | j                  r| j                  j                  d       y)z)Read terminal status line: Ring Indicatorzreturning dummy for riFrv   r1   s    r.   riz	Serial.riA  s1     ||"$$;;KK56r2   c                 ~    | j                   s
t               | j                  r| j                  j                  d       y)z)Read terminal status line: Carrier Detectzreturning dummy for cd)Trv   r1   s    r.   cdz	Serial.cdJ  r   r2   c                 6    | j                   j                         S )z@Get the file handle of the underlying socket for use with select)r!   filenor1   s    r.   r   zSerial.filenoV  s    ||""$$r2   N)   )g      ?)__name__
__module____qualname____doc__	BAUDRATESr/   r%   r6   r   propertyrS   re   rs   r*   r+   ry   r|   r)   r'   r   r   r   r   r    r2   r.   r   r   -   s    74I#<B,:  'R3jG2<J\X
X
        %r2   r   __main__zsocket://localhost:7000z{}
z	write...
s   hello
z	read: {}
)"
__future__r   r\   r:   rN   r   r7   r;   ImportErrorurllib.parseparseserial.serialutilr   r   r   r   r   r	   DEBUGINFOWARNINGERRORrD   r    r   r   syssstdoutrs   r#   flushre   r6   r   r2   r.   <module>r      s  & '     $6 6
 ]]LL]]	 k%Z k%`	 z()AJJV]]1%&JJ\"GGJGGIJJ\((34GGI C
  $##$s   D. .D=<D=