관리-도구
편집 파일: __init__.cpython-39.pyc
a �����U�W8$����������������������@���s8��d�Z�ddlZddlZG�dd��de�ZG�dd��de�ZG�dd��de�ZG�d d ��d e�ZG�dd��dej�Z e d k�r4ddlZddlZddl Z dZG�dd��de�Zejeddd�Ze ee��$Ze�d��e�d��W�d����n1�s�0����Y��ejeddd�Ze ee�Ze����e���\ZZe�d��e�d��e����dS�)z%Support threading with serial ports. �����Nc�������������������@���s(���e�Zd�ZdZdd��Zdd��Zdd��ZdS�) �Protocolzq Protocol as used by the ReaderThread. This base class provides empty implementations of all methods. c�����������������C���s���dS�)z$Called when reader thread is startedN����self� transportr���r����</usr/lib/python3.9/site-packages/serial/threaded/__init__.py�connection_made���s����zProtocol.connection_madec�����������������C���s���dS�)z2Called with snippets received from the serial portNr����r����datar���r���r���� data_received���s����zProtocol.data_receivedc�����������������C���s���t�|t�r|�dS�)zg Called when the serial port is closed or the reader loop terminated otherwise. N)� isinstance� Exception�r����excr���r���r����connection_lost���s���� zProtocol.connection_lostN)�__name__� __module__�__qualname__�__doc__r���r���r���r���r���r���r���r������s���r���c�����������������������sD���e�Zd�ZdZdZdd��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. �����c�����������������C���s���t���|�_d�|�_d�S��N)� bytearray�bufferr����r���r���r���r����__init__/���s����zPacketizer.__init__c�����������������C���s ���||�_�dS��zStore transportN�r���r���r���r���r���r���3���s����zPacketizer.connection_madec��������������������s���d|�_�tt|���|��dS�)�Forget transportN)r����superr���r���r����� __class__r���r���r���7���s����zPacketizer.connection_lostc�����������������C���s>���|�j��|��|�j|�j�v�r:|�j��|�jd�\}|�_�|��|��qdS�)z9Buffer received data, find TERMINATOR, call handle_packet����N)r����extend� TERMINATOR�split� handle_packet)r���r ����packetr���r���r���r���<���s����zPacketizer.data_receivedc�����������������C���s���t�d��dS��z1Process packets - to be overridden by subclassingz/please implement functionality in handle_packetN��NotImplementedError�r���r'���r���r���r���r&���C���s����zPacketizer.handle_packet)r���r���r���r���r$���r���r���r���r���r&���� __classcell__r���r���r ���r���r���%���s���r���c�����������������������sP���e�Zd�ZdZdZdZdd��Zdd��Z��fdd �Zd d��Z dd ��Z dd��Z���ZS�)�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���R���s����zFramedPacket.__init__c�����������������C���s ���||�_�dS�r���r���r���r���r���r���r���W���s����zFramedPacket.connection_madec��������������������s,���d|�_�d|�_|�jdd�=�tt|���|��dS�)r���NF)r���r0���r'���r���r-���r���r���r ���r���r���r���[���s����zFramedPacket.connection_lostc�����������������C���sr���t��|�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)r���r ���Zbyter���r���r���r���b���s���� zFramedPacket.data_receivedc�����������������C���s���t�d��dS�r(���r)���r+���r���r���r���r&���p���s����zFramedPacket.handle_packetc�����������������C���s���dS�)z0Process data that is received outside of packetsNr���r ���r���r���r���r5���t���s����z&FramedPacket.handle_out_of_packet_data) r���r���r���r���r2���r3���r���r���r���r���r&���r5���r,���r���r���r ���r���r-���H���s���r-���c�������������������@���s4���e�Zd�ZdZdZdZdZdd��Zdd��Zd d ��Z dS�)� LineReaderzZ Read and write (Unicode) lines from/to serial port. The encoding is applied. s��� zutf-8�replacec�����������������C���s���|���|�|�j|�j���d�S�r���)�handle_line�decode�ENCODING�UNICODE_HANDLINGr+���r���r���r���r&�������s����zLineReader.handle_packetc�����������������C���s���t�d��dS�)z2Process one line - to be overridden by subclassingz-please implement functionality in handle_lineNr)���)r����liner���r���r���r8�������s����zLineReader.handle_linec�����������������C���s"���|�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����textr���r���r���� write_line����s����zLineReader.write_lineN) r���r���r���r���r$���r:���r;���r&���r8���r@���r���r���r���r���r6���y���s���r6���c�����������������������sX���e�Zd�ZdZ��fdd�Zdd��Zdd��Zdd ��Zd d��Zdd ��Z dd��Z dd��Z���ZS�)�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|�_dS�)z� Initialize thread. Note that the serial_instance' timeout is set to one second! Other settings are not changed. TN) r���rA���r����daemonr1����protocol_factory�alive� threading�Lock�_lock�Event�_connection_made�protocol)r���Zserial_instancerC���r ���r���r���r�������s���� zReaderThread.__init__c�����������������C���s*���d|�_�t|�jd�r|�j����|��d��dS�)zStop the reader threadF�cancel_read����N)rD����hasattrr1���rK����joinr���r���r���r����stop����s���� zReaderThread.stopc�������������� ���C���sP��t�|�jd�sd|�j_|����|�_z|�j�|���W�nB�typ�}�z*d|�_|�j�|��|�j � ���W�Y�d}~dS�d}~0�0�d}|�j � ���|�j�r4|�jj�r4z|�j�|�jj p�d�}W�n8�tjy��}�z|}W�Y�d}~�q4W�Y�d}~q�d}~0�0�|r�z|�j�|��W�q��t�y0�}�z|}W�Y�d}~�q4W�Y�d}~q�d}~0�0�q�d|�_|�j�|��d|�_dS�)zReader looprK���r"���FN)rM���r1����timeoutrC���rJ���r���r ���rD���r���rI����setZis_open�readZ in_waitingZSerialExceptionr���)r����e�errorr ���r���r���r����run����s6���� $&zReaderThread.runc�����������������C���s6���|�j���|�j�|��W�d����n1�s(0����Y��dS�)zThread safe writing (uses lock)N)rG���r1���r=���r ���r���r���r���r=�������s����zReaderThread.writec�����������������C���s<���|�j��"�|�����|�j����W�d����n1�s.0����Y��dS�)z8Close the serial port and exit reader thread (uses lock)N)rG���rO���r1����closer���r���r���r���rV�������s����zReaderThread.closec�����������������C���s4���|�j�r(|�j����|�j�std��|�|�jfS�td��dS�)zj Wait until connection is set up and return the transport and protocol instances. �connection_lost already calledzalready stoppedN)rD���rI����wait�RuntimeErrorrJ���r���r���r���r����connect����s���� zReaderThread.connectc�����������������C���s&���|������|�j����|�js td��|�jS�)zs Enter context handler. May raise RuntimeError in case the connection could not be created. rW���)�startrI���rX���rD���rY���rJ���r���r���r���r���� __enter__����s ���� zReaderThread.__enter__c�����������������C���s���|������dS�)zLeave context: close portN)rV���)r����exc_typeZexc_valZexc_tbr���r���r����__exit__����s����zReaderThread.__exit__) r���r���r���r���r���rO���rU���r=���rV���rZ���r\���r^���r,���r���r���r ���r���rA�������s���#rA����__main__zloop://c�����������������������s,���e�Zd�Z��fdd�Zdd��Zdd��Z���ZS�)� PrintLinesc��������������������s*���t�t|���|��tj�d��|��d��d�S�)Nzport opened zhello world)r���r`���r����sys�stdoutr=���r@���r���r ���r���r���r�����s����zPrintLines.connection_madec�����������������C���s���t�j�d�|���d�S�)Nzline received: {!r} )ra���rb���r=����formatr ���r���r���r���r8�����s����zPrintLines.handle_linec�����������������C���s���|rt��|��tj�d��d�S�)Nzport closed )� traceback� print_excra���rb���r=���r���r���r���r���r�����s���� zPrintLines.connection_lost)r���r���r���r���r8���r���r,���r���r���r ���r���r`��� ��s���r`���i���r"���)ZbaudraterP���ZhellorL���)r���r1���rE����objectr���r���r-���r6����ThreadrA���r���ra����timerd���ZPORTr`���Zserial_for_urlZserrJ���r@����sleep�tr[���rZ���r���rV���r���r���r���r����<module> ���s0���#1q (