a =Y27ã@s ddlZddlZddlZddlZddlZz ddlZWneyRddlmZYn0ddl m Z m Z m Z m Z mZmZejejejejdœZdZGdd„de ƒZedkrddlZedƒZej d  e¡¡ej d ¡e d ¡e ¡ej d  e d¡¡¡e ¡dS) éN)Ú SerialBaseÚSerialExceptionÚto_bytesÚportNotOpenErrorÚwriteTimeoutErrorÚTimeout)ÚdebugÚinfoZwarningÚerroréc@s¼eZdZdZdZdd„Zdd„Zdd„Zd d „Ze d d „ƒZ d*dd„Z dd„Z dd„Z dd„Zd+dd„Zdd„Zdd„Zdd„Ze dd „ƒZe d!d"„ƒZe d#d$„ƒZe d%d&„ƒZd'd(„Zd)S),ÚSerialz-Serial port implementation for plain sockets.)é2éKéné†é–éÈi,iXi°ii` iÀi€%iKi–iáiÂc CsÊd|_|jdurtdƒ‚|jr&tdƒ‚ztj| |j¡td|_ Wn<t y~}z$d|_ td  |j|¡ƒ‚WYd}~n d}~00|j   d¡|  ¡d|_|js¨| ¡|js¶| ¡| ¡| ¡dS)zx Open port with current settings. This may throw a SerialException if the port cannot be opened. Nz.Port must be configured before it can be used.zPort is already open.)ÚtimeoutzCould not open port {}: {}FT)ÚloggerZ_portrÚis_openÚsocketZcreate_connectionÚfrom_urlZportstrÚ POLL_TIMEOUTÚ_socketÚ ExceptionÚformatZ setblockingÚ_reconfigure_portZ_dsrdtrÚ_update_dtr_stateZ_rtsctsÚ_update_rts_stateÚreset_input_bufferÚreset_output_buffer)ÚselfÚmsg©r#úE/usr/lib/python3.9/site-packages/serial/urlhandler/protocol_socket.pyÚopen1s& ( z Serial.opencCs(|jdurtdƒ‚|jr$|j d¡dS)zz Set communication parameters on opened port. For the socket:// protocol all settings are ignored! NzCan only operate on open portsz!ignored port configuration change)rrrr ©r!r#r#r$rOs zSerial._reconfigure_portcCsP|jrL|jr:[?logging={debug|info|warning|error}]": not starting with socket:// ({!r})TÚloggingzpySerial.socketrzenabled loggingzunknown option: {!r}izport not in range 0...65535z_expected a string in the form "socket://:[?logging={debug|info|warning|error}]": {}N)ÚurlparseÚurlsplitÚschemerrÚparse_qsÚqueryÚitemsr*Z basicConfigZ getLoggerrZsetLevelÚ LOGGER_LEVELSrÚ ValueErrorÚportÚhostname)r!ÚurlÚpartsÚoptionÚvaluesÚer#r#r$rhs0  þÿ  ÿÿzSerial.from_urlcCs,|js t‚t |jgggd¡\}}}t|ƒS)z9Return the number of bytes currently in the input buffer.r)rrÚselectrÚlen)r!ZlrZlwZlxr#r#r$Ú in_waiting…szSerial.in_waitingéc CsB|js t‚tƒ}t|jƒ}t|ƒ|kr:zVt |jggg| ¡¡\}}}|sRWq:|j  |t|ƒ¡}|srt dƒ‚|  |¡Wn¬t yÎ}z8|j t jt jt jt jt jfvrºt d |¡ƒ‚WYd}~nfd}~0tjtjfy*}z<|dt jt jt jt jt jfvrt d |¡ƒ‚WYd}~n d}~00| ¡rq:qt|ƒ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. zsocket disconnectedúread failed: {}Nr)rrÚ bytearrayrÚ_timeoutr;r:rÚ time_leftÚrecvrÚextendÚOSErrorÚerrnoÚEAGAINÚEALREADYÚ EWOULDBLOCKÚ EINPROGRESSÚEINTRrr rÚexpiredÚbytes)r!ÚsizeÚreadrÚreadyÚ_Úbufr9r#r#r$rN’s, ""$z Serial.readc Cs |js t‚t|ƒ}t|ƒ}}t|jƒ}|dkr”z|j |¡}|jrL|WS|j s†|  ¡r^t ‚t   g|jgg|  ¡¡\}}}|s¬t ‚n&t   g|jggd¡\}}}|s¬tdƒ‚||d…}||8}Wn¼tyÖ‚Ynªty(} z:| jtjtjtjtjtjfvrtd | ¡ƒ‚WYd} ~ n`d} ~ 0t jy~} z<| dtjtjtjtjtjfvrjtd | ¡ƒ‚WYd} ~ n d} ~ 00|js(|  ¡r(t ‚q(|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. rNzwrite failed (select)zwrite failed: {})rrrr;rZ_write_timeoutrÚsendZis_non_blockingZ is_infiniterKrr:rArrDrErFrGrHrIrJrr ) r!ÚdataÚdZtx_lenÚlengthrÚnrPrOr9r#r#r$Úwrite»s@        ""$z Serial.writec Csì|js t‚d}|rèt |jgggd¡\}}}z|j d¡WqtyŒ}z8|jtjtjtj tj tj fvrxt d  |¡ƒ‚WYd}~qd}~0tjtjfyä}z:|dtjtjtj tj tj fvrÐt d  |¡ƒ‚WYd}~qd}~00qdS)z9Clear input buffer, discarding all that is in the buffer.Trir>N)rrr:rrBrDrErFrGrHrIrJrrr r)r!rOrPr9r#r#r$rðs" zSerial.reset_input_buffercCs |js t‚|jr|j d¡dS)zs Clear output buffer, aborting the current output and discarding all that is in the buffer. zignored reset_output_bufferN©rrrr r&r#r#r$r szSerial.reset_output_bufferçÐ?cCs&|js t‚|jr"|j d |¡¡dS)za Send break condition. Timed, returns to idle state after given duration. zignored send_break({!r})N)rrrr r)r!Zdurationr#r#r$Ú send_breakszSerial.send_breakcCs|jr|j d |j¡¡dS)zJSet break: Controls TXD. When active, to transmitting is possible.z!ignored _update_break_state({!r})N)rr rZ _break_stater&r#r#r$Ú_update_break_stateszSerial._update_break_statecCs|jr|j d |j¡¡dS)z)Set terminal status line: Request To Sendzignored _update_rts_state({!r})N)rr rZ _rts_stater&r#r#r$r"szSerial._update_rts_statecCs|jr|j d |j¡¡dS)z-Set terminal status line: Data Terminal Readyzignored _update_dtr_state({!r})N)rr rZ _dtr_stater&r#r#r$r'szSerial._update_dtr_statecCs |js t‚|jr|j d¡dS)z(Read terminal status line: Clear To Sendzreturning dummy for ctsTrXr&r#r#r$Úcts,s  z Serial.ctscCs |js t‚|jr|j d¡dS)z)Read terminal status line: Data Set Readyzreturning dummy for dsrTrXr&r#r#r$Údsr5s  z Serial.dsrcCs |js t‚|jr|j d¡dS)z)Read terminal status line: Ring Indicatorzreturning dummy for riFrXr&r#r#r$Úri>s  z Serial.ricCs |js t‚|jr|j d¡dS)z)Read terminal status line: Carrier Detectzreturning dummy for cd)TrXr&r#r#r$ÚcdGs  z Serial.cdcCs |j ¡S)z@Get the file handle of the underlying socket for use with select)rÚfilenor&r#r#r$r`Ssz Serial.filenoN)r=)rY)Ú__name__Ú __module__Ú __qualname__Ú__doc__Z BAUDRATESr%rr'rÚpropertyr<rNrWrr rZr[rrr\r]r^r_r`r#r#r#r$r +s2  )5     r Ú__main__zsocket://localhost:7000z{} z write... shello z read: {} ) rEr*r:rr(r+Ú ImportErrorÚ urllib.parseÚparseZserial.serialutilrrrrrrÚDEBUGÚINFOZWARNINGZERRORr1rr raÚsysÚsÚstdoutrWrÚflushrNr'r#r#r#r$Ús6   ü1