
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
M]W@#                 @   s   d dl Z d dlZd dlZyd dlZW n ek
rB   d dljZY nX 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redZde_ee dS )    Nc             c   s   d}xrt j| D ]d}djt|d|  ko2dk n  rB|jdndfV  |d7 }|dkrbdV  q|dkrdV  d}qW |dkrx(|dk r|d7 }|dkrdV  dV  qW dV  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.           N   )r   r	   )NN)r   r	   )r   r   )NN)serialZ	iterbytesformatorddecode)datanb r   "/usr/lib/python3.6/protocol_spy.pysixteen"   s     6

r   c             c   s   g }g }d}xnt | D ]b\}}|dkrd|djdj|dj|gfV  |dd= |dd= |d7 }q|j| |j| qW dS )z yield lines with hexdump of datar   Nr   r	   r
   )r   joinappend)r   valuesr   offsethar   r   r   hexdump9   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 )Nz[32mz[31m)outputcolorrx_colortx_color)selfr   r   r   r   r   __init__L   s    zFormatRaw.__init__c             C   s.   | j r| jj| j | jj| | jj  dS )zshow received dataN)r   r   writer    flush)r"   r   r   r   r   rxR   s    zFormatRaw.rxc             C   s.   | j r| jj| j | jj| | jj  dS )zshow transmitted dataN)r   r   r$   r!   r%   )r"   r   r   r   r   txY   s    zFormatRaw.txc             C   s   dS )z(do not) show control callsNr   )r"   namevaluer   r   r   control`   s    zFormatRaw.controlN)__name__
__module____qualname____doc__r#   r&   r'   r*   r   r   r   r   r   I   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 j  | _|| _|| _d| _d| _d| _d S )Nz[32mz[31mz[37m)time
start_timer   r   r    r!   control_color)r"   r   r   r   r   r   r#   t   s    
zFormatHexdump.__init__r	   c             C   s&   | j jdj|||| | j j  d S )Nz{:010.3f} {:4} {}{}
)r   r$   r   r%   )r"   Z	timestampZlabelr)   Zvalue2r   r   r   
write_line|   s    zFormatHexdump.write_linec             C   sl   | j r| jj| j |rPxNt|D ](\}}| jtj | j ddj|| q"W n| jtj | j dd dS )zshow received data as hex dumpZRXz{:04X}  z<empty>N)	r   r   r$   r    r   r3   r0   r1   r   )r"   r   r   rowr   r   r   r&      s    &zFormatHexdump.rxc             C   sN   | j r| jj| j x4t|D ](\}}| jtj | j ddj|| qW dS )z!show transmitted data as hex dumpZTXz{:04X}  N)	r   r   r$   r!   r   r3   r0   r1   r   )r"   r   r   r4   r   r   r   r'      s    zFormatHexdump.txc             C   s0   | j r| jj| j | jtj | j || dS )zshow control callsN)r   r   r$   r2   r3   r0   r1   )r"   r(   r)   r   r   r   r*      s    zFormatHexdump.controlN)r	   )	r+   r,   r-   r.   r#   r3   r&   r'   r*   r   r   r   r   r/   e   s   

r/   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rd 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|| d | _d| _d S )NF)superr5   r#   	formattershow_all)r"   argskwargs)	__class__r   r   r#      s    zSerial.__init__c             C   s"   |d k	rt jjj| | j| d S )N)r   r5   port__set__from_url)r"   r)   r   r   r   r<      s    zSerial.portc       	      C   s   t j|}|jdkr&tjdj|jt}d}tj}yvxpt j	|j
dj D ]Z\}}|dkrjt|d d}qJ|dkrxd}qJ|d	krt}qJ|d
krd| _qJtdj|qJW W n2 tk
r } ztjdj|W Y dd}~X nX |||| _dj|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	   )urlparseZurlsplitschemer   ZSerialExceptionr   r/   sysstderrZparse_qsZqueryitemsopenr   r8   
ValueErrorr7   r   Znetlocpath)	r"   Zurlpartsr7   r   r   Zoptionr   er   r   r   r>      s2    


zSerial.from_urlc                s   | j j| tt| j|S )N)r7   r'   r6   r5   r$   )r"   r'   )r;   r   r   r$      s    zSerial.writer   c                s*   t t| j|}|s| jr&| jj| |S )N)r6   r5   readr8   r7   r&   )r"   sizer&   )r;   r   r   rM      s    
zSerial.readcancel_readc                s    | j jdd tt| j  d S )NzQ-RXrO   )r7   r*   r6   r5   rO   )r"   )r;   r   r   rO      s    zSerial.cancel_readcancel_writec                s    | j jdd tt| j  d S )NzQ-TXrP   )r7   r*   r6   r5   rP   )r"   )r;   r   r   rP      s    zSerial.cancel_writec                s*   t t| j}| jr&| jjddj| |S )NzQ-RXzin_waiting -> {})r6   r5   
in_waitingr8   r7   r*   r   )r"   r   )r;   r   r   rQ      s    zSerial.in_waitingc                s    | j jdd tt| j  d S )NzQ-TXr%   )r7   r*   r6   r5   r%   )r"   )r;   r   r   r%      s    zSerial.flushc                s    | j jdd tt| j  d S )NzQ-RXreset_input_buffer)r7   r*   r6   r5   rR   )r"   )r;   r   r   rR      s    zSerial.reset_input_bufferc                s    | j jdd tt| j  d S )NzQ-TXreset_output_buffer)r7   r*   r6   r5   rS   )r"   )r;   r   r   rS      s    zSerial.reset_output_buffer      ?c                s(   | j jddj| tt| j| d S )NBRKzsend_break {}s)r7   r*   r   r6   r5   
send_break)r"   Zduration)r;   r   r   rV      s    zSerial.send_breakc             C   s*   | j jd|rdnd tjjj| | d S )NrU   activeinactive)r7   r*   r   r5   break_conditionr=   )r"   levelr   r   r   rY      s    zSerial.break_conditionc             C   s*   | j jd|rdnd tjjj| | d S )NZRTSrW   rX   )r7   r*   r   r5   rtsr=   )r"   rZ   r   r   r   r[      s    z
Serial.rtsc             C   s*   | j jd|rdnd tjjj| | d S )NZDTRrW   rX   )r7   r*   r   r5   dtrr=   )r"   rZ   r   r   r   r\      s    z
Serial.dtrc                s&   t t| j}| jjd|rdnd |S )NZCTSrW   rX   )r6   r5   ctsr7   r*   )r"   rZ   )r;   r   r   r]     s    z
Serial.ctsc                s&   t t| j}| jjd|rdnd |S )NZDSRrW   rX   )r6   r5   dsrr7   r*   )r"   rZ   )r;   r   r   r^     s    z
Serial.dsrc                s&   t t| j}| jjd|rdnd |S )NZRIrW   rX   )r6   r5   rir7   r*   )r"   rZ   )r;   r   r   r_     s    z	Serial.ric                s&   t t| j}| jjd|rdnd |S )NZCDrW   rX   )r6   r5   cdr7   r*   )r"   rZ   )r;   r   r   r`     s    z	Serial.cd)r   )rT   )r+   r,   r-   r.   r#   r   r5   r<   setterr>   r$   rM   hasattrrO   rP   propertyrQ   r%   rR   rS   rV   rY   r[   r\   r]   getterr^   r_   r`   __classcell__r   r   )r;   r   r5      s,   r5   __main__zspy:///dev/ttyS0)rE   r0   r   rC   ImportErrorZurllib.parseparser   r   objectr   r/   r5   r+   Zserr<   printr   r   r   r   <module>   s    3 