^g$dZddlmZddlZddlZGddeZGddeZGdd eZGd d eZ Gd d ej Z e dkrddl Z ddlZddlZdZGdde ZejeddZe ee5Zedejddddn #1swxYwYejeddZe eeZee\ZZedejdedSdS)z%Support threading with serial ports. )absolute_importNc$eZdZdZdZdZdZdS)Protocolzq Protocol as used by the ReaderThread. This base class provides empty implementations of all methods. cdS)z$Called when reader thread is startedNself transports :/usr/lib/python3/dist-packages/serial/threaded/__init__.pyconnection_madezProtocol.connection_madecdS)z2Called with snippets received from the serial portNrr datas r data_receivedzProtocol.data_receivedr rc4t|tr|dS)zg Called when the serial port is closed or the reader loop terminated otherwise. N) isinstance Exceptionr excs r connection_lostzProtocol.connection_losts% c9 % % I  rN)__name__ __module__ __qualname____doc__r rrrrr rrsN 333AAArrc>eZdZdZdZdZdZfdZdZdZ xZ 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:t|_d|_dSN) bytearraybufferr r s r __init__zPacketizer.__init__1skk rc||_dSzStore transportNr rs r r zPacketizer.connection_made5 "rcfd|_tt||dS)Forget transportN)r superrrr r __class__s r rzPacketizer.connection_lost9s. j$//44444rc|j||j|jvrM|j|jd\}|_|||j|jvKdSdS)z9Buffer received data, find TERMINATOR, call handle_packetN)r#extend TERMINATORsplit handle_packet)r rpackets r rzPacketizer.data_received>sx 4   o,,"&+"3"3DOQ"G"G FDK   v & & &o,,,,,,rc tdz1Process packets - to be overridden by subclassingz/please implement functionality in handle_packetNotImplementedErrorr r5s r r4zPacketizer.handle_packetE!"STTTr) rrrrr2r%r rrr4 __classcell__r.s@r rr'sJ###55555 '''UUUUUUUrrcHeZdZdZdZdZdZdZfdZdZ dZ d Z xZ S) FramedPacketz Read binary packets. Packets are expected to have a start and stop marker. The class also keeps track of the transport. ()cHt|_d|_d|_dS)NF)r"r5 in_packetr r$s r r%zFramedPacket.__init__Tskk rc||_dSr'r(rs r r zFramedPacket.connection_madeYr)rcd|_d|_|jdd=tt||dS)r+NF)r rCr5r,r?rr-s r rzFramedPacket.connection_lost]sB KN lD!!11#66666rcPtj|D]}||jkrd|_||jkr9d|_|t |j|jdd=Y|jr|j|{| |dS)z4Find data enclosed in START/STOP, call handle_packetTFN) serial iterbytesSTARTrCSTOPr4bytesr5r1handle_out_of_packet_data)r rbytes r rzFramedPacket.data_receivedds$T** 5 5Dtz!!!%""!&""5#5#5666KNN 5 ""4((((..t4444 5 5rc tdr7r8r:s r r4zFramedPacket.handle_packetrr;rcdS)z0Process data that is received outside of packetsNrrs r rLz&FramedPacket.handle_out_of_packet_datavs r) rrrrrIrJr%r rrr4rLr<r=s@r r?r?Js E D ###77777 5 5 5UUU       rr?c0eZdZdZdZdZdZdZdZdZ dS) LineReaderzZ Read and write (Unicode) lines from/to serial port. The encoding is applied. s zutf-8replacecl|||j|jdSr!) handle_linedecodeENCODINGUNICODE_HANDLINGr:s r r4zLineReader.handle_packets/ t}d6KLLMMMMMrc td)z2Process one line - to be overridden by subclassingz-please implement functionality in handle_liner8)r lines r rTzLineReader.handle_lines!"QRRRrc|j||j|j|jzdS)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 writeencoderVrWr2)r texts r write_linezLineReader.write_lines< T[[8MNNQUQ``aaaaarN) rrrrr2rVrWr4rTr^rrr rQrQ{sh JH NNNSSSbbbbbrrQcLeZdZdZfdZdZdZdZdZdZ dZ d Z xZ 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. ctt|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) r,r`r%daemonrGprotocol_factoryalive threadingLock_lockEvent_connection_madeprotocol)r serial_instancercr.s r r%zReaderThread.__init__sh lD!!**,,, % 0 ^%% ) 1 1 rcd|_t|jdr|j|ddS)zStop the reader threadF cancel_readN)rdhasattrrGrmjoinr$s r stopzReaderThread.stopsD 4; . . & K # # % % % ! rc"t|jds d|j_||_ |j|nR#t $rE}d|_|j||j Yd}~dSd}~wwxYwd}|j |jr|jj r |j |jj pd}|r5 |j|n8#t $r }|}Yd}~n:d}~wwxYwn#tj$r }|}Yd}~nd}~wwxYw|jr |jj d|_|j|d|_dS)z Reader looprmr0FN)rorGtimeoutrcrjr rrdrrisetis_openread in_waitingrSerialException)r eerrorrs r runzReaderThread.runst{M22 $"#DK --//   M ) )$ / / / /   DJ M ) )! , , ,  ! % % ' ' ' FFFFF    !!###j T[0  {'' (>(C!DD 33D9999$ !  )      j T[0 "  %%e,,, sGA B&!:B!!B&&D6D D2&D--D26EE  Ecx|j5|j|cdddS#1swxYwYdS)zThread safe writing (uses lock)N)rgrGr[rs r r[zReaderThread.writes Z + +;$$T** + + + + + + + + + + + + + + + + + +s /33c|j5||jddddS#1swxYwYdS)z8Close the serial port and exit reader thread (uses lock)N)rgrqrGcloser$s r r~zReaderThread.closesZ  IIKKK K                       s.AA Ac|jr8|j|jstd||jfStd)zj Wait until connection is set up and return the transport and protocol instances. connection_lost already calledzalready stopped)rdriwait RuntimeErrorrjr$s r connectzReaderThread.connectsY : 2  ! & & ( ( (: E"#CDDD$-( (011 1rc||j|jst d|jS)zs Enter context handler. May raise RuntimeError in case the connection could not be created. r)startrirrdrrjr$s r __enter__zReaderThread.__enter__sH  ""$$$z A?@@ @}rc.|dS)zLeave context: close portN)r~)r exc_typeexc_valexc_tbs r __exit__zReaderThread.__exit__s r) rrrrr%rqr{r[r~rrrr<r=s@r r`r`s !!!F+++     2 2 2   rr`__main__zloop://c*eZdZfdZdZdZxZS) PrintLinesctt||tjd|ddS)Nz port opened z hello world)r,rr sysstdoutr[r^)r r r.s r r zPrintLines.connection_madesO *d # # 3 3I > > > J  _ - - - OOM * * * * *rcjtjd|dS)Nzline received: {!r} )rrr[formatrs r rTzPrintLines.handle_lines- J  4;;DAA B B B B Brcp|rtj|tjddS)Nz port closed ) traceback print_excrrr[rs r rzPrintLines.connection_losts7 )#C((( J  _ - - - - -r)rrrr rTrr<r=s@r rrsY + + + + +  C C C . . . . . . .rrir0)baudratershellorn)r __future__rrGreobjectrrr?rQThreadr`rrtimerPORTrserial_for_urlserrjr^sleeptrrr r~rrr rs'&&&&& v* U U U U U U U UF. . . . . 8. . . bbbbbbbbb4lllll9#lllb zJJJKKK D . . . . .Z . . . & vq A A AC c: & &(G$$$ 1  & vq A A AC S*%%AGGIII))++Ix    DJqMMMGGIIIIIGs&CCC