HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/serial/threaded/__pycache__/__init__.cpython-38.pyc
U

�U�W8$�	@s$dZddlZddlZGdd�de�ZGdd�de�ZGdd�de�ZGd	d
�d
e�ZGdd�dej�Z	e
d
k�r ddlZddlZddl
Z
dZGdd�de�Zejeddd�Ze	ee��Ze�d�e�d�W5QRXejeddd�Ze	ee�Ze��e��\ZZe�d�e�d�e��dS)z%Support threading with serial ports.
�Nc@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�:/usr/lib/python3/dist-packages/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__rrrrrrrrsrcsDeZdZdZdZdd�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__/szPacketizer.__init__cCs
||_dS�zStore transportN�rrrrrr3szPacketizer.connection_madecsd|_tt|��|�dS)�Forget transportN)r�superrrr��	__class__rrr7szPacketizer.connection_lostcCs>|j�|�|j|jkr:|j�|jd�\}|_|�|�qdS)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�rr'rrrr&CszPacketizer.handle_packet)rrrrr$rrrrr&�
__classcell__rrr rr%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_packetrrrrrrRszFramedPacket.__init__cCs
||_dSrrrrrrrWszFramedPacket.connection_madecs,d|_d|_|jdd�=tt|��|�dS)rNF)rr0r'rr-rrr rrr[szFramedPacket.connection_lostcCsrt�|�D]b}||jkr d|_q
||jkrNd|_|�t|j��|jdd�=q
|jrb|j�|�q
|�	|�q
dS)z4Find data enclosed in START/STOP, call handle_packetTFN)
�serialZ	iterbytes�STARTr0�STOPr&�bytesr'r#�handle_out_of_packet_data)rr
Zbyterrrrbs

zFramedPacket.data_receivedcCstd��dSr(r)r+rrrr&pszFramedPacket.handle_packetcCsdS)z0Process data that is received outside of packetsNrr	rrrr5tsz&FramedPacket.handle_out_of_packet_data)
rrrrr2r3rrrrr&r5r,rrr rr-Hs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��dSr)�handle_line�decode�ENCODING�UNICODE_HANDLINGr+rrrr&�szLineReader.handle_packetcCstd��dS)z2Process one line - to be overridden by subclassingz-please implement functionality in handle_lineNr))r�linerrrr8�szLineReader.handle_linecCs"|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�encoder:r;r$)r�textrrr�
write_line�szLineReader.write_lineN)
rrrrr$r:r;r&r8r@rrrrr6ysr6csXeZdZdZ�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|���d|_||_||_d|_t��|_	t�
�|_d|_dS)z�        Initialize thread.

        Note that the serial_instance' timeout is set to one second!
        Other settings are not changed.
        TN)
rrArZdaemonr1�protocol_factory�alive�	threadingZLock�_lockZEvent�_connection_made�protocol)rZserial_instancerBr rrr�s

zReaderThread.__init__cCs*d|_t|jd�r|j��|�d�dS)zStop the reader threadF�cancel_read�N)rC�hasattrr1rH�joinrrrr�stop�s
zReaderThread.stopc
CsFt|jd�sd|j_|��|_z|j�|�WnDtk
rr}z&d|_|j�|�|j	�
�WY�dSd}~XYnXd}|j	�
�|j�r*|jj�r*z|j�|jj
p�d�}Wn2tjk
r�}z|}WY��q*W5d}~XYq�X|r�z|j�|�Wq�tk
�r&}z|}WY��q*W5d}~XYq�Xq�d|_|j�|�d|_dS)zReader looprHr"FN)rJr1�timeoutrBrGrr
rCrrF�setZis_open�readZ
in_waitingZSerialExceptionr)r�e�errorr
rrr�run�s6


zReaderThread.runc	Cs"|j�|j�|�W5QRXdS)zThread safe writing (uses lock)N)rEr1r=r	rrrr=�szReaderThread.writec	Cs(|j�|��|j��W5QRXdS)z8Close the serial port and exit reader thread (uses lock)N)rErLr1�closerrrrrS�szReaderThread.closecCs4|jr(|j��|jstd��||jfStd��dS)zj
        Wait until connection is set up and return the transport and protocol
        instances.
        �connection_lost already calledzalready stoppedN)rCrF�wait�RuntimeErrorrGrrrr�connect�s

zReaderThread.connectcCs&|��|j��|js td��|jS)zs        Enter context handler. May raise RuntimeError in case the connection
        could not be created.
        rT)�startrFrUrCrVrGrrrr�	__enter__�s

zReaderThread.__enter__cCs|��dS)zLeave context: close portN)rS)r�exc_typeZexc_val�exc_tbrrr�__exit__�szReaderThread.__exit__)
rrrrrrLrRr=rSrWrYr\r,rrr rrA�s#rA�__main__zloop://cs,eZdZ�fdd�Zdd�Zdd�Z�ZS)�
PrintLinescs*tt|��|�tj�d�|�d�dS)Nzport opened
zhello world)rr^r�sys�stdoutr=r@rr rrrszPrintLines.connection_madecCstj�d�|��dS)Nzline received: {!r}
)r_r`r=�formatr	rrrr8szPrintLines.handle_linecCs|rt�|�tj�d�dS)Nzport closed
)�	traceback�	print_excr_r`r=rrrrrs
zPrintLines.connection_lost)rrrrr8rr,rrr rr^
sr^i�r")ZbaudraterMZhellorI)rr1rD�objectrrr-r6ZThreadrArr_�timerbZPORTr^Zserial_for_urlZserrGr@�sleep�trXrWrrSrrrr�<module>	s0#1q