dbfdg 3 M]W;@sdZddlZddlZGdddejZejddZejddZed krGd d d ej Z ej Z ee e d d dZ e je e je jdS)zSupport asyncio with serial ports. EXPERIMENTAL Posix platforms only, Python 3.4+ only. Windows event loops can not wait for serial ports with the current implementation. It should be possible to get that working though. NcseZdZdZfddZeddZddZdd Zd d Z d d Z ddZ ddZ ddZ ddZd:ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd;d-d.ZdSerialTransportugAn asyncio transport model of a serial communication channel. A transport class is an abstraction of a communication channel. This allows protocol implementations to be developed against the transport abstraction without needing to know the details of the underlying channel, such as whether it is a pipe, a socket, or indeed a serial port. You generally won’t instantiate a transport yourself; instead, you will call `create_serial_connection` which will create the transport and try to initiate the underlying communication channel, calling you back when it succeeds. csvtj||_||_||_d|_d|_d|_g|_|j d|_ d|_ d|j_ d|j_ |j|j||j|jdS)NFir)super__init___loop _protocol_serial_closing_protocol_paused_max_read_size _write_buffer_set_write_buffer_limits _has_reader _has_writerZtimeoutZ write_timeout call_soonconnection_made_ensure_reader)selfloopprotocolZserial_instance) __class__/usr/lib/python3.6/aio.pyr%s zSerialTransport.__init__cCs|jS)zThe underlying Serial instance.)r)rrrrserial<szSerialTransport.serialcCs dj|dS)NzH{self.__class__.__name__}({self._loop}, {self._protocol}, {self.serial}))r)format)rrrr__repr__AszSerialTransport.__repr__cCs|jS)z2Return True if the transport is closing or closed.)r)rrrr is_closingDszSerialTransport.is_closingcCs|js|jddS)aDClose the transport gracefully. Any buffered data will be written asynchronously. No more data will be received and further writes will be silently ignored. After all buffered data is flushed, the protocol's connection_lost() method will be called with None as its argument. N)r_close)rrrrcloseHs zSerialTransport.closecCsXy|jj|j}Wn0tjk rB}z|j|dWYdd}~XnX|rT|jj|dS)N)exc)rreadr rSerialExceptionrr data_received)rdataerrr _read_readyTs zSerialTransport._read_readycCs|jr dS|jdkrzy|jj|}Wn.tjk rT}z|j|ddSd}~XnX|t|krfdS||d}|j|j j ||j dS)zWrite some data to the transport. This method does not block; it buffers the data and arranges for it to be sent out asynchronously. Writes made after the transport has been closed will be ignored.Nrz%Fatal write error on serial transport) rget_write_buffer_sizerwriterr _fatal_errorlen_ensure_writerr append_maybe_pause_protocol)rr"nrrrrr&]s     zSerialTransport.writecCsdS)z_Serial ports do not support the concept of end-of-file. Always returns False. Fr)rrrr can_write_eofvszSerialTransport.can_write_eofcCs |jdS)uPause the receiving end of the transport. No data will be passed to the protocol’s data_received() method until resume_reading() is called. N)_remove_reader)rrrr pause_reading}szSerialTransport.pause_readingcCs |jdS)zResume the receiving end of the transport. Incoming data will be passed to the protocol's data_received() method until pause_reading() is called. N)r)rrrrresume_readingszSerialTransport.resume_readingNcCs|j||d|jdS)uLSet the high- and low-water limits for write flow control. These two values control when call the protocol’s pause_writing()and resume_writing() methods are called. If specified, the low-water limit must be less than or equal to the high-water limit. Neither high nor low can be negative. )highlowN)r r+)rr1r2rrrset_write_buffer_limitssz'SerialTransport.set_write_buffer_limitscCsttt|jS)zThe number of bytes in the write buffer. This buffer is unbounded, so the result may be larger than the the high water mark. )summapr(r )rrrrr%sz%SerialTransport.get_write_buffer_sizecCs tddS)Nz-Serial connections do not support end-of-file)NotImplementedError)rrrr write_eofszSerialTransport.write_eofcCs|jddS)a.Close the transport immediately. Pending operations will not be given opportunity to complete, and buffered data will be lost. No more data will be received and further writes will be ignored. The protocol's connection_lost() method will eventually be called. N)_abort)rrrrabortszSerialTransport.abortcCsl|j|jkrdS|jshd|_y|jjWn:tk rf}z|jjd|||jdWYdd}~XnXdS)a To be called whenever the write-buffer size increases. Tests the current write-buffer size against the high water mark configured for this transport. If the high water mark is exceeded, the protocol is instructed to pause_writing(). NTzprotocol.pause_writing() failed)message exception transportr)r% _high_waterr r pause_writing Exceptionrcall_exception_handler)rrrrrr+sz%SerialTransport._maybe_pause_protocolcCsh|jrd|j|jkrdd|_y|jjWn:tk rb}z|jjd|||jdWYdd}~XnXdS)a-To be called whenever the write-buffer size decreases. Tests the current write-buffer size against the low water mark configured for this transport. If the write-buffer size is below the low water mark, the protocol is instructed that is can resume_writing(). Fz protocol.resume_writing() failed)r:r;r<rN)r r% _low_waterrresume_writingr?rr@)rrrrr_maybe_resume_protocolsz&SerialTransport._maybe_resume_protocolcCsdj|j}t|}|jjy|jj|}WnRttfk rR|jj|Ynt j k r}z|j |dWYdd}~XnXX|t|kr|j |j |jr|jr|jdS||d}|jj||j dS)aAsynchronously write buffered data. This method is called back asynchronously as a writer registered with the asyncio event-loop against the underlying file descriptor for the serial port. Should the write-buffer become empty if this method is invoked while the transport is closing, the protocol's connection_lost() method will be called with None as its argument. z%Fatal write error on serial transportN)joinr r(clearrr&BlockingIOErrorInterruptedErrorr*rr r'_remove_writerrCr_flushedr)rr"Z num_bytesr,rrrr _write_readys&     zSerialTransport._write_readycCs.|j r*|j r*|jj|jj|jd|_dS)NT)r rrZ add_readerrfdr$)rrrrrszSerialTransport._ensure_readercCs |jr|jj|jjd|_dS)NF)r rZ remove_readerrrL)rrrrr.szSerialTransport._remove_readercCs.|j r*|j r*|jj|jj|jd|_dS)NT)rrrZ add_writerrrLrK)rrrrr) szSerialTransport._ensure_writercCs |jr|jj|jjd|_dS)NF)rrZ remove_writerrrL)rrrrrIszSerialTransport._remove_writercCsd|dkr|dkrdnd|}|dkr,|d}||ko>dknsTtd||f||_||_dS)z&Ensure consistent write-buffer limits.N@irz*high (%r) must be >= low (%r) must be >= 0i) ValueErrorr=rA)rr1r2rrrr s z(SerialTransport._set_write_buffer_limitsFatal error on serial transportcCs&|jj||||jd|j|dS)z?Report a fatal error to the event-loop and abort the transport.)r:r;r<rN)rr@rr8)rrr:rrrr'"s  zSerialTransport._fatal_errorcCs |jdkS)z3True if the write buffer is empty, otherwise False.r)r%)rrrrrJ,szSerialTransport._flushedcCs2d|_|j|jr.|j|jj|j|dS)aClose the transport gracefully. If the write buffer is already empty, writing will be stopped immediately and a call to the protocol's connection_lost() method scheduled. If the write buffer is not already empty, the asynchronous writing will continue, and the _write_ready method will call this _close method again when the buffer has been flushed completely. TN)rr.rJrIrr_call_connection_lost)rrrrrr0s zSerialTransport._closecCs*d|_|j|j|jj|j|dS)aPClose the transport immediately. Pending operations will not be given opportunity to complete, and buffered data will be lost. No more data will be received and further writes will be ignored. The protocol's connection_lost() method will eventually be called with the passed exception. TN)rr.rIrrrQ)rrrrrr8Bs zSerialTransport._abortc CsH|jjz|jj|Wd|jj|jjd|_d|_d|_XdS)zClose the connection. Informs the protocol through connection_lost() and clears pending buffers and closes the serial connection. N)rflushrconnection_lostr rFrr)rrrrrrQPs   z%SerialTransport._call_connection_lost)NN)NN)rP)N)!__name__ __module__ __qualname____doc__rpropertyrrrrr$r&r-r/r0r3r%r7r9r+rCrKrr.r)rIr r'rJrr8rQ __classcell__rr)rrrs8      +  rcOs&tj||}|}t|||}||fS)N)rZserial_for_urlr)rprotocol_factoryargskwargsZserrr<rrrcreate_serial_connectionds  r]c+s|jdtj}|jdtjj}tj||d}tj||dx2tf|fddd|D]\}}||fVqZWtj|||}t ||dS) aA wrapper for create_serial_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a StreamWriter instance. The arguments are all the usual arguments to Serial(). Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader. This function is a coroutine. rlimit)r^r)rcsS)Nrr)rrrsz(open_serial_connection..)rrZN) getasyncioget_event_loopZstreamsZ_DEFAULT_LIMIT StreamReaderZStreamReaderProtocolr] StreamWriter StopIteration)r\rr^readerr<_writerr)rropen_serial_connectionlsri__main__c@s4eZdZddZddZddZddZd d Zd S) OutputcCs&||_td|d|j_|jddS)Nz port openedFs hello world )r<printrZrtsr&)rr<rrrrs zOutput.connection_madecCs$tdt|d|kr |jjdS)Nz data received )rlreprr<r)rr"rrrr!szOutput.data_receivedcCstdtjjdS)Nz port closed)rlrarbstop)rrrrrrSszOutput.connection_lostcCstdt|jjdS)Nz pause writing)rlr<r%)rrrrr>szOutput.pause_writingcCst|jjtddS)Nzresume writing)rlr<r%)rrrrrBszOutput.resume_writingN)rTrUrVrr!rSr>rBrrrrrks rkz /dev/ttyUSB0i)Zbaudrate)rWrarZ Transportr coroutiner]rirTZProtocolrkrbrcoroZrun_until_completeZ run_foreverrrrrrsQ#