
    &a_%U              
       >   d dl mZ d dlZd dlZ	 e 	 e 	 e
 d Zd Z edg      Z edg      Z ed	g      Z ed
g      Zd\  ZZZZZd\  ZZZd\  ZZZZededededediZ G d de      Z G d de      Z  G d de      Z! G d de      Z" G d dejF                        Z$e%dk(  r:d dl&Z& e$       Z'e&jP                  jS                  djU                  e'jV                               e&jP                  jS                  djU                  e'jX                               e&jP                  jS                  d jU                  e'jZ                               e&jP                  jS                  d!jU                  e'j\                               e&jP                  jS                  d"jU                  e'j^                               e&jP                  jS                  d#jU                  e'             yy# eef$ r  G d de      ZY w xY w# eef$ r e	ZY w xY w# eef$ r e	fZ
Y w xY w)$    )absolute_importNc                       e Zd Zy)
memoryviewN)__name__
__module____qualname__     3/usr/lib/python3/dist-packages/serial/serialutil.pyr   r      s    r
   r   c              #   |   K   t        | t              r| j                         } d}	 | ||dz    }|dz  }|r| nyw)z=Iterate over bytes, returning bytes instead of ints (python3)r      N)
isinstancer   tobytes)bias      r   	iterbytesr   (   sK     !Z IIK	A
aAJ	QG s   :<c                    t        | t              r| S t        | t              rt        |       S t        | t              r| j	                         S t        | t
              rt        dj                  |             t        t        |             S )z"convert a sequence to a bytes typez?unicode strings are not supported, please encode to bytes: {!r})r   bytes	bytearrayr   r   unicode	TypeErrorformat)seqs    r   to_bytesr   8   sn    #u
	C	#Sz	C	${{}	C	!Y``adeff Ys^$$r
            
   )NEOMS)r   g      ?   )            NoneEvenOddMarkSpacec                       e Zd ZdZy)SerialExceptionz.Base class for serial port related exceptions.Nr   r   r   __doc__r	   r
   r   r0   r0   \   s    8r
   r0   c                       e Zd ZdZy)SerialTimeoutExceptionz Write timeouts give an exceptionNr1   r	   r
   r   r4   r4   `   s    *r
   r4   c                   "     e Zd ZdZ fdZ xZS )PortNotOpenErrorzPort is not openc                 ,    t         t        |   d       y )Nz)Attempting to use a port that is not open)superr6   __init__)self	__class__s    r   r9   zPortNotOpenError.__init__f   s    ./Z[r
   )r   r   r   r2   r9   __classcell__)r;   s   @r   r6   r6   d   s    \ \r
   r6   c                   l    e Zd ZdZ eed      rej                  Znej
                  Zd Zd Z	d Z
d Zy)Timeouta>      Abstraction for timeout operations. Using time.monotonic() if available
    or time.time() in all other cases.

    The class can also be initialized with 0 or None, in order to support
    non-blocking and fully blocking I/O operations. The attributes
    is_non_blocking and is_infinite are set accordingly.
    	monotonicc                 |    |du | _         |dk(  | _        || _        || j                         |z   | _        yd| _        y)z(Initialize a timeout with given durationNr   )is_infiniteis_non_blockingdurationTIMEtarget_timer:   rC   s     r   r9   zTimeout.__init__   sC    $, (A #yy{X5D#Dr
   c                 H    | j                   duxr | j                         dk  S )z4Return a boolean, telling if the timeout has expiredNr   )rE   	time_leftr:   s    r   expiredzTimeout.expired   s$    t+E0@A0EEr
   c                    | j                   ry| j                  ry| j                  | j                         z
  }|| j                  kD  r.| j                         | j                  z   | _        | j                  S t        d|      S )z:Return how many seconds are left until the timeout expiresr   N)rB   rA   rE   rD   rC   max)r:   deltas     r   rH   zTimeout.time_left   si    $$tyy{2Et}}$#'99;#> }}$1e}$r
   c                 B    || _         | j                         |z   | _        y)zb        Restart a timeout, only supported if a timeout was already set up
        before.
        N)rC   rD   rE   rF   s     r   restartzTimeout.restart   s    
 !99;1r
   N)r   r   r   r2   hasattrtimer?   rD   r9   rJ   rH   rO   r	   r
   r   r>   r>   j   s>     t[!
 ~~
 yy$F%2r
   r>   c                      e Zd ZdZdZeeeefZ	e
eeeefZeeefZddee
edddddddfdZed        Zej.                  d        Zed	        Zej.                  d
        Zed        Zej.                  d        Zed        Zej.                  d        Zed        Zej.                  d        Zed        Zej.                  d        Zed        Zej.                  d        Zed        Zej.                  d        Zed        Zej.                  d        Zed        Z e j.                  d        Z ed        Z!e!j.                  d        Z!ed        Z"e"j.                  dGd       Z"ed        Z#e#j.                  d         Z#ed!        Z$e$j.                  d"        Z$ed#        Z%e%j.                  d$        Z%ed%        Z&e&j.                  d&        Z&d'Z'd( Z(d) Z)d* Z*d+ Z+d, Z,d- Z-d. Z.d/ Z/d0 Z0dHd1Z1d2 Z2d3 Z3d4 Z4dHd5Z5dId6Z6dId7Z7d8 Z8d9 Z9d: Z:d; Z;d< Z<ed=        Z=e=j.                  d>        Z=ed?        Z>e>j.                  d@        Z>dA Z?dB Z@dC ZAdD ZBeCdfdEZDdF ZEy)J
SerialBasezh    Serial port base class. Provides __init__ function and properties to
    get/set port settings.
    )2   K   n            i,  iX  i  i  i`	  i  %  i K  i   i   i  i  i  i  i  i  i@B i  i` i i%& i- ig5 i 	= NrZ   Fc                 |   d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        || _        || _        || _        || _        || _        || _        |	| _        || _        || _        |
| _        || _        || _        d|v r|j?                  d      | _        d|v r|j?                  d      | _        |rtA        djC                  |            || jE                          yy)z        Initialize comm port object. If a "port" is given, then the port will be
        opened immediately. Otherwise a Serial port object in closed state
        is returned.
        FNTwriteTimeoutinterCharTimeoutz"unexpected keyword arguments: {!r})#is_openportstrname_port	_baudrate	_bytesize_parity	_stopbits_timeout_write_timeout_xonxoff_rtscts_dsrdtr_inter_byte_timeout_rs485_mode
_rts_state
_dtr_state_break_state
_exclusiveportbaudratebytesizeparitystopbitstimeoutwrite_timeoutxonxoffrtsctsdsrdtrinter_byte_timeout	exclusivepop
ValueErrorr   open)r:   rq   rr   rs   rt   ru   rv   rx   ry   rw   rz   r{   r|   kwargss                 r   r9   zSerialBase.__init__   sH   ( 	
"#' ! 	   *"4" V#!'N!;D'&,jj1C&DD#AHHPQQIIK r
   c                     | j                   S )z{        Get the current port setting. The value that was passed on init or using
        setPort() is passed back.
        )ra   rI   s    r   rq   zSerialBase.port   s     zzr
   c                    |3t        |t              s#t        dj                  t	        |                  | j
                  }|r| j                          || _        || _        | j                  | _	        |r| j                          yy)z!        Change the port.
        Nz'"port" must be None or a string, not {})r   
basestringr~   r   typer^   closer_   ra   r`   r   )r:   rq   was_opens      r   rq   zSerialBase.port  sn    
 JtZ$@FMMdSWjYZZ<<JJL
LL	IIK r
   c                     | j                   S )z"Get the current baud rate setting.)rb   rI   s    r   rr   zSerialBase.baudrate       ~~r
   c                     	 t        |      }|dk  rt        dj                  |            || _        | j                  r| j                          yy# t        $ r t        dj                  |            w xY w)z        Change baud rate. It raises a ValueError if the port is open and the
        baud rate is not possible. If the port is closed, then the value is
        accepted and the exception is raised when the port is opened.
        r   zNot a valid baudrate: {!r}N)intr~   r   rb   r^   _reconfigure_portr   )r:   rr   r   s      r   rr   zSerialBase.baudrate  sy    		)HA 1u !=!D!DX!NOODN||&&(   	L9@@JKK	Ls   A $A5c                     | j                   S )z"Get the current byte size setting.)rc   rI   s    r   rs   zSerialBase.bytesize-  r   r
   c                     || j                   vrt        dj                  |            || _        | j                  r| j                          yy)zChange byte size.zNot a valid byte size: {!r}N)	BYTESIZESr~   r   rc   r^   r   )r:   rs   s     r   rs   zSerialBase.bytesize2  sF     4>>):AA(KLL!<<""$ r
   c                     | j                   S )z)Get the current exclusive access setting.)rp   rI   s    r   r|   zSerialBase.exclusive;  s     r
   c                 L    || _         | j                  r| j                          yy)z$Change the exclusive access setting.N)rp   r^   r   )r:   r|   s     r   r|   zSerialBase.exclusive@  s#     $<<""$ r
   c                     | j                   S )zGet the current parity setting.)rd   rI   s    r   rt   zSerialBase.parityG       ||r
   c                     || j                   vrt        dj                  |            || _        | j                  r| j                          yy)zChange parity setting.zNot a valid parity: {!r}N)PARITIESr~   r   rd   r^   r   )r:   rt   s     r   rt   zSerialBase.parityL  sF     &7>>vFGG<<""$ r
   c                     | j                   S )z"Get the current stop bits setting.)re   rI   s    r   ru   zSerialBase.stopbitsU  r   r
   c                     || j                   vrt        dj                  |            || _        | j                  r| j                          yy)zChange stop bits size.zNot a valid stop bit size: {!r}N)STOPBITSr~   r   re   r^   r   )r:   ru   s     r   ru   zSerialBase.stopbitsZ  sF     4==(>EEhOPP!<<""$ r
   c                     | j                   S z Get the current timeout setting.)rf   rI   s    r   rv   zSerialBase.timeoutc       }}r
   c                     |%	 |dz    |dk  rt        dj                  |            || _        | j                  r| j                          yy# t         $ r t        dj                  |            w xY w)Change timeout setting.Nr   Not a valid timeout: {!r}r   )r   r~   r   rf   r^   r   r:   rv   s     r   rv   zSerialBase.timeouth  s     N! { !<!C!CG!LMM<<""$   N !<!C!CG!LMMNs   A $A1c                     | j                   S r   )rg   rI   s    r   rw   zSerialBase.write_timeoutv  s     """r
   c                     |%|dk  rt        dj                  |            	 |dz    || _        | j                  r| j                          yy# t        $ r t        dj                  |            w xY w)r   Nr   r   r   )r~   r   r   rg   r^   r   r   s     r   rw   zSerialBase.write_timeout{  s     { !<!C!CG!LMMN! &<<""$ 	  N !<!C!CG!LMMN   A $A1c                     | j                   S )z0Get the current inter-character timeout setting.)rk   rI   s    r   r{   zSerialBase.inter_byte_timeout  s     '''r
   c                     |%|dk  rt        dj                  |            	 |dz    || _        | j                  r| j                          yy# t        $ r t        dj                  |            w xY w)z"Change inter-byte timeout setting.Nr   r   r   )r~   r   r   rk   r^   r   )r:   
ic_timeouts     r   r{   zSerialBase.inter_byte_timeout  s     !A~ !<!C!CJ!OPPQQ $. <<""$ 	  Q !<!C!CJ!OPPQr   c                     | j                   S )z!Get the current XON/XOFF setting.)rh   rI   s    r   rx   zSerialBase.xonxoff  r   r
   c                 L    || _         | j                  r| j                          yy)zChange XON/XOFF setting.N)rh   r^   r   )r:   rx   s     r   rx   zSerialBase.xonxoff  s#      <<""$ r
   c                     | j                   S )z-Get the current RTS/CTS flow control setting.)ri   rI   s    r   ry   zSerialBase.rtscts  r   r
   c                 L    || _         | j                  r| j                          yy)z$Change RTS/CTS flow control setting.N)ri   r^   r   )r:   ry   s     r   ry   zSerialBase.rtscts  s#     <<""$ r
   c                     | j                   S )z-Get the current DSR/DTR flow control setting.)rj   rI   s    r   rz   zSerialBase.dsrdtr  r   r
   c                 t    || j                   | _        n|| _        | j                  r| j                          yy)z#Change DsrDtr flow control setting.N)ri   rj   r^   r   )r:   rz   s     r   rz   zSerialBase.dsrdtr  s5     ><<DL "DL<<""$ r
   c                     | j                   S N)rm   rI   s    r   rtszSerialBase.rts      r
   c                 L    || _         | j                  r| j                          y y r   )rm   r^   _update_rts_stater:   values     r   r   zSerialBase.rts  !    <<""$ r
   c                     | j                   S r   )rn   rI   s    r   dtrzSerialBase.dtr  r   r
   c                 L    || _         | j                  r| j                          y y r   )rn   r^   _update_dtr_stater   s     r   r   zSerialBase.dtr  r   r
   c                     | j                   S r   )ro   rI   s    r   break_conditionzSerialBase.break_condition  s       r
   c                 L    || _         | j                  r| j                          y y r   )ro   r^   _update_break_stater   s     r   r   zSerialBase.break_condition  s"    !<<$$& r
   c                     | j                   S )z        Enable RS485 mode and apply new settings, set to None to disable.
        See serial.rs485.RS485Settings for more info about the value.
        )rl   rI   s    r   
rs485_modezSerialBase.rs485_mode  s     r
   c                 L    || _         | j                  r| j                          y y r   )rl   r^   r   )r:   rs485_settingss     r   r   zSerialBase.rs485_mode  s"    )<<""$ r
   )
rr   rs   rt   ru   rx   rz   ry   rv   rw   r{   c                 p    t        | j                  D cg c]  }|t        | d|z         f c}      S c c}w )zb        Get current port settings as a dictionary. For use with
        apply_settings().
        _)dict_SAVED_SETTINGSgetattr)r:   keys     r   get_settingszSerialBase.get_settings  s3    
 @T@TUc74s34UVVUs   3c                 |    | j                   D ]-  }||v s||   t        | d|z         k7  st        | |||          / y)z        Apply stored settings from a dictionary returned from
        get_settings(). It's allowed to delete keys from the dictionary. These
        values will simply left unchanged.
        r   N)r   r   setattr)r:   dr   s      r   apply_settingszSerialBase.apply_settings  sF     '' 	+CaxAcFgdC#I&>>c1S6*	+r
   c                 d    dj                  | j                  j                  t        |       |       S )zAString representation of the current port settings and its state.z{name}<id=0x{id:x}, open={p.is_open}>(port={p.portstr!r}, baudrate={p.baudrate!r}, bytesize={p.bytesize!r}, parity={p.parity!r}, stopbits={p.stopbits!r}, timeout={p.timeout!r}, xonxoff={p.xonxoff!r}, rtscts={p.rtscts!r}, dsrdtr={p.dsrdtr!r}))r`   idp)r   r;   r   r   rI   s    r   __repr__zSerialBase.__repr__  s6    ; <B6//BtH <B <F	Fr
   c                      yNTr	   rI   s    r   readablezSerialBase.readable      r
   c                      yr   r	   rI   s    r   writablezSerialBase.writable  r   r
   c                      y)NFr	   rI   s    r   seekablezSerialBase.seekable!  s    r
   c                     | j                  t        |            }t        |      }	 ||d | |S # t        $ r<}dd l}t	        ||j                        s||j                  d|      |d | Y d }~|S d }~ww xY w)Nr   r   )readlenr   arrayr   )r:   r   datanerrr   s         r   readintozSerialBase.readinto$  sw    yyQ I	+AbqE   	+a-	KKT*AbqE	+s   . 	A31A..A3c                 V    | j                   | j                  s| j                          | S r   )ra   r^   r   rI   s    r   	__enter__zSerialBase.__enter__3  s     ::!$,,IIKr
   c                 $    | j                          y r   )r   )r:   argsr   s      r   __exit__zSerialBase.__exit__8  s    

r
   c                 v    | j                   s
t               d| _        t        j                  |       d| _        y)za        Send break condition. Timed, returns to idle state after given
        duration.
        TFN)r^   r6   r   rQ   sleeprF   s     r   
send_breakzSerialBase.send_break=  s0    
 ||"$$#

8$r
   c                 $    | j                          y r   )reset_input_bufferrI   s    r   
flushInputzSerialBase.flushInputK  s    !r
   c                 $    | j                          y r   )reset_output_bufferrI   s    r   flushOutputzSerialBase.flushOutputN  s      "r
   c                     | j                   S r   )
in_waitingrI   s    r   	inWaitingzSerialBase.inWaitingQ  s    r
   c                 &    | j                  |       y r   )r   rF   s     r   	sendBreakzSerialBase.sendBreakT  s    !r
   c                     || _         y r   )r   r   s     r   setRTSzSerialBase.setRTSW  	    r
   c                     || _         y r   )r   r   s     r   setDTRzSerialBase.setDTRZ  r   r
   c                     | j                   S r   )ctsrI   s    r   getCTSzSerialBase.getCTS]      xxr
   c                     | j                   S r   )dsrrI   s    r   getDSRzSerialBase.getDSR`  r   r
   c                     | j                   S r   )rirI   s    r   getRIzSerialBase.getRIc      wwr
   c                     | j                   S r   )cdrI   s    r   getCDzSerialBase.getCDf  r  r
   c                     || _         y r   )rq   )r:   rq   s     r   setPortzSerialBase.setPorti  s	    	r
   c                     | j                   S r   rw   rI   s    r   r\   zSerialBase.writeTimeoutl  s    !!!r
   c                     || _         y r   r  r   s     r   r\   zSerialBase.writeTimeoutp  s
    $r
   c                     | j                   S r   r{   rI   s    r   r]   zSerialBase.interCharTimeoutt  s    &&&r
   c                     || _         y r   r  )r:   r]   s     r   r]   zSerialBase.interCharTimeoutx  s
    "2r
   c                 "    | j                         S r   )r   rI   s    r   getSettingsDictzSerialBase.getSettingsDict|  s      ""r
   c                 &    | j                  |       y r   )r   )r:   r   s     r   applySettingsDictzSerialBase.applySettingsDict  s    Ar
   c                     | j                   S r   )r^   rI   s    r   isOpenzSerialBase.isOpen  s    ||r
   c                 8    | j                  | j                        S )zL        Read all bytes currently available in the buffer of the OS.
        )r   r   rI   s    r   read_allzSerialBase.read_all  s     yy))r
   c                 <   t        |      }t               }t        | j                        }	 | j	                  d      }|r6||z  }|| d |k(  r	 t        |      S |&t        |      |k\  r	 t        |      S 	 t        |      S |j                         r	 t        |      S r)z        Read until an expected sequence is found ('
' by default), the size
        is exceeded or until timeout occurs.
        r   N)r   r   r>   rf   r   rJ   r   )r:   expectedsizelentermlinerv   cs          r   
read_untilzSerialBase.read_until  s    
 h-{$--(		!A		?h. T{ #D	T(9
 T{  T{  T{ r
   c              /   @   K   	  | j                   |i |}|sy| w)zs        Read lines, implemented as generator. It will raise StopIteration on
        timeout (empty read).
        N)r  )r:   r   r   r  s       r   iread_untilzSerialBase.iread_until  s0     
 "4??D3F3DJ	 s   r   )g      ?)r   )Fr   r   r   r2   	BAUDRATESFIVEBITSSIXBITS	SEVENBITS	EIGHTBITSr   PARITY_NONEPARITY_EVEN
PARITY_ODDPARITY_MARKPARITY_SPACEr   STOPBITS_ONESTOPBITS_ONE_POINT_FIVESTOPBITS_TWOr   r9   propertyrq   setterrr   rs   r|   rt   ru   rv   rw   r{   rx   ry   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r\   r]   r  r  r  r  LFr  r  r	   r
   r   rS   rS      s%   ,I 7Iy9I[*k<PH5|DH ##&#$(@T   
[[    __) )"   __% %   % %   ]]% %   __% %   ^^% % # # % % ( ( % %   ^^% %   ]]% %   ]]	% 	%   	ZZ% %
   	ZZ% %
 ! ! ' '     % %-OW+F


	%"#" " " % % ' ' 3 3#* #%4 ,	r
   rS   __main__zport name:  {}
zbaud rates: {}
zbyte sizes: {}
zparities:   {}
zstop bits:  {}
z{}
)0
__future__r   iorQ   r   	NameErrorAttributeErrorobjectr   strr   r   r   XONXOFFCRr-  r#  r$  r%  r&  r'  r(  r)  r*  r  r   r!  r"  PARITY_NAMESIOErrorr0   r4   r6   r>   	RawIOBaserS   r   syssstdoutwriter   r`   r  r   r   r   r	   r
   r   <module>r?     s.   ' 	  %  tn~rd^rd^ CZ ?[*k<6A 3%|*6 ''9i '9g 9+_ +\ \82f 82vH HX zAJJ'..qvv67JJ'..q{{;<JJ'..q{{;<JJ'..qzz:;JJ'..qzz:;JJV]]1%& w 	>" V  	>" G
 	>" Js3   G% G> H %G;:G;>	H
H
HH