o &a_%U @sddlmZddlZddlZzeWneefy%GdddeZYnwzeWn eefy7e ZYnwze WneefyJe fZ YnwddZ ddZ e d gZ e d gZe d gZe d gZd \ZZZZZd\ZZZd\ZZZZededededediZGdddeZGdddeZ GdddeZ!GdddeZ"Gdddej#Z$e%dkrddl&Z&e$Z'e&j()d *e'j+e&j()d!*e'j,e&j()d"*e'j-e&j()d#*e'j.e&j()d$*e'j/e&j()d%*e'dSdS)&)absolute_importNc@s eZdZdS) memoryviewN)__name__ __module__ __qualname__rr3/usr/lib/python3/dist-packages/serial/serialutil.pyrsrccsDt|tr |}d} |||d}|d7}|r|VndSq )z=Iterate over bytes, returning bytes instead of ints (python3)rTN) isinstancertobytes)biarrr iterbytes(s rcCsVt|tr|St|trt|St|tr|St|tr%td|tt|S)z"convert a sequence to a bytes typez?unicode strings are not supported, please encode to bytes: {!r})r bytes bytearrayrr unicode TypeErrorformat)seqrrrto_bytes8s     r )NEOMS)r g?)NoneEvenOddMarkSpacec@eZdZdZdS)SerialExceptionz.Base class for serial port related exceptions.Nrrr__doc__rrrrr+\r+c@r*)SerialTimeoutExceptionz Write timeouts give an exceptionNr,rrrrr/`r.r/cs eZdZdZfddZZS)PortNotOpenErrorzPort is not opencstt|ddS)Nz)Attempting to use a port that is not open)superr0__init__self __class__rrr2fszPortNotOpenError.__init__)rrrr-r2 __classcell__rrr5rr0dsr0c@sHeZdZdZeedrejZnejZddZddZ ddZ d d Z d S) Timeouta> Abstraction for timeout operations. Using time.monotonic() if available or time.time() in all other cases. The class can also be initialized with 0 or None, in order to support non-blocking and fully blocking I/O operations. The attributes is_non_blocking and is_infinite are set accordingly. monotoniccCs>|du|_|dk|_||_|dur|||_dSd|_dS)z(Initialize a timeout with given durationNr) is_infiniteis_non_blockingdurationTIME target_timer4r<rrrr2s   zTimeout.__init__cCs|jduo |dkS)z4Return a boolean, telling if the timeout has expiredNr)r> time_leftr3rrrexpiredszTimeout.expiredcCsL|jrdS|jr dS|j|}||jkr!||j|_|jStd|S)z:Return how many seconds are left until the timeout expiresrN)r;r:r>r=r<max)r4deltarrrr@s  zTimeout.time_leftcCs||_|||_dS)zb Restart a timeout, only supported if a timeout was already set up before. N)r<r=r>r?rrrrestartszTimeout.restartN) rrrr-hasattrtimer9r=r2rAr@rDrrrrr8js   r8c @seZdZdZdZeeeefZ e e e e efZeeefZddee edddddddf ddZedd Zejd d Zed d Zejd d ZeddZejddZeddZejddZeddZejddZeddZejddZeddZejddZeddZejddZed d!Zejd"d!Zed#d$Z e jd%d$Z ed&d'Z!e!jd(d'Z!ed)d*Z"e"jdwd+d*Z"ed,d-Z#e#jd.d-Z#ed/d0Z$e$jd1d0Z$ed2d3Z%e%jd4d3Z%ed5d6Z&e&jd7d6Z&d8Z'd9d:Z(d;d<Z)d=d>Z*d?d@Z+dAdBZ,dCdDZ-dEdFZ.dGdHZ/dIdJZ0dxdLdMZ1dNdOZ2dPdQZ3dRdSZ4dxdTdUZ5dydWdXZ6dydYdZZ7d[d\Z8d]d^Z9d_d`Z:dadbZ;dcddZe>jdjdiZ>dkdlZ?dmdnZ@dodpZAdqdrZBeCdfdsdtZDdudvZEdS)z SerialBasezh Serial port base class. Provides __init__ function and properties to get/set port settings. )2Kni,iXiii` i%iKiiiiii iii@Bii`ii%&i-ig5i =NrNFc Ks d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_||_||_||_||_||_||_| |_||_||_| |_| |_| |_d| vrg| d|_d| vrq| d|_| rzt d!| |dur|"dSdS)z Initialize comm port object. If a "port" is given, then the port will be opened immediately. Otherwise a Serial port object in closed state is returned. FNT writeTimeoutinterCharTimeoutz"unexpected keyword arguments: {!r})#is_openportstrname_port _baudrate _bytesize_parity _stopbits_timeout_write_timeout_xonxoff_rtscts_dsrdtr_inter_byte_timeout _rs485_mode _rts_state _dtr_state _break_state _exclusiveportbaudratebytesizeparitystopbitstimeout write_timeoutxonxoffrtsctsdsrdtrinter_byte_timeout exclusivepop ValueErrorropen)r4rdrerfrgrhrirkrlrjrmrnrokwargsrrrr2sP   zSerialBase.__init__cC|jS)z{ Get the current port setting. The value that was passed on init or using setPort() is passed back. )rTr3rrrrdzSerialBase.portcCs^|durt|tstdt||j}|r|||_||_|j|_ |r-| dSdS)z! Change the port. Nz'"port" must be None or a string, not {}) r basestringrqrtyperQcloserRrTrSrr)r4rdwas_openrrrrds cCrt)z"Get the current baud rate setting.)rUr3rrrrezSerialBase.baudratecCs\zt|}Wntytd|w|dkr td|||_|jr,|dSdS)z Change baud rate. It raises a ValueError if the port is open and the baud rate is not possible. If the port is closed, then the value is accepted and the exception is raised when the port is opened. zNot a valid baudrate: {!r}rN)intrrqrrUrQ_reconfigure_port)r4rer rrrres   cCrt)z"Get the current byte size setting.)rVr3rrrrf-rzzSerialBase.bytesizecC4||jvr td|||_|jr|dSdS)zChange byte size.zNot a valid byte size: {!r}N) BYTESIZESrqrrVrQr|)r4rfrrrrf2  cCrt)z)Get the current exclusive access setting.)rcr3rrrro;rzzSerialBase.exclusivecC||_|jr |dSdS)z$Change the exclusive access setting.N)rcrQr|)r4rorrrro@ cCrt)zGet the current parity setting.)rWr3rrrrgGrzzSerialBase.paritycCr})zChange parity setting.zNot a valid parity: {!r}N)PARITIESrqrrWrQr|)r4rgrrrrgLrcCrt)z"Get the current stop bits setting.)rXr3rrrrhUrzzSerialBase.stopbitscCr})zChange stop bits size.zNot a valid stop bit size: {!r}N)STOPBITSrqrrXrQr|)r4rhrrrrhZrcCrtz Get the current timeout setting.)rYr3rrrricrzzSerialBase.timeoutcCsd|dur$z|dWntytd|w|dkr$td|||_|jr0|dSdS)Change timeout setting.Nr Not a valid timeout: {!r}r)rrqrrYrQr|r4rirrrrihs   cCrtr)rZr3rrrrjvrzzSerialBase.write_timeoutcCd|dur$|dkrtd|z|dWnty#td|w||_|jr0|dSdS)rNrrr )rqrrrZrQr|rrrrrj{   cCrt)z0Get the current inter-character timeout setting.)r^r3rrrrnrzzSerialBase.inter_byte_timeoutcCr)z"Change inter-byte timeout setting.Nrrr )rqrrr^rQr|)r4 ic_timeoutrrrrnrcCrt)z!Get the current XON/XOFF setting.)r[r3rrrrkrzzSerialBase.xonxoffcCr)zChange XON/XOFF setting.N)r[rQr|)r4rkrrrrkrcCrt)z-Get the current RTS/CTS flow control setting.)r\r3rrrrlrzzSerialBase.rtsctscCr)z$Change RTS/CTS flow control setting.N)r\rQr|)r4rlrrrrlrcCrt)z-Get the current DSR/DTR flow control setting.)r]r3rrrrmrzzSerialBase.dsrdtrcCs.|dur |j|_n||_|jr|dSdS)z#Change DsrDtr flow control setting.N)r\r]rQr|)r4rmrrrrms   cCrtN)r`r3rrrrtszSerialBase.rtscC||_|jr |dSdSr)r`rQ_update_rts_stater4valuerrrr cCrtr)rar3rrrdtrrzSerialBase.dtrcCrr)rarQ_update_dtr_staterrrrrrcCrtr)rbr3rrrbreak_conditionrzSerialBase.break_conditioncCrr)rbrQ_update_break_staterrrrrrcCrt)z Enable RS485 mode and apply new settings, set to None to disable. See serial.rs485.RS485Settings for more info about the value. )r_r3rrr rs485_moderuzSerialBase.rs485_modecCrr)r_rQr|)r4rs485_settingsrrrrr) rerfrgrhrkrmrlrirjrncstfddjDS)zb Get current port settings as a dictionary. For use with apply_settings(). csg|] }|td|fqS)_)getattr).0keyr3rr sz+SerialBase.get_settings..)dict_SAVED_SETTINGSr3rr3r get_settingsszSerialBase.get_settingscCs>|jD]}||vr||t|d|krt||||qdS)z Apply stored settings from a dictionary returned from get_settings(). It's allowed to delete keys from the dictionary. These values will simply left unchanged. rN)rrsetattr)r4drrrrapply_settingss zSerialBase.apply_settingscCsdj|jjt||dS)zAString representation of the current port settings and its state.z{name}(port={p.portstr!r}, baudrate={p.baudrate!r}, bytesize={p.bytesize!r}, parity={p.parity!r}, stopbits={p.stopbits!r}, timeout={p.timeout!r}, xonxoff={p.xonxoff!r}, rtscts={p.rtscts!r}, dsrdtr={p.dsrdtr!r}))rSidp)rr6rrr3rrr__repr__szSerialBase.__repr__cCdSNTrr3rrrreadablezSerialBase.readablecCrrrr3rrrwritablerzSerialBase.writablecCr)NFrr3rrrseekable!rzSerialBase.seekablec Cs||t|}t|}z ||d|<W|Sty=}zddl}t||js(||d||d|<WYd}~|Sd}~ww)Nrr )readlenrarrayr )r4r datanerrrrrrreadinto$s zSerialBase.readintocCs|jdur |js ||Sr)rTrQrrr3rrr __enter__3szSerialBase.__enter__cO |dSr)rx)r4argsrsrrr__exit__8 zSerialBase.__exit__?cCs&|jstd|_t|d|_dS)za Send break condition. Timed, returns to idle state after given duration. TFN)rQr0rrFsleepr?rrr send_break=s   zSerialBase.send_breakcCrr)reset_input_bufferr3rrr flushInputKrzSerialBase.flushInputcCrr)reset_output_bufferr3rrr flushOutputNrzSerialBase.flushOutputcCrtr) in_waitingr3rrr inWaitingQzSerialBase.inWaitingcC||dSr)rr?rrr sendBreakTzSerialBase.sendBreakr cC ||_dSr)rrrrrsetRTSW zSerialBase.setRTScCrr)rrrrrsetDTRZrzSerialBase.setDTRcCrtr)ctsr3rrrgetCTS]rzSerialBase.getCTScCrtr)dsrr3rrrgetDSR`rzSerialBase.getDSRcCrtr)rir3rrrgetRIcrzSerialBase.getRIcCrtr)cdr3rrrgetCDfrzSerialBase.getCDcCrr)rd)r4rdrrrsetPortirzSerialBase.setPortcCrtrrjr3rrrrOlrzSerialBase.writeTimeoutcCrrrrrrrrOp cCrtrrnr3rrrrPtrzSerialBase.interCharTimeoutcCrrr)r4rPrrrrPxrcCs|Sr)rr3rrrgetSettingsDict|szSerialBase.getSettingsDictcCrr)r)r4rrrrapplySettingsDictrzSerialBase.applySettingsDictcCrtr)rQr3rrrisOpenrzSerialBase.isOpencCs ||jS)zL Read all bytes currently available in the buffer of the OS. )rrr3rrrread_alls zSerialBase.read_allcCst|}t}t|j} |d}|r6||7}|| d|kr& t|S|dur5t||kr5 t|Sn t|S|rD t|Sq )z Read until an expected sequence is found (' ' by default), the size is exceeded or until timeout occurs. Tr N)rrr8rYrrAr)r4expectedsizelentermlinericrrr read_untils(  zSerialBase.read_untilcos$ |j|i|}|sdS|Vq)zs Read lines, implemented as generator. It will raise StopIteration on timeout (empty read). TN)r)r4rrsrrrr iread_untilszSerialBase.iread_untilr)r)r )Frrrr- BAUDRATESFIVEBITSSIXBITS SEVENBITS EIGHTBITSr~ PARITY_NONE PARITY_EVEN PARITY_ODD PARITY_MARK PARITY_SPACEr STOPBITS_ONESTOPBITS_ONE_POINT_FIVE STOPBITS_TWOrr2propertyrdsetterrerfrorgrhrirjrnrkrlrmrrrrrrrrrrrrrrrrrrrrrrrrrrrOrPrrrrLFrrrrrrrGs   J                                           rG__main__zport name: {} zbaud rates: {} zbyte sizes: {} zparities: {} zstop bits: {} z{} )0 __future__riorFr NameErrorAttributeErrorobjectrstrrvrrXONXOFFCRrrrrrrrrrrrrr PARITY_NAMESIOErrorr+r/r0r8 RawIOBaserGrsyssstdoutwriterrSrr~rrrrrrsh         ;