o
    &Úa_%U  ã                
   @   sð  d dl mZ d dlZd dlZze W n eefy%   G dd„ deƒZY nw ze W n eefy7   e	ZY nw ze
 W n eefyJ   e	fZ
Y nw dd„ Zd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„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"G dd„ dej#ƒZ$e%dkröd dl&Z&e$ƒ Z'e&j( )d  *e'j+¡¡ e&j( )d! *e'j,¡¡ e&j( )d" *e'j-¡¡ e&j( )d# *e'j.¡¡ e&j( )d$ *e'j/¡¡ e&j( )d% *e'¡¡ dS dS )&é    )Úabsolute_importNc                   @   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   sD   � t | tƒr
|  ¡ } d}	 | ||d … }|d7 }|r|V  ndS q)z=Iterate over bytes, returning bytes instead of ints (python3)r   Té   N)Ú
isinstancer   Útobytes)ÚbÚiÚar   r   r   Ú	iterbytes(   s   €
úr   c                 C   sV   t | tƒr| S t | tƒrt| ƒS t | tƒr|  ¡ S t | tƒr%td | ¡ƒ‚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)Úseqr   r   r   Úto_bytes8   s   



r   é   é   é   é
   )ÚNÚEÚOÚMÚS)r	   g      ø?é   )é   é   é   é   ÚNoneÚEvenÚOddÚMarkÚSpacec                   @   ó   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+   \   ó    r+   c                   @   r*   )ÚSerialTimeoutExceptionz Write timeouts give an exceptionNr,   r   r   r   r   r/   `   r.   r/   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPortNotOpenErrorzPort is not openc                    s   t t| ƒ d¡ d S )Nz)Attempting to use a port that is not open)Úsuperr0   Ú__init__©Úself©Ú	__class__r   r   r2   f   s   zPortNotOpenError.__init__)r   r   r   r-   r2   Ú__classcell__r   r   r5   r   r0   d   s    r0   c                   @   sH   e Zd ZdZeedƒrejZnejZdd„ Zdd„ Z	dd„ Z
d	d
„ ZdS )Ú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                 C   s>   |du | _ |dk| _|| _|dur|  ¡ | | _dS d| _dS )z(Initialize a timeout with given durationNr   )Úis_infiniteÚis_non_blockingÚdurationÚTIMEÚtarget_time©r4   r<   r   r   r   r2      s   


zTimeout.__init__c                 C   s   | j duo
|  ¡ dkS )z4Return a boolean, telling if the timeout has expiredNr   )r>   Ú	time_leftr3   r   r   r   Úexpired‰   s   zTimeout.expiredc                 C   sL   | j rdS | jr
dS | j|  ¡  }|| jkr!|  ¡ | j | _| jS td|ƒS )z:Return how many seconds are left until the timeout expiresr   N)r;   r:   r>   r=   r<   Úmax)r4   Údeltar   r   r   r@   �   s   

zTimeout.time_leftc                 C   s   || _ |  ¡ | | _dS )zb        Restart a timeout, only supported if a timeout was already set up
        before.
        N)r<   r=   r>   r?   r   r   r   Úrestartœ   s   zTimeout.restartN)r   r   r   r-   ÚhasattrÚtimer9   r=   r2   rA   r@   rD   r   r   r   r   r8   j   s    

r8   c                   @   s  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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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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dwd+d*„ƒZ"ed,d-„ ƒZ#e#jd.d-„ ƒZ#ed/d0„ ƒZ$e$jd1d0„ ƒZ$ed2d3„ ƒZ%e%jd4d3„ ƒZ%ed5d6„ ƒZ&e&jd7d6„ ƒZ&d8Z'd9d:„ Z(d;d<„ Z)d=d>„ Z*d?d@„ Z+dAdB„ Z,dCdD„ Z-dEdF„ Z.dGdH„ Z/dIdJ„ Z0dxdLdM„Z1dNdO„ Z2dPdQ„ Z3dRdS„ Z4dxdTdU„Z5dydWdX„Z6dydYdZ„Z7d[d\„ Z8d]d^„ Z9d_d`„ Z:dadb„ Z;dcdd„ Z<ededf„ ƒZ=e=jdgdf„ ƒZ=edhdi„ ƒZ>e>jdjdi„ ƒZ>dkdl„ Z?dmdn„ Z@dodp„ ZAdqdr„ ZBeCdfdsdt„ZDdudv„ ZEdS )zÚ
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ÀÆ- iàg5 i 	= NrN   Fc                 K   s  d| _ d| _d| _d| _d| _d| _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _d| _d| _d| _|| _|| _|| _|| _|| _|| _|	| _|| _|| _|
| _|| _|| _d|v rg| d¡| _d|v rq| d¡| _|rzt d !|¡ƒ‚|dur„|  "¡  dS 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Ú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)r4   rd   re   rf   rg   rh   ri   rk   rl   rj   rm   rn   ro   Úkwargsr   r   r   r2   ´   sP   ÿzSerialBase.__init__c                 C   ó   | j S )z{        Get the current port setting. The value that was passed on init or using
        setPort() is passed back.
        )rT   r3   r   r   r   rd   þ   ó   zSerialBase.portc                 C   s^   |durt |tƒstd t|ƒ¡ƒ‚| j}|r|  ¡  || _|| _| j| _	|r-|  
¡  dS dS )z!        Change the port.
        Nz'"port" must be None or a string, not {})r
   Ú
basestringrq   r   ÚtyperQ   ÚcloserR   rT   rS   rr   )r4   rd   Úwas_openr   r   r   rd     s   ÿc                 C   rt   )z"Get the current baud rate setting.)rU   r3   r   r   r   re     ó   zSerialBase.baudratec                 C   s\   zt |ƒ}W n ty   td |¡ƒ‚w |dk r td |¡ƒ‚|| _| jr,|  ¡  dS 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: {!r}r   N)Úintr   rq   r   rU   rQ   Ú_reconfigure_port)r4   re   r   r   r   r   re     s   ÿÿc                 C   rt   )z"Get the current byte size setting.)rV   r3   r   r   r   rf   -  rz   zSerialBase.bytesizec                 C   ó4   || j vrtd |¡ƒ‚|| _| jr|  ¡  dS dS )zChange byte size.zNot a valid byte size: {!r}N)Ú	BYTESIZESrq   r   rV   rQ   r|   )r4   rf   r   r   r   rf   2  ó   
ÿc                 C   rt   )z)Get the current exclusive access setting.)rc   r3   r   r   r   ro   ;  rz   zSerialBase.exclusivec                 C   ó   || _ | jr|  ¡  dS dS )z$Change the exclusive access setting.N)rc   rQ   r|   )r4   ro   r   r   r   ro   @  ó   ÿc                 C   rt   )zGet the current parity setting.)rW   r3   r   r   r   rg   G  rz   zSerialBase.parityc                 C   r}   )zChange parity setting.zNot a valid parity: {!r}N)ÚPARITIESrq   r   rW   rQ   r|   )r4   rg   r   r   r   rg   L  r   c                 C   rt   )z"Get the current stop bits setting.)rX   r3   r   r   r   rh   U  rz   zSerialBase.stopbitsc                 C   r}   )zChange stop bits size.zNot a valid stop bit size: {!r}N)ÚSTOPBITSrq   r   rX   rQ   r|   )r4   rh   r   r   r   rh   Z  r   c                 C   rt   ©z Get the current timeout setting.)rY   r3   r   r   r   ri   c  rz   zSerialBase.timeoutc                 C   sd   |dur$z|d  W n t y   td |¡ƒ‚w |dk r$td |¡ƒ‚|| _| jr0|  ¡  dS dS )úChange timeout setting.Nr	   úNot a valid timeout: {!r}r   )r   rq   r   rY   rQ   r|   ©r4   ri   r   r   r   ri   h  s   ÿÿc                 C   rt   r„   )rZ   r3   r   r   r   rj   v  rz   zSerialBase.write_timeoutc                 C   ód   |dur$|dk rt d |¡ƒ‚z|d  W n ty#   t d |¡ƒ‚w || _| jr0|  ¡  dS dS )r…   Nr   r†   r	   )rq   r   r   rZ   rQ   r|   r‡   r   r   r   rj   {  ó   ÿÿc                 C   rt   )z0Get the current inter-character timeout setting.)r^   r3   r   r   r   rn   Š  rz   zSerialBase.inter_byte_timeoutc                 C   rˆ   )z"Change inter-byte timeout setting.Nr   r†   r	   )rq   r   r   r^   rQ   r|   )r4   Ú
ic_timeoutr   r   r   rn   �  r‰   c                 C   rt   )z!Get the current XON/XOFF setting.)r[   r3   r   r   r   rk   ž  rz   zSerialBase.xonxoffc                 C   r€   )zChange XON/XOFF setting.N)r[   rQ   r|   )r4   rk   r   r   r   rk   £  r�   c                 C   rt   )z-Get the current RTS/CTS flow control setting.)r\   r3   r   r   r   rl   ª  rz   zSerialBase.rtsctsc                 C   r€   )z$Change RTS/CTS flow control setting.N)r\   rQ   r|   )r4   rl   r   r   r   rl   ¯  r�   c                 C   rt   )z-Get the current DSR/DTR flow control setting.)r]   r3   r   r   r   rm   ¶  rz   zSerialBase.dsrdtrc                 C   s.   |du r	| j | _n|| _| jr|  ¡  dS dS )z#Change DsrDtr flow control setting.N)r\   r]   rQ   r|   )r4   rm   r   r   r   rm   »  s   
ÿc                 C   rt   ©N)r`   r3   r   r   r   ÚrtsÇ  ó   zSerialBase.rtsc                 C   ó   || _ | jr|  ¡  d S d S r‹   )r`   rQ   Ú_update_rts_state©r4   Úvaluer   r   r   rŒ   Ë  ó   ÿc                 C   rt   r‹   )ra   r3   r   r   r   ÚdtrÑ  r�   zSerialBase.dtrc                 C   rŽ   r‹   )ra   rQ   Ú_update_dtr_stater�   r   r   r   r“   Õ  r’   c                 C   rt   r‹   )rb   r3   r   r   r   Úbreak_conditionÛ  r�   zSerialBase.break_conditionc                 C   rŽ   r‹   )rb   rQ   Ú_update_break_stater�   r   r   r   r•   ß  r’   c                 C   rt   )z˜        Enable RS485 mode and apply new settings, set to None to disable.
        See serial.rs485.RS485Settings for more info about the value.
        )r_   r3   r   r   r   Ú
rs485_modeè  ru   zSerialBase.rs485_modec                 C   rŽ   r‹   )r_   rQ   r|   )r4   Úrs485_settingsr   r   r   r—   ð  r’   )
re   rf   rg   rh   rk   rm   rl   ri   rj   rn   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Úkeyr3   r   r   Ú
<listcomp>  s    z+SerialBase.get_settings.<locals>.<listcomp>)ÚdictÚ_SAVED_SETTINGSr3   r   r3   r   Úget_settingsü  s   zSerialBase.get_settingsc                 C   s>   | j D ]}||v r|| t| d| ƒkrt| ||| ƒ qd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.
        r™   N)rŸ   rš   Úsetattr)r4   Údrœ   r   r   r   Úapply_settings  s
   
€þzSerialBase.apply_settingsc                 C   s   dj | jjt| ƒ| d�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}))rS   ÚidÚp)r   r6   r   r¤   r3   r   r   r   Ú__repr__  s   üzSerialBase.__repr__c                 C   ó   dS ©NTr   r3   r   r   r   Úreadable  ó   zSerialBase.readablec                 C   r§   r¨   r   r3   r   r   r   Úwritable  rª   zSerialBase.writablec                 C   r§   )NFr   r3   r   r   r   Úseekable!  rª   zSerialBase.seekablec              
   C   s|   |   t|ƒ¡}t|ƒ}z	||d |…< W |S  ty= } zdd l}t||jƒs(|‚| d|¡|d |…< W Y d }~|S d }~ww )Nr   r   )ÚreadÚlenr   Úarrayr
   )r4   r   ÚdataÚnÚerrr¯   r   r   r   Úreadinto$  s   û€ûzSerialBase.readintoc                 C   s   | j d ur| js|  ¡  | S r‹   )rT   rQ   rr   r3   r   r   r   Ú	__enter__3  s   zSerialBase.__enter__c                 O   ó   |   ¡  d S r‹   )rx   )r4   Úargsrs   r   r   r   Ú__exit__8  ó   zSerialBase.__exit__ç      Ð?c                 C   s&   | j stƒ ‚d| _t |¡ d| _dS )za        Send break condition. Timed, returns to idle state after given
        duration.
        TFN)rQ   r0   r•   rF   Úsleepr?   r   r   r   Ú
send_break=  s
   

zSerialBase.send_breakc                 C   rµ   r‹   )Úreset_input_bufferr3   r   r   r   Ú
flushInputK  r¸   zSerialBase.flushInputc                 C   rµ   r‹   )Úreset_output_bufferr3   r   r   r   ÚflushOutputN  r¸   zSerialBase.flushOutputc                 C   rt   r‹   )Ú
in_waitingr3   r   r   r   Ú	inWaitingQ  ó   zSerialBase.inWaitingc                 C   ó   |   |¡ d S r‹   )r»   r?   r   r   r   Ú	sendBreakT  ó   zSerialBase.sendBreakr	   c                 C   ó
   || _ d S r‹   )rŒ   r�   r   r   r   ÚsetRTSW  ó   
zSerialBase.setRTSc                 C   rÆ   r‹   )r“   r�   r   r   r   ÚsetDTRZ  rÈ   zSerialBase.setDTRc                 C   rt   r‹   )Úctsr3   r   r   r   ÚgetCTS]  rÂ   zSerialBase.getCTSc                 C   rt   r‹   )Údsrr3   r   r   r   ÚgetDSR`  rÂ   zSerialBase.getDSRc                 C   rt   r‹   )Úrir3   r   r   r   ÚgetRIc  rÂ   zSerialBase.getRIc                 C   rt   r‹   )Úcdr3   r   r   r   ÚgetCDf  rÂ   zSerialBase.getCDc                 C   rÆ   r‹   )rd   )r4   rd   r   r   r   ÚsetPorti  rÈ   zSerialBase.setPortc                 C   rt   r‹   ©rj   r3   r   r   r   rO   l  r�   zSerialBase.writeTimeoutc                 C   rÆ   r‹   rÓ   r‡   r   r   r   rO   p  ó   
c                 C   rt   r‹   ©rn   r3   r   r   r   rP   t  r�   zSerialBase.interCharTimeoutc                 C   rÆ   r‹   rÕ   )r4   rP   r   r   r   rP   x  rÔ   c                 C   s   |   ¡ S r‹   )r    r3   r   r   r   ÚgetSettingsDict|  s   zSerialBase.getSettingsDictc                 C   rÃ   r‹   )r£   )r4   r¢   r   r   r   ÚapplySettingsDict  rÅ   zSerialBase.applySettingsDictc                 C   rt   r‹   )rQ   r3   r   r   r   ÚisOpen‚  rÂ   zSerialBase.isOpenc                 C   s   |   | j¡S )zL        Read all bytes currently available in the buffer of the OS.
        )r­   rÀ   r3   r   r   r   Úread_allˆ  s   zSerialBase.read_allc                 C   sŠ   t |ƒ}tƒ }t| jƒ}	 |  d¡}|r6||7 }|| d… |kr&	 t|ƒS |dur5t |ƒ|kr5	 t|ƒS n	 t|ƒS | ¡ rD	 t|ƒS q)z�        Read until an expected sequence is found ('
' by default), the size
        is exceeded or until timeout occurs.
        Tr	   N)r®   r   r8   rY   r­   rA   r   )r4   ÚexpectedÚsizeÚlentermÚlineri   Úcr   r   r   Ú
read_untilŽ  s(   

ú€ýþôzSerialBase.read_untilc                 o   s$   � 	 | j |i |¤Ž}|sdS |V  q)zs        Read lines, implemented as generator. It will raise StopIteration on
        timeout (empty read).
        TN)rß   )r4   r¶   rs   rÝ   r   r   r   Úiread_until¤  s   €üzSerialBase.iread_untilr‹   )r¹   )r	   )Fr   r   r   r-   Ú	BAUDRATESÚFIVEBITSÚSIXBITSÚ	SEVENBITSÚ	EIGHTBITSr~   ÚPARITY_NONEÚPARITY_EVENÚ
PARITY_ODDÚPARITY_MARKÚPARITY_SPACEr‚   ÚSTOPBITS_ONEÚSTOPBITS_ONE_POINT_FIVEÚSTOPBITS_TWOrƒ   r2   Úpropertyrd   Úsetterre   rf   ro   rg   rh   ri   rj   rn   rk   rl   rm   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Ò   rO   rP   rÖ   r×   rØ   rÙ   ÚLFrß   rà   r   r   r   r   rG   ¥   sî    

ôJ






































rG   Ú__main__zport name:  {}
zbaud rates: {}
zbyte sizes: {}
zparities:   {}
zstop bits:  {}
z{}
)0Ú
__future__r   ÚiorF   r   Ú	NameErrorÚAttributeErrorÚobjectr   Ústrrv   r   r   ÚXONÚXOFFÚCRrð   ræ   rç   rè   ré   rê   rë   rì   rí   râ   rã   rä   rå   ÚPARITY_NAMESÚIOErrorr+   r/   r0   r8   Ú	RawIOBaserG   r   ÚsysÚsÚstdoutÚwriter   rS   rá   r~   r‚   rƒ   r   r   r   r   Ú<module>   sh   	ýÿ
ÿ




û	;    ø