Sindbad~EG File Manager

Current Path : /usr/local/lib/python3.6/site-packages/serial/threaded/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/serial/threaded/__pycache__/__init__.cpython-36.pyc

3

 ��cg$�@s0dZddlmZddlZddlZGdd�de�ZGdd�de�ZGdd	�d	e�ZGd
d�de�Z	Gdd
�d
ej
�Zedk�r,ddl
Z
ddlZddlZdZGdd�de	�Zejeddd�Zeee��Zejd�ejd�WdQRXejeddd�Zeee�Zej�ej�\ZZejd�ejd�ej�dS)z%Support threading with serial ports.
�)�absolute_importNc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�Protocolzq    Protocol as used by the ReaderThread. This base class provides empty
    implementations of all methods.
    cCsdS)z$Called when reader thread is startedN�)�self�	transportrr�</tmp/pip-build-_uu8ur7s/pyserial/serial/threaded/__init__.py�connection_madeszProtocol.connection_madecCsdS)z2Called with snippets received from the serial portNr)r�datarrr�
data_receivedszProtocol.data_receivedcCst|t�r|�dS)zg        Called when the serial port is closed or the reader loop terminated
        otherwise.
        N)�
isinstance�	Exception)r�excrrr�connection_losts
zProtocol.connection_lostN)�__name__�
__module__�__qualname__�__doc__rr
rrrrrrsrcsDeZdZdZdZdd�Zdd�Z�fdd�Zd	d
�Zdd�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.
    �cCst�|_d|_dS)N)�	bytearray�bufferr)rrrr�__init__1szPacketizer.__init__cCs
||_dS)zStore transportN)r)rrrrrr5szPacketizer.connection_madecsd|_tt|�j|�dS)zForget transportN)r�superrr)rr
)�	__class__rrr9szPacketizer.connection_lostcCsB|jj|�x0|j|jkr<|jj|jd�\}|_|j|�qWdS)z9Buffer received data, find TERMINATOR, call handle_packet�N)r�extend�
TERMINATOR�split�
handle_packet)rr	�packetrrrr
>szPacketizer.data_receivedcCstd��dS)z1Process packets - to be overridden by subclassingz/please implement functionality in handle_packetN)�NotImplementedError)rrrrrrEszPacketizer.handle_packet)rrrrrrrrr
r�
__classcell__rr)rrr'srcsPeZdZdZdZdZdd�Zdd�Z�fdd	�Zd
d�Z	dd
�Z
dd�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.
    �(�)cCst�|_d|_d|_dS)NF)rr�	in_packetr)rrrrrTszFramedPacket.__init__cCs
||_dS)zStore transportN)r)rrrrrrYszFramedPacket.connection_madecs,d|_d|_|jdd�=tt|�j|�dS)zForget transportNF)rr%rrr"r)rr
)rrrr]szFramedPacket.connection_lostcCsvxptj|�D]b}||jkr"d|_q||jkrPd|_|jt|j��|jdd�=q|jrd|jj|�q|j	|�qWdS)z4Find data enclosed in START/STOP, call handle_packetTFN)
�serial�	iterbytes�STARTr%�STOPr�bytesrr�handle_out_of_packet_data)rr	Zbyterrrr
ds

zFramedPacket.data_receivedcCstd��dS)z1Process packets - to be overridden by subclassingz/please implement functionality in handle_packetN)r )rrrrrrrszFramedPacket.handle_packetcCsdS)z0Process data that is received outside of packetsNr)rr	rrrr+vsz&FramedPacket.handle_out_of_packet_data)
rrrrr(r)rrrr
rr+r!rr)rrr"Jsr"c@s4eZdZdZdZdZdZdd�Zdd�Zd	d
�Z	dS)�
LineReaderzZ
    Read and write (Unicode) lines from/to serial port.
    The encoding is applied.
    s
zutf-8�replacecCs|j|j|j|j��dS)N)�handle_line�decode�ENCODING�UNICODE_HANDLING)rrrrrr�szLineReader.handle_packetcCstd��dS)z2Process one line - to be overridden by subclassingz-please implement functionality in handle_lineN)r )r�linerrrr.�szLineReader.handle_linecCs"|jj|j|j|j�|j�dS)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�encoder0r1r)r�textrrr�
write_line�szLineReader.write_lineN)
rrrrrr0r1rr.r6rrrrr,{sr,csXeZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�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.
    csDtt|�j�d|_||_||_d|_tj�|_	tj
�|_d|_dS)z�        Initialize thread.

        Note that the serial_instance' timeout is set to one second!
        Other settings are not changed.
        TN)
rr7r�daemonr&�protocol_factory�alive�	threading�Lock�_lock�Event�_connection_made�protocol)rZserial_instancer9)rrrr�s

zReaderThread.__init__cCs*d|_t|jd�r|jj�|jd�dS)zStop the reader threadF�cancel_read�N)r:�hasattrr&rA�join)rrrr�stop�s
zReaderThread.stopc&Cs0t|jd�sd|j_|j�|_y|jj|�Wn<tk
rj}z d|_|jj|�|j	j
�dSd}~XnXd}|j	j
�x�|jo�|jj�ry|jj|jj
p�d�}Wn*tjk
r�}z|}PWYdd}~Xq|X|r|y|jj|�Wq|tk
�r}z|}PWYdd}~Xq|Xq|Wd|_|jj|�d|_dS)zReader looprArFN)rCr&�timeoutr9r@rrr:rr?�setZis_open�readZ
in_waitingZSerialExceptionr
)r�e�errorr	rrr�run�s6


zReaderThread.runc	Cs|j�|jj|�SQRXdS)zThread safe writing (uses lock)N)r=r&r3)rr	rrrr3�szReaderThread.writec	Cs(|j�|j�|jj�WdQRXdS)z8Close the serial port and exit reader thread (uses lock)N)r=rEr&�close)rrrrrL�szReaderThread.closecCs4|jr(|jj�|jstd��||jfStd��dS)zj
        Wait until connection is set up and return the transport and protocol
        instances.
        zconnection_lost already calledzalready stoppedN)r:r?�wait�RuntimeErrorr@)rrrr�connect�s

zReaderThread.connectcCs&|j�|jj�|js td��|jS)zs        Enter context handler. May raise RuntimeError in case the connection
        could not be created.
        zconnection_lost already called)�startr?rMr:rNr@)rrrr�	__enter__�s

zReaderThread.__enter__cCs|j�dS)zLeave context: close portN)rL)r�exc_type�exc_val�exc_tbrrr�__exit__�szReaderThread.__exit__)
rrrrrrErKr3rLrOrQrUr!rr)rrr7�s#r7�__main__zloop://cs,eZdZ�fdd�Zdd�Zdd�Z�ZS)�
PrintLinescs*tt|�j|�tjjd�|jd�dS)Nzport opened
zhello world)rrWr�sys�stdoutr3r6)rr)rrrrszPrintLines.connection_madecCstjjdj|��dS)Nzline received: {!r}
)rXrYr3�format)rr	rrrr.szPrintLines.handle_linecCs|rtj|�tjjd�dS)Nzport closed
)�	traceback�	print_excrXrYr3)rr
rrrrs
zPrintLines.connection_lost)rrrrr.rr!rr)rrrWsrWi�r)ZbaudraterFZhellorB)r�
__future__rr&r;�objectrrr"r,�Threadr7rrX�timer[ZPORTrWZserial_for_urlZserr@r6�sleep�trPrOrrLrrrr�<module>s2#1q






Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists