
MV6L                 @   sU  d  d l  Z  d  d l Z y e Wn. e e f k
 rP Gd d   d e  Z Yn Xy e Wn e e f k
 ry e Z Yn Xd d   Z d d   Z	 e	 d g  Z
 e	 d	 g  Z e	 d
 g  Z e	 d g  Z d d d d d f \ Z Z Z Z Z d d d f \ Z Z Z d d d d f \ Z Z Z Z e d e d e d e d e d i Z Gd d   d e  Z Gd d    d  e  Z e d!  Z e d"  Z Gd# d$   d$ e  j   Z! e" d% k rQd  d l# Z# e!   Z$ e# j% j& d& e$ j'  e# j% j& d' e$ j(  e# j% j& d( e$ j)  e# j% j& d) e$ j*  e# j% j& d* e$ j+  e# j% j& d+ e$  d S),    Nc               @   s   e  Z d  Z d S)
memoryviewN)__name__
__module____qualname__ r   r   3/usr/lib/python3/dist-packages/serial/serialutil.pyr      s   r   c             c   sY   t  |  t  r |  j   }  d } x1 |  | | d  } | d 7} | rP | Vq$ Pq$ Wd S)z=Iterate over bytes, returning bytes instead of ints (python3)r      N)
isinstancer   tobytes)bxar   r   r   	iterbytes!   s    
r   c             C   s   t  |  t  r |  St  |  t  r, t |   St  |  t  rE |  j   St  |  t  rj t d |  f   nL t   } x6 |  D]. } t  | t  r t |  } | j |  qz Wt |  Sd S)z"convert a sequence to a bytes typez=unicode strings are not supported, please encode to bytes: %rN)	r	   bytes	bytearrayr   r
   unicode	TypeErrorordappend)seqr   itemr   r   r   to_bytes1   s    

	r            
   NEOMSr   g      ?               NoneZEvenZOddZMarkZSpacec               @   s   e  Z d  Z d Z d S)SerialExceptionz.Base class for serial port related exceptions.N)r   r   r   __doc__r   r   r   r   r'   Z   s   r'   c               @   s   e  Z d  Z d Z d S)SerialTimeoutExceptionz Write timeouts give an exceptionN)r   r   r   r(   r   r   r   r   r)   ^   s   r)   zWrite timeoutz)Attempting to use a port that is not openc               @   si  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" d#  Z e d$ d%    Z e j d& d%    Z e d' d(    Z e j d) d(    Z e d* d+    Z e j d, d+    Z e d- d.    Z e j d/ d.    Z e d0 d1    Z e j d2 d1    Z e d3 d4    Z e j d5 d4    Z e d6 d7    Z e j d8 d7    Z e d9 d:    Z e j d; d:    Z e d< d=    Z e j d> d=    Z e d? d@    Z  e  j dA d@    Z  e dB dC    Z! e! j d  dD dC   Z! e dE dF    Z" e" j dG dF    Z" e dH dI    Z# e# j dJ dI    Z# e dK dL    Z$ e$ j dM dL    Z$ e dN dO    Z% e% j dP dO    Z% d Z& d[ d\   Z' d] d^   Z( d_ d`   Z) da db   Z* dc dd   Z+ de df   Z, dg dh   Z- di dj   Z. dk dl   Z/ dm dn do  Z0 dp dq   Z1 dr ds   Z2 dt du   Z3 dm dv dw  Z4 dx dy dz  Z5 dx d{ d|  Z6 d} d~   Z7 d d   Z8 d d   Z9 d d   Z: e d d    Z; e; j d d    Z; e d d    Z< e< j d d    Z< d d   Z= d d   Z> d d   Z? d d   Z@ eA d  d d  ZB d d   ZC d  S)
SerialBasezh    Serial port base class. Provides __init__ function and properties to
    get/set port settings.
    2   K   n            ,  X      `	    %   K                    @B   ` 逄 %& - g5  	= NFc             K   sb  d |  _  d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _	 d |  _
 d |  _ d |  _ d |  _ d |  _ d |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ | |  _ | |  _ |
 |  _ | |  _ d | k r| j d  |  _ d | k r/| j d  |  _ | rHt d | f   | d k	 r^|  j   d S)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_port	_baudrate	_bytesize_parity	_stopbits_timeout_write_timeout_xonxoff_rtscts_dsrdtr_inter_byte_timeout_rs485_mode
_rts_state
_dtr_state_break_stateportbaudratebytesizeparitystopbitstimeoutwrite_timeoutxonxoffrtsctsdsrdtrinter_byte_timeoutpop
ValueErroropen)selfr[   r\   r]   r^   r_   r`   rb   rc   ra   rd   re   kwargsr   r   r   __init__u   sF    																											zSerialBase.__init__c             C   s   |  j  S)z        Get the current port setting. The value that was passed on init or using
        setPort() is passed back. See also the attribute portstr which contains
        the name of the port as a string.
        )rL   )ri   r   r   r   r[      s    zSerialBase.portc             C   sK   |  j  } | r |  j   | |  _ | |  _ |  j |  _ | rG |  j   d S)zv        Change the port. The attribute portstr is set to a string that
        contains the name of the port.
        N)rK   closeportstrrL   namerh   )ri   r[   Zwas_openr   r   r   r[      s    	
		c             C   s   |  j  S)z"Get the current baud rate setting.)rM   )ri   r   r   r   r\      s    zSerialBase.baudratec             C   sw   y t  |  } Wn% t k
 r7 t d | f   Yn< X| d k rW t d | f   | |  _ |  j rs |  j   d S)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.
        zNot a valid baudrate: %rr   N)intr   rg   rM   rK   _reconfigure_port)ri   r\   r   r   r   r   r\      s    		c             C   s   |  j  S)z"Get the current byte size setting.)rN   )ri   r   r   r   r]      s    zSerialBase.bytesizec             C   sB   | |  j  k r" t d | f   | |  _ |  j r> |  j   d S)zChange byte size.zNot a valid byte size: %rN)	BYTESIZESrg   rN   rK   rp   )ri   r]   r   r   r   r]      s
    		c             C   s   |  j  S)zGet the current parity setting.)rO   )ri   r   r   r   r^      s    zSerialBase.parityc             C   sB   | |  j  k r" t d | f   | |  _ |  j r> |  j   d S)zChange parity setting.zNot a valid parity: %rN)PARITIESrg   rO   rK   rp   )ri   r^   r   r   r   r^     s
    		c             C   s   |  j  S)z"Get the current stop bits setting.)rP   )ri   r   r   r   r_     s    zSerialBase.stopbitsc             C   sB   | |  j  k r" t d | f   | |  _ |  j r> |  j   d S)zChange stop bits size.zNot a valid stop bit size: %rN)STOPBITSrg   rP   rK   rp   )ri   r_   r   r   r   r_     s
    		c             C   s   |  j  S)z Get the current timeout setting.)rQ   )ri   r   r   r   r`     s    zSerialBase.timeoutc             C   s   | d k	 r_ y | d Wn% t  k
 r? t d | f   Yn X| d k  r_ t d | f   | |  _ |  j r{ |  j   d S)zChange timeout setting.Nr   zNot a valid timeout: %rr   )r   rg   rQ   rK   rp   )ri   r`   r   r   r   r`   $  s    		c             C   s   |  j  S)z Get the current timeout setting.)rR   )ri   r   r   r   ra   3  s    zSerialBase.write_timeoutc             C   s|   | d k	 r\ | d k  r+ t  d | f   y | d Wn" t k
 r[ t  d |   Yn X| |  _ |  j rx |  j   d S)zChange timeout setting.Nr   zNot a valid timeout: %rr   )rg   r   rR   rK   rp   )ri   r`   r   r   r   ra   8  s    		c             C   s   |  j  S)z0Get the current inter-character timeout setting.)rV   )ri   r   r   r   re   H  s    zSerialBase.inter_byte_timeoutc             C   sy   | d k	 rY | d k  r( t  d |   y | d Wn" t k
 rX t  d |   Yn X| |  _ |  j ru |  j   d S)z"Change inter-byte timeout setting.Nr   zNot a valid timeout: %rr   )rg   r   rV   rK   rp   )ri   Z
ic_timeoutr   r   r   re   M  s    		c             C   s   |  j  S)z!Get the current XON/XOFF setting.)rS   )ri   r   r   r   rb   ]  s    zSerialBase.xonxoffc             C   s    | |  _  |  j r |  j   d S)zChange XON/XOFF setting.N)rS   rK   rp   )ri   rb   r   r   r   rb   b  s    		c             C   s   |  j  S)z-Get the current RTS/CTS flow control setting.)rT   )ri   r   r   r   rc   j  s    zSerialBase.rtsctsc             C   s    | |  _  |  j r |  j   d S)z$Change RTS/CTS flow control setting.N)rT   rK   rp   )ri   rc   r   r   r   rc   o  s    		c             C   s   |  j  S)z-Get the current DSR/DTR flow control setting.)rU   )ri   r   r   r   rd   w  s    zSerialBase.dsrdtrc             C   s;   | d k r |  j  |  _ n	 | |  _ |  j r7 |  j   d S)z#Change DsrDtr flow control setting.N)rT   rU   rK   rp   )ri   rd   r   r   r   rd   |  s
    		c             C   s   |  j  S)N)rX   )ri   r   r   r   rts  s    zSerialBase.rtsc             C   s    | |  _  |  j r |  j   d  S)N)rX   rK   Z_update_rts_state)ri   valuer   r   r   rt     s    		c             C   s   |  j  S)N)rY   )ri   r   r   r   dtr  s    zSerialBase.dtrc             C   s    | |  _  |  j r |  j   d  S)N)rY   rK   Z_update_dtr_state)ri   ru   r   r   r   rv     s    		c             C   s   |  j  S)N)rZ   )ri   r   r   r   break_condition  s    zSerialBase.break_conditionc             C   s    | |  _  |  j r |  j   d  S)N)rZ   rK   Z_update_break_state)ri   ru   r   r   r   rw     s    		c             C   s   |  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.
        )rW   )ri   r   r   r   
rs485_mode  s    zSerialBase.rs485_modec             C   s    | |  _  |  j r |  j   d  S)N)rW   rK   rp   )ri   Zrs485_settingsr   r   r   rx     s    		r\   r]   r^   r_   rb   rd   rc   r`   ra   re   c                s    t    f d d     j D  S)zb        Get current port settings as a dictionary. For use with
        apply_settings().
        c                s)   g  |  ] } | t    d  |  f  q S)_)getattr).0key)ri   r   r   
<listcomp>  s   	 z+SerialBase.get_settings.<locals>.<listcomp>)dict_SAVED_SETTINGS)ri   r   )ri   r   get_settings  s    zSerialBase.get_settingsc             C   sU   xN |  j  D]C } | | k r
 | | t |  d |  k r
 t |  | | |  q
 Wd S)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.
        ry   N)r   rz   setattr)ri   dr|   r   r   r   apply_settings  s    )zSerialBase.apply_settingsc             C   sV   d |  j  j t |   |  j |  j |  j |  j |  j |  j |  j	 |  j
 |  j |  j f S)zAString representation of the current port settings and its state.z}%s<id=0x%x, open=%s>(port=%r, baudrate=%r, bytesize=%r, parity=%r, stopbits=%r, timeout=%r, xonxoff=%r, rtscts=%r, dsrdtr=%r))	__class__r   idrK   rm   r\   r]   r^   r_   r`   rb   rc   rd   )ri   r   r   r   __repr__  s    		zSerialBase.__repr__c             C   s   d S)NTr   )ri   r   r   r   readable  s    zSerialBase.readablec             C   s   d S)NTr   )ri   r   r   r   writable  s    zSerialBase.writablec             C   s   d S)NFr   )ri   r   r   r   seekable  s    zSerialBase.seekablec             C   s   |  j  t |   } t |  } y | | d  |  <Wne t k
 r } zE d d  l } t | | j  sn |  | j d |  | d  |  <WYd  d  } ~ Xn X| S)Nr   r   )readlenr   arrayr	   )ri   r   datanerrr   r   r   r   readinto  s    /zSerialBase.readintoc             C   s   |  S)Nr   )ri   r   r   r   	__enter__  s    zSerialBase.__enter__c             O   s   |  j    d  S)N)rl   )ri   argsrj   r   r   r   __exit__   s    zSerialBase.__exit__g      ?c             C   s2   |  j  s t  d |  _ t j |  d |  _ d S)za        Send break condition. Timed, returns to idle state after given
        duration.
        TFN)rK   portNotOpenErrorrw   timeZsleep)ri   durationr   r   r   
send_break  s
    		zSerialBase.send_breakc             C   s   |  j    d  S)N)Zreset_input_buffer)ri   r   r   r   
flushInput  s    zSerialBase.flushInputc             C   s   |  j    d  S)N)Zreset_output_buffer)ri   r   r   r   flushOutput  s    zSerialBase.flushOutputc             C   s   |  j  S)N)
in_waiting)ri   r   r   r   	inWaiting  s    zSerialBase.inWaitingc             C   s   |  j  |  d  S)N)r   )ri   r   r   r   r   	sendBreak  s    zSerialBase.sendBreakr   c             C   s   | |  _  d  S)N)rt   )ri   ru   r   r   r   setRTS  s    zSerialBase.setRTSc             C   s   | |  _  d  S)N)rv   )ri   ru   r   r   r   setDTR"  s    zSerialBase.setDTRc             C   s   |  j  S)N)Zcts)ri   r   r   r   getCTS%  s    zSerialBase.getCTSc             C   s   |  j  S)N)Zdsr)ri   r   r   r   getDSR(  s    zSerialBase.getDSRc             C   s   |  j  S)N)Zri)ri   r   r   r   getRI+  s    zSerialBase.getRIc             C   s   |  j  S)N)Zcd)ri   r   r   r   getCD.  s    zSerialBase.getCDc             C   s   |  j  S)N)ra   )ri   r   r   r   rI   1  s    zSerialBase.writeTimeoutc             C   s   | |  _  d  S)N)ra   )ri   r`   r   r   r   rI   5  s    c             C   s   |  j  S)N)re   )ri   r   r   r   rJ   9  s    zSerialBase.interCharTimeoutc             C   s   | |  _  d  S)N)re   )ri   rJ   r   r   r   rJ   =  s    c             C   s
   |  j    S)N)r   )ri   r   r   r   getSettingsDictA  s    zSerialBase.getSettingsDictc             C   s   |  j  |  d  S)N)r   )ri   r   r   r   r   applySettingsDictD  s    zSerialBase.applySettingsDictc             C   s   |  j  S)N)rK   )ri   r   r   r   isOpenG  s    zSerialBase.isOpenc             C   s   |  j  |  j  S)zL        Read all bytes currently available in the buffer of the OS.
        )r   r   )ri   r   r   r   read_allM  s    zSerialBase.read_allc             C   s   t  |  } t   } x^ |  j d  } | rq | | 7} | | d  | k rO P| d k	 rr t  |  | k rr Pq Pq Wt |  S)z        Read until a termination sequence is found ('
' by default), the size
        is exceeded or until timeout occurs.
        r   N)r   r   r   r   )ri   
terminatorsizeZlentermlinecr   r   r   
read_untilS  s    	
zSerialBase.read_untilc             o   s)   x" |  j  | |   } | s P| Vq Wd S)zs        Read lines, implemented as generator. It will raise StopIteration on
        timeout (empty read).
        N)r   )ri   r   rj   r   r   r   r   iread_untilf  s
    zSerialBase.iread_until)r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   )
zbaudratezbytesizezparityzstopbitszxonxoffzdsrdtrzrtsctsztimeoutzwrite_timeoutzinter_byte_timeout)Dr   r   r   r(   	BAUDRATESFIVEBITSSIXBITS	SEVENBITS	EIGHTBITSrq   PARITY_NONEPARITY_EVEN
PARITY_ODDPARITY_MARKPARITY_SPACErr   STOPBITS_ONESTOPBITS_ONE_POINT_FIVESTOPBITS_TWOrs   rk   propertyr[   setterr\   r]   r^   r_   r`   ra   re   rb   rc   rd   rt   rv   rw   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rI   rJ   r   r   r   r   LFr   r   r   r   r   r   r*   f   s      :	
	  r*   __main__zport name:  %s
zbaud rates: %s
zbyte sizes: %s
zparities:   %s
zstop bits:  %s
z%s
),ior   r   	NameErrorAttributeErrorobjectr   strr   r   ZXONZXOFFZCRr   r   r   r   r   r   r   r   r   r   r   r   r   ZPARITY_NAMESIOErrorr'   r)   ZwriteTimeoutErrorr   	RawIOBaser*   r   syssstdoutwritern   r   rq   rr   rs   r   r   r   r   <module>
   sN   $  	