o
    l@xd#                     @   s   d dl mZ d dlZd dlZd dlZd dlmZ zd dlZW n ey-   d dl	m
Z Y nw dd Zdd ZG dd	 d	eZG d
d deZG dd dejZedkr`edZde_ee dS dS )    )absolute_importN)to_bytesc                 c   s    d}t | D ]2}dt|d|  krdk r!n n|dndfV  |d7 }|dkr1d	V  q|d
kr:dV  d}q|dkrZ|d
k rU|d7 }|dkrNd	V  dV  |d
k sCdV  dS dS )z    yield tuples of hex and ASCII display in multiples of 16. Includes a
    space after 8 bytes and (None, None) after 16 bytes and at the end.
    r   z{:02X}        ascii.      )     )NN)z   r
   N)serial	iterbytesformatorddecode)datanb r   FD:\rfid_django\env\Lib\site-packages\serial/urlhandler/protocol_spy.pysixteen%   s(   6
r   c                 c   s~    g }g }d}t | D ]1\}}|du r2|dd|d|gfV  |dd= |dd= |d7 }q|| || qdS )z yield lines with hexdump of datar   Nr
   r   r   )r   joinappend)r   valuesr   offsethar   r   r   hexdump<   s    



r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )	FormatRawz&Forward only RX and TX data to output.c                 C   s   || _ || _d| _d| _d S )N[32m[31m)outputcolorrx_colortx_colorselfr"   r#   r   r   r   __init__O   s   
zFormatRaw.__init__c                 C   .   | j r
| j| j | j| | j  dS )zshow received dataN)r#   r"   writer$   flushr'   r   r   r   r   rxU      zFormatRaw.rxc                 C   r)   )zshow transmitted dataN)r#   r"   r*   r%   r+   r,   r   r   r   tx\   r.   zFormatRaw.txc                 C   s   dS )z(do not) show control callsNr   r'   namevaluer   r   r   controlc   s   zFormatRaw.controlN)__name__
__module____qualname____doc__r(   r-   r/   r3   r   r   r   r   r   L   s    r   c                   @   s:   e Zd ZdZdd ZdddZdd Zd	d
 Zdd ZdS )FormatHexdumpax      Create a hex dump of RX ad TX data, show when control lines are read or
    written.

    output example::

        000000.000 Q-RX flushInput
        000002.469 RTS  inactive
        000002.773 RTS  active
        000003.001 TX   48 45 4C 4C 4F                                    HELLO
        000003.102 RX   48 45 4C 4C 4F                                    HELLO

    c                 C   s,   t   | _|| _|| _d| _d| _d| _d S )Nr    r!   z[37m)time
start_timer"   r#   r$   r%   control_colorr&   r   r   r   r(   w   s   

zFormatHexdump.__init__r   c                 C   s&   | j d|||| | j   d S )Nz{:010.3f} {:4} {}{}
)r"   r*   r   r+   )r'   	timestamplabelr2   Zvalue2r   r   r   
write_line   s   zFormatHexdump.write_linec                 C   sj   | j r
| j| j |r't|D ]\}}| t | j dd|| qdS | t | j dd dS )zshow received data as hex dumpZRX{:04X}  z<empty>N)	r#   r"   r*   r$   r   r>   r9   r:   r   r'   r   r   rowr   r   r   r-      s   "zFormatHexdump.rxc                 C   sJ   | j r
| j| j t|D ]\}}| t | j dd|| qdS )z!show transmitted data as hex dumpZTXr?   N)	r#   r"   r*   r%   r   r>   r9   r:   r   r@   r   r   r   r/      s
   "zFormatHexdump.txc                 C   s0   | j r
| j| j | t | j || dS )zshow control callsN)r#   r"   r*   r;   r>   r9   r:   r0   r   r   r   r3      s   zFormatHexdump.controlN)r   )	r4   r5   r6   r7   r(   r>   r-   r/   r3   r   r   r   r   r8   h   s    

r8   c                       sT  e Zd ZdZ fddZejjjdd Zdd Z	 fdd	Z
d, fdd	Zeejdr2 fddZeejdr> fddZe fddZ fddZ fddZ fddZd- fdd	Zejjjdd Zejjjd d! Zejjjd"d# Zejjj fd$d%Zejjj fd&d'Zejjj fd(d)Zejjj fd*d+Z  ZS ).Serialzc    Inherit the native Serial port implementation and wrap all the methods and
    attributes.
    c                    s&   t t| j|i | d | _d| _d S )NF)superrB   r(   	formattershow_all)r'   argskwargs	__class__r   r   r(      s   
zSerial.__init__c                 C   s&   |d urt jj| | | d S d S N)r   rB   port__set__from_url)r'   r2   r   r   r   rK      s   zSerial.portc           	   
   C   s   t |}|jdkrtd|jt}d}tj}z8t 	|j
d D ],\}}|dkr4t|d d}q$|dkr;d}q$|d	krBt}q$|d
krJd| _q$td|W n tyf } ztd|d}~ww |||| _d|j|jgS )z(extract host and port from an URL stringZspyzmexpected a string in the form "spy://port[?option[=value][&option[=value]]]": not starting with spy:// ({!r})FTfiler   wr#   rawallzunknown option: {!r}zPexpected a string in the form "spy://port[?option[=value][&option[=value]]]": {}Nr   )urlparseurlsplitschemer   ZSerialExceptionr   r8   sysstderrparse_qsqueryitemsopenr   rE   
ValueErrorrD   r   netlocpath)	r'   urlpartsrD   r#   r"   optionr   er   r   r   rM      s<   

zSerial.from_urlc                    s$   t |}| j| tt| |S rJ   )r   rD   r/   rC   rB   r*   )r'   r/   rH   r   r   r*      s   zSerial.writer   c                    s*   t t| |}|s| jr| j| |S rJ   )rC   rB   readrE   rD   r-   )r'   sizer-   rH   r   r   rb      s   
zSerial.readcancel_readc                        | j dd tt|   d S )NQ-RXrd   )rD   r3   rC   rB   rd   r'   rH   r   r   rd         zSerial.cancel_readcancel_writec                    re   )NQ-TXri   )rD   r3   rC   rB   ri   rg   rH   r   r   ri      rh   zSerial.cancel_writec                    s*   t t| j}| jr| jdd| |S )Nrf   zin_waiting -> {})rC   rB   
in_waitingrE   rD   r3   r   )r'   r   rH   r   r   rk      s   zSerial.in_waitingc                    re   )Nrj   r+   )rD   r3   rC   rB   r+   rg   rH   r   r   r+      rh   zSerial.flushc                    re   )Nrf   reset_input_buffer)rD   r3   rC   rB   rl   rg   rH   r   r   rl      rh   zSerial.reset_input_bufferc                    re   )Nrj   reset_output_buffer)rD   r3   rC   rB   rm   rg   rH   r   r   rm      rh   zSerial.reset_output_buffer      ?c                    s(   | j dd| tt| | d S )NBRKzsend_break {}s)rD   r3   r   rC   rB   
send_break)r'   durationrH   r   r   rp      s   zSerial.send_breakc                 C   *   | j d|rdnd tjj| | d S )Nro   activeinactive)rD   r3   r   rB   break_conditionrL   r'   levelr   r   r   ru         zSerial.break_conditionc                 C   rr   )NZRTSrs   rt   )rD   r3   r   rB   rtsrL   rv   r   r   r   ry      rx   z
Serial.rtsc                 C   rr   )NZDTRrs   rt   )rD   r3   r   rB   dtrrL   rv   r   r   r   rz     rx   z
Serial.dtrc                    ,   t t| j}| jd|rd |S d |S )NZCTSrs   rt   )rC   rB   ctsrD   r3   rv   rH   r   r   r|     
   z
Serial.ctsc                    r{   )NZDSRrs   rt   )rC   rB   dsrrD   r3   rv   rH   r   r   r~     r}   z
Serial.dsrc                    r{   )NZRIrs   rt   )rC   rB   rirD   r3   rv   rH   r   r   r     r}   z	Serial.ric                    r{   )NZCDrs   rt   )rC   rB   cdrD   r3   rv   rH   r   r   r     r}   z	Serial.cd)r   )rn   )r4   r5   r6   r7   r(   r   rB   rK   setterrM   r*   rb   hasattrrd   ri   propertyrk   r+   rl   rm   rp   ru   ry   rz   r|   getterr~   r   r   __classcell__r   r   rH   r   rB      s@    



rB   __main__zspy:///dev/ttyS0)
__future__r   rU   r9   r   Zserial.serialutilr   rR   ImportErrorurllib.parseparser   r   objectr   r8   rB   r4   ZserrK   printr   r   r   r   <module>   s*   3 