3 M]W+ @sddlZddlZddlZddlZddlZy ddlZWnek rRddljZYnXddl m Z m Z m Z m Z ejejejejdZdZGddde ZedkrddlZedZejjd jeejjd ejd ejejjd jejdejdS) N) SerialBaseSerialExceptionportNotOpenErrorto_bytes)debuginfoZwarningerrorc@seZdZdZd:ZddZddZddZddZe ddZ d;ddZ d d!Z d"d#Z d$d%Zd:[?logging={debug|info|warning|error}]": not starting with socket:// ({!r})TloggingzpySerial.socketrzenabled loggingzunknown option: {!r}izport not in range 0...65535z_expected a string in the form "socket://:[?logging={debug|info|warning|error}]": {}N)urlparseZurlsplitschemerr$Zparse_qsZqueryitemsr1Z basicConfigZ getLoggerrZsetLevel LOGGER_LEVELSr ValueErrorZportZhostname)r*ZurlpartsZoptionvalueser,r,r-r es(     zSerial.from_urlcCs,|js ttj|jgggd\}}}t|S)z9Return the number of bytes currently in the input buffer.r)rrselectr"len)r*ZlrZlwZlxr,r,r- in_waitingszSerial.in_waitingc Cst|js tt}|j}xRt||krjyxtj}tj|jggg|\}}}|sRP|jj|t|}|srt d|j ||dk r|tj|8}|dkrPWqt j k rYqt j k r}zt dj|WYdd}~Xqtk r$}z"|jtjkrt dj|WYdd}~Xqtj k rf}z$|dtjkrVt dj|WYdd}~XqXqWt|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 disconnectedNrzconnection failed ({})zread failed: {})rr bytearrayZ_timeoutr;r0r:r"Zrecvrextendrrrr$OSErrorerrnoZEAGAINbytes) r*sizereadrZ start_timeZready_Zbufr9r,r,r-rDs: $z Serial.readcCsZ|js ty|jjt|Wn2tjk rP}ztdj|WYdd}~XnXt |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. zsocket connection failed: {}N) rrr"Zsendallrrrrr$r;)r*datar9r,r,r-writes z Serial.writecCs |js t|jr|jjddS)z9Clear input buffer, discarding all that is in the buffer.zignored reset_input_bufferN)rrrr)r*r,r,r-r(szSerial.reset_input_buffercCs |js t|jr|jjddS)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"|jjdj|dS)za Send break condition. Timed, returns to idle state after given duration. zignored send_break({!r})N)rrrrr$)r*Zdurationr,r,r- send_breakszSerial.send_breakcCs|jr|jjdj|jdS)zJSet break: Controls TXD. When active, to transmitting is possible.z!ignored _update_break_state({!r})N)rrr$Z _break_state)r*r,r,r-_update_break_stateszSerial._update_break_statecCs|jr|jjdj|jdS)z)Set terminal status line: Request To Sendzignored _update_rts_state({!r})N)rrr$Z _rts_state)r*r,r,r-r'szSerial._update_rts_statecCs|jr|jjdj|jdS)z-Set terminal status line: Data Terminal Readyzignored _update_dtr_state({!r})N)rrr$Z _dtr_state)r*r,r,r-r&szSerial._update_dtr_statecCs |js t|jr|jjddS)z(Read terminal status line: Clear To Sendzreturning dummy for ctsT)rrrr)r*r,r,r-ctss  z Serial.ctscCs |js t|jr|jjddS)z)Read terminal status line: Data Set Readyzreturning dummy for dsrT)rrrr)r*r,r,r-dsrs  z Serial.dsrcCs |js t|jr|jjddS)z)Read terminal status line: Ring Indicatorzreturning dummy for riF)rrrr)r*r,r,r-ri s  z Serial.ricCs |js t|jr|jjddS)z)Read terminal status line: Carrier Detectzreturning dummy for cd)T)rrrr)r*r,r,r-cds  z Serial.cdcCs |jjS)z@Get the file handle of the underlying socket for use with select)r"fileno)r*r,r,r-rO!sz Serial.filenoN)r r r rrrrrrrrrrrrrr)r=)rH)__name__ __module__ __qualname____doc__Z BAUDRATESr.r%r/r propertyr<rDrGr(r)rIrJr'r&rKrLrMrNrOr,r,r,r-r *s*  1  r __main__zsocket://localhost:7000z{} z write... shello z read: {} )rAr1r:rr0r2 ImportErrorZ urllib.parseparseZserial.serialutilrrrrDEBUGINFOZWARNINGZERRORr5r!r rPsyssstdoutrGr$flushrDr/r,r,r,r-s4