3 M]W%@s"ddlZddlZddlZy ddlZWnek rBddljZYnXy ddlZWnek rlddlZYnXddl m Z m Z m Z m Z mZmZejejejejdZGddde ZedkrddlZedZejjdjeejjd ejd ejejjd jejd ejdS) N) SerialBaseSerialExceptionto_bytes iterbyteswriteTimeoutErrorportNotOpenError)debuginfoZwarningerrorcseZdZdZd6ZfddZddZfddZddZddZ e ddZ d7d d!Z d"d#Z d$d%Zd&d'Zd(d)Zd*d+Zd,d-Ze d.d/Ze d0d1Ze d2d3Ze d4d5ZZS)8SerialzSSerial port implementation that simulates a loop back connection in plain software.2Kn,X` %Kcs(tt|j||d|_d|_d|_dS)Ni)superr __init__ buffer_sizequeuelogger)selfargskwargs) __class__#/usr/lib/python3.6/protocol_loop.pyr-szSerial.__init__cCs~|jrtdd|_tj|j|_|jdkr4td|j|j|j d|_|j s\|j |j sj|j |j|jdS)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_openrr!r QueuerZ_portfrom_urlZport_reconfigure_portZ_dsrdtr_update_dtr_stateZ_rtscts_update_rts_statereset_input_bufferreset_output_buffer)r"r&r&r'open3s  z Serial.openc sF|jr4d|_y|jjdWntjk r2YnXtt|jdS)NF)r(r Z put_nowaitZFullrr close)r")r%r&r'r1Nsz Serial.closecCsRt|jtj s,d|jko$dkn r {:d})r(rr!rr8r qsize)r"r&r&r' in_waiting{s zSerial.in_waitingc Cs|js t|jdk r.|jdkr.tj|j}nd}t}x|dkr|jry|jj|jd}Wn"tjk r~|jdkrzPYnX|dk r||7}|d8}nP|r:tj|kr:|jr|jj dPq:Wt |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)timeoutrCz read timeout) r(rZ_timeouttime bytearrayr getEmptyr!r bytes)r"sizerDdatabr&r&r'reads*   z Serial.readcCst|js tt|}dt||j}|jdk rH||jkrHtj|jtx"t |D]}|j j ||jdqRWt|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. g$@N)rD) r(rrlenr5Z_write_timeoutrEZsleeprrr Zput)r"rKZtime_used_to_sendZbyter&r&r'writes z Serial.writec CsV|js t|jr|jjdyx|jjr6|jjq WWntjk rPYnXdS)z9Clear input buffer, discarding all that is in the buffer.zreset_input_buffer()N)r(rr!r r rA get_nowaitrH)r"r&r&r'r.s  zSerial.reset_input_bufferc CsV|js t|jr|jjdyx|jjr6|jjq WWntjk rPYnXdS)zs Clear output buffer, aborting the current output and discarding all that is in the buffer. zreset_output_buffer()N)r(rr!r r rArPrH)r"r&r&r'r/s  zSerial.reset_output_buffercCs|jr|jjdj|jdS)z[ Set break: Controls TXD. When active, to transmitting is possible. z_update_break_state({!r})N)r!r r8Z _break_state)r"r&r&r'_update_break_stateszSerial._update_break_statecCs|jr|jjdj|jdS)z)Set terminal status line: Request To Sendz'_update_rts_state({!r}) -> state of CTSN)r!r r8 _rts_state)r"r&r&r'r-szSerial._update_rts_statecCs|jr|jjdj|jdS)z-Set terminal status line: Data Terminal Readyz'_update_dtr_state({!r}) -> state of DSRN)r!r r8 _dtr_state)r"r&r&r'r,szSerial._update_dtr_statecCs*|js t|jr$|jjdj|j|jS)z(Read terminal status line: Clear To SendzCTS -> state of RTS ({!r}))r(rr!r r8rR)r"r&r&r'ctss z Serial.ctscCs |jr|jjdj|j|jS)z)Read terminal status line: Data Set ReadyzDSR -> state of DTR ({!r}))r!r r8rS)r"r&r&r'dsrsz Serial.dsrcCs |js t|jr|jjddS)z)Read terminal status line: Ring Indicatorzreturning dummy for RIF)r(rr!r )r"r&r&r'ris  z Serial.ricCs |js t|jr|jjddS)z)Read terminal status line: Carrier Detectzreturning dummy for CDT)r(rr!r )r"r&r&r'cds  z Serial.cd)r r rrrrrrrrrrrrrrr)rC)__name__ __module__ __qualname____doc__Z BAUDRATESrr0r1r+r*propertyrBrMrOr.r/rQr-r,rTrUrVrW __classcell__r&r&)r%r'r 's(    !   r __main__zloop://z{} z write... zhello z read: {!r} )r9r6rEr: ImportErrorZ urllib.parseparser r)Zserial.serialutilrrrrrrDEBUGINFOZWARNINGZERRORr=r rXsyssstdoutrOr8flushrMr1r&r&r&r's4    i