a wWôW‡'ã@s ddlZddlZddlZz ddlZWneyBddlmZYn0z ddlZWneyjddlZYn0ddl m Z m Z m Z m Z mZmZejejejejdœZGdd„de ƒZedkrddlZedƒZej d e¡¡ej d ¡e d ¡e ¡ej d  e d ¡¡¡e ¡dS) éN)Ú SerialBaseÚSerialExceptionÚto_bytesÚ iterbytesÚwriteTimeoutErrorÚportNotOpenError)ÚdebugÚinfoZwarningÚerrorcsÎeZdZdZdZ‡fdd„Zdd„Z‡fdd„Zd d „Zd d „Z e d d„ƒZ d*dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Ze d"d#„ƒZe d$d%„ƒZe d&d'„ƒZe d(d)„ƒZ‡ZS)+ÚSerialzSSerial port implementation that simulates a loop back connection in plain software.)é2éKéné†é–éÈi,iXi°ii` iÀi€%iKi–iáiÂcs2d|_d|_d|_d|_tt|ƒj|i|¤ŽdS)NiF)Ú buffer_sizeÚqueueÚloggerÚ _cancel_writeÚsuperr Ú__init__)ÚselfÚargsÚkwargs©Ú __class__©úC/usr/lib/python3.9/site-packages/serial/urlhandler/protocol_loop.pyr-s zSerial.__init__cCs~|jrtdƒ‚d|_t |j¡|_|jdur4tdƒ‚| |j¡|  ¡d|_|j s\|  ¡|j sj|  ¡| ¡| ¡dS)zx Open port with current settings. This may throw a SerialException if the port cannot be opened. zPort is already open.Nz.Port must be configured before it can be used.T)Úis_openrrrÚQueuerZ_portÚfrom_urlÚportÚ_reconfigure_portZ_dsrdtrÚ_update_dtr_stateZ_rtsctsÚ_update_rts_stateÚreset_input_bufferÚreset_output_buffer©rrrrÚopen4s  z Serial.opencsD|jr2d|_z|j d¡Wntjy0Yn0tt|ƒ ¡dS)NF)rrÚ put_nowaitZFullrr Úcloser(rrrr+Osz Serial.closecCsLt|jtjƒr&d|jkr$dks6ntd |j¡ƒ‚|jrH|j d¡dS)zx Set communication parameters on opened port. For the loop:// protocol all settings are ignored! rlzinvalid baudrate: {!r}z_reconfigure_port()N)Ú isinstanceÚ _baudrateÚnumbersZIntegralÚ ValueErrorÚformatrr r(rrrr#Xs&zSerial._reconfigure_portc CsÈt |¡}|jdkr$td |j¡ƒ‚zlt |jd¡ ¡D]T\}}|dkr~t  ¡t  d¡|_ |j   t |d¡|j  d¡q8td |¡ƒ‚q8Wn2tyÂ}ztd  |¡ƒ‚WYd }~n d }~00d S) z(extract host and port from an URL stringZloopznexpected a string in the form "loop://[?logging={debug|info|warning|error}]": not starting with loop:// ({!r})TÚloggingz pySerial.looprzenabled loggingzunknown option: {!r}zPexpected a string in the form "loop://[?logging={debug|info|warning|error}]": {}N)ÚurlparseÚurlsplitÚschemerr0Úparse_qsÚqueryÚitemsr1Z basicConfigZ getLoggerrZsetLevelÚ LOGGER_LEVELSrr/)rÚurlÚpartsÚoptionÚvaluesÚerrrr!cs*  þÿ ÿÿzSerial.from_urlcCs2|js t‚|jr(|j d |j ¡¡¡|j ¡S)z9Return the number of bytes currently in the input buffer.zin_waiting -> {:d})rrrrr0rÚqsizer(rrrÚ in_waiting|s zSerial.in_waitingécCsÈ|js t‚|jdur.|jdkr.t ¡|j}nd}tƒ}|dkrÀ|jrÀz|jj|jd}Wn"tjy||jdkrxYqÀYn0|durÀ||7}|d8}nqÀ|r8t ¡|kr8|jrÀ|j  d¡qÀq8t |ƒS)zÑ Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read. Nr©Útimeoutr@z read timeout) rrÚ_timeoutÚtimeÚ bytearrayrÚgetÚEmptyrr Úbytes)rÚsizerBÚdataÚbrrrÚread‡s*    z Serial.readcCs|j d¡dS)N)rr*r(rrrÚ cancel_read¨szSerial.cancel_readcCs d|_dS)NT)rr(rrrÚ cancel_write«szSerial.cancel_writecCs¢d|_|jst‚t|ƒ}dt|ƒ|j}|jdurz||jkrz|j}|dkrl|jslt t |dƒ¡|d8}qD|jrvdSt ‚t |ƒD]}|j j ||jdq‚t|ƒS)z² Output the given byte string over the serial port. Can block if the connection is blocked. May raise SerialException if the connection is closed. Fg$@Nrgà?rA)rrrrÚlenr-Z_write_timeoutrDÚsleepÚminrrrZput)rrJZtime_used_to_sendZ time_leftZbyterrrÚwrite®s   z Serial.writecCsP|js t‚|jr|j d¡z|j ¡r4|j ¡qWntjyJYn0dS)z9Clear input buffer, discarding all that is in the buffer.zreset_input_buffer()N©rrrr rr>Z get_nowaitrGr(rrrr&És  zSerial.reset_input_buffercCsP|js t‚|jr|j d¡z|j ¡r4|j ¡qWntjyJYn0dS)zs Clear output buffer, aborting the current output and discarding all that is in the buffer. zreset_output_buffer()NrSr(rrrr'Õs  zSerial.reset_output_buffercCs|jr|j d |j¡¡dS)z[ Set break: Controls TXD. When active, to transmitting is possible. z_update_break_state({!r})N)rr r0Z _break_stater(rrrÚ_update_break_stateäszSerial._update_break_statecCs|jr|j d |j¡¡dS)z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)rr r0Ú _rts_stater(rrrr%ìszSerial._update_rts_statecCs|jr|j d |j¡¡dS)z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRN©rr r0Z _dtr_stater(rrrr$ñszSerial._update_dtr_statecCs*|js t‚|jr$|j d |j¡¡|jS)z(Read terminal status line: Clear To SendzCTS -> state of RTS ({!r}))rrrr r0rUr(rrrÚctsös z Serial.ctscCs |jr|j d |j¡¡|jS)z)Read terminal status line: Data Set ReadyzDSR -> state of DTR ({!r})rVr(rrrÚdsrÿsz Serial.dsrcCs |js t‚|jr|j d¡dS)z)Read terminal status line: Ring Indicatorzreturning dummy for RIF©rrrr r(rrrÚris  z Serial.ricCs |js t‚|jr|j d¡dS)z)Read terminal status line: Carrier Detectzreturning dummy for CDTrYr(rrrÚcds  z Serial.cd)r@)Ú__name__Ú __module__Ú __qualname__Ú__doc__Z BAUDRATESrr)r+r#r!Úpropertyr?rLrMrNrRr&r'rTr%r$rWrXrZr[Ú __classcell__rrrrr 's4    !    r Ú__main__zloop://z{} z write... zhello z read: {!r} é)r1r.rDr2Ú ImportErrorÚ urllib.parseÚparserr Zserial.serialutilrrrrrrÚDEBUGÚINFOZWARNINGZERRORr8r r\ÚsysÚsÚstdoutrRr0ÚflushrLr+rrrrÚs6     üw