o
    ^g$                     @   sF  d Z ddlmZ ddlZddl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rddlZddlZddlZdZG dd de	ZejedddZeeeZed ed W d   n1 suw   Y  ejedddZeeeZe  e \ZZed ed e  dS dS )z%Support threading with serial ports.
    )absolute_importNc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Protocolzq    Protocol as used by the ReaderThread. This base class provides empty
    implementations of all methods.
    c                 C      dS )z$Called when reader thread is startedN self	transportr   r   :/usr/lib/python3/dist-packages/serial/threaded/__init__.pyconnection_made       zProtocol.connection_madec                 C   r   )z2Called with snippets received from the serial portNr   r   datar   r   r	   data_received   r   zProtocol.data_receivedc                 C   s   t |tr|dS )zg        Called when the serial port is closed or the reader loop terminated
        otherwise.
        N)
isinstance	Exceptionr   excr   r   r	   connection_lost   s   
zProtocol.connection_lostN)__name__
__module____qualname____doc__r
   r   r   r   r   r   r	   r      s
    r   c                       sD   e Zd ZdZdZdd Zdd Z fddZd	d
 Zdd Z	  Z
S )
Packetizerz
    Read binary packets from serial port. Packets are expected to be terminated
    with a TERMINATOR byte (null byte by default).

    The class also keeps track of the transport.
        c                 C   s   t  | _d | _d S N)	bytearraybufferr   r   r   r   r	   __init__1   s   
zPacketizer.__init__c                 C   
   || _ dS zStore transportNr   r   r   r   r	   r
   5      
zPacketizer.connection_madec                    s   d| _ tt| | dS )Forget transportN)r   superr   r   r   	__class__r   r	   r   9   s   zPacketizer.connection_lostc                 C   sL   | j | | j| j v r$| j | jd\}| _ | | | j| j v sdS dS )z9Buffer received data, find TERMINATOR, call handle_packet   N)r   extend
TERMINATORsplithandle_packet)r   r   packetr   r   r	   r   >   s
   
zPacketizer.data_receivedc                 C      t dz1Process packets - to be overridden by subclassingz/please implement functionality in handle_packetNotImplementedErrorr   r,   r   r   r	   r+   E      zPacketizer.handle_packet)r   r   r   r   r)   r   r
   r   r   r+   __classcell__r   r   r%   r	   r   '   s    r   c                       sP   e Zd ZdZdZdZdd Zdd Z fdd	Zd
d Z	dd Z
dd Z  ZS )FramedPacketz
    Read binary packets. Packets are expected to have a start and stop marker.

    The class also keeps track of the transport.
       (   )c                 C   s   t  | _d| _d | _d S )NF)r   r,   	in_packetr   r   r   r   r	   r   T   s   
zFramedPacket.__init__c                 C   r   r    r!   r   r   r   r	   r
   Y   r"   zFramedPacket.connection_madec                    s,   d| _ d| _| jdd= tt| | dS )r#   NF)r   r7   r,   r$   r4   r   r   r%   r   r	   r   ]   s   zFramedPacket.connection_lostc                 C   sr   t |D ]1}|| jkrd| _q|| jkr'd| _| t| j | jdd= q| jr1| j| q| 	| qdS )z4Find data enclosed in START/STOP, call handle_packetTFN)
serialZ	iterbytesSTARTr7   STOPr+   bytesr,   r(   handle_out_of_packet_data)r   r   Zbyter   r   r	   r   d   s   

zFramedPacket.data_receivedc                 C   r-   r.   r/   r1   r   r   r	   r+   r   r2   zFramedPacket.handle_packetc                 C   r   )z0Process data that is received outside of packetsNr   r   r   r   r	   r<   v   s   z&FramedPacket.handle_out_of_packet_data)r   r   r   r   r9   r:   r   r
   r   r   r+   r<   r3   r   r   r%   r	   r4   J   s    r4   c                   @   s4   e Zd ZdZdZdZdZdd Zdd Zd	d
 Z	dS )
LineReaderzZ
    Read and write (Unicode) lines from/to serial port.
    The encoding is applied.
    s   
zutf-8replacec                 C   s   |  || j| j d S r   )handle_linedecodeENCODINGUNICODE_HANDLINGr1   r   r   r	   r+      s   zLineReader.handle_packetc                 C   r-   )z2Process one line - to be overridden by subclassingz-please implement functionality in handle_liner/   )r   liner   r   r	   r?      r2   zLineReader.handle_linec                 C   s"   | j || j| j| j  dS )z
        Write text to the transport. ``text`` is a Unicode string and the encoding
        is applied before sending ans also the newline is append.
        N)r   writeencoderA   rB   r)   )r   textr   r   r	   
write_line   s   "zLineReader.write_lineN)
r   r   r   r   r)   rA   rB   r+   r?   rG   r   r   r   r	   r=   {   s    r=   c                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )ReaderThreada      Implement a serial port read loop and dispatch to a Protocol instance (like
    the asyncio.Protocol) but do it with threads.

    Calls to close() will close the serial port but it is also possible to just
    stop() this thread and continue the serial port instance otherwise.
    c                    sD   t t|   d| _|| _|| _d| _t | _	t
 | _d| _dS )z        Initialize thread.

        Note that the serial_instance' timeout is set to one second!
        Other settings are not changed.
        TN)r$   rH   r   Zdaemonr8   protocol_factoryalive	threadingZLock_lockZEvent_connection_madeprotocol)r   Zserial_instancerI   r%   r   r	   r      s   


zReaderThread.__init__c                 C   s*   d| _ t| jdr| j  | d dS )zStop the reader threadFcancel_read   N)rJ   hasattrr8   rO   joinr   r   r   r	   stop   s   
zReaderThread.stopc              
   C   s:  t | jds
d| j_|  | _z| j|  W n! ty8 } zd| _| j| | j	
  W Y d}~dS d}~ww d}| j	
  | jr| jjrz| j| jjpPd}W n tjyh } z|}W Y d}~n+d}~ww |rz| j| W n ty } z|}W Y d}~nd}~ww | jr| jjsGd| _| j| d| _dS )zReader looprO   r'   FN)rQ   r8   timeoutrI   rN   r
   r   rJ   r   rM   setZis_openreadZ
in_waitingZSerialExceptionr   )r   eerrorr   r   r   r	   run   sD   



zReaderThread.runc                 C   s6   | j  | j|W  d   S 1 sw   Y  dS )zThread safe writing (uses lock)N)rL   r8   rD   r   r   r   r	   rD      s   
$zReaderThread.writec                 C   s>   | j  |   | j  W d   dS 1 sw   Y  dS )z8Close the serial port and exit reader thread (uses lock)N)rL   rS   r8   closer   r   r   r	   rZ      s   "zReaderThread.closec                 C   s0   | j r| j  | j std| | jfS td)zj
        Wait until connection is set up and return the transport and protocol
        instances.
        connection_lost already calledzalready stopped)rJ   rM   waitRuntimeErrorrN   r   r   r   r	   connect   s   

zReaderThread.connectc                 C   s&   |    | j  | jstd| jS )zs        Enter context handler. May raise RuntimeError in case the connection
        could not be created.
        r[   )startrM   r\   rJ   r]   rN   r   r   r   r	   	__enter__   s
   
zReaderThread.__enter__c                 C   s   |    dS )zLeave context: close portN)rZ   )r   exc_typeZexc_valexc_tbr   r   r	   __exit__   s   zReaderThread.__exit__)r   r   r   r   r   rS   rY   rD   rZ   r^   r`   rc   r3   r   r   r%   r	   rH      s    #rH   __main__zloop://c                       s,   e Zd Z fddZdd Zdd Z  ZS )
PrintLinesc                    s*   t t| | tjd | d d S )Nzport opened
zhello world)r$   re   r
   sysstdoutrD   rG   r   r%   r   r	   r
     s   zPrintLines.connection_madec                 C   s   t jd| d S )Nzline received: {!r}
)rf   rg   rD   formatr   r   r   r	   r?     s   zPrintLines.handle_linec                 C   s   |rt | tjd d S )Nzport closed
)	traceback	print_excrf   rg   rD   r   r   r   r	   r     s   
zPrintLines.connection_lost)r   r   r   r
   r?   r   r3   r   r   r%   r	   re     s    re   i  r'   )ZbaudraterT   ZhellorP   )r   Z
__future__r   r8   rK   objectr   r   r4   r=   ZThreadrH   r   rf   timeri   ZPORTre   Zserial_for_urlZserrN   rG   sleeptr_   r^   r   rZ   r   r   r   r	   <module>   s8   #1q



