a gYT @sddlZddlZzeWn&eefy>GdddeZYn0zeWneefybeZYn0zeWneefyefZYn0ddZ ddZ e dgZ e d gZ e d gZ e d gZd \ZZZZZd \ZZZd\ZZZZededededediZGdddeZGdddeZedZedZ 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&dS)%Nc@s eZdZdS) memoryviewN)__name__ __module__ __qualname__rr5/usr/lib/python3.9/site-packages/serial/serialutil.pyrsrccsBt|tr|}d}|||d}|d7}|r>|Vqq>qdS)z=Iterate over bytes, returning bytes instead of ints (python3)rN) isinstancertobytes)biarrr iterbytes&s rcCs\t|tr|St|tr t|St|tr2|St|trLtd|n tt|SdS)z"convert a sequence to a bytes typez?unicode strings are not supported, please encode to bytes: {!r}N)r bytes bytearrayrr unicode TypeErrorformat)seqrrrto_bytes6s    r )NEOMS)rg?)NoneZEvenZOddZMarkZSpacec@seZdZdZdS)SerialExceptionz.Base class for serial port related exceptions.Nrrr__doc__rrrrr%Zsr%c@seZdZdZdS)SerialTimeoutExceptionz Write timeouts give an exceptionNr&rrrrr(^sr(z Write timeoutz)Attempting to use a port that is not openc@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|_||_|dur2|||_nd|_dS)z(Initialize a timeout with given durationNr) is_infiniteis_non_blockingdurationTIME target_timeselfr-rrr__init__{s   zTimeout.__init__cCs|jduo|dkS)z4Return a boolean, telling if the timeout has expiredNr)r/ time_leftr1rrrexpiredszTimeout.expiredcCsP|jr dS|jrdS|j|}||jkrB||j|_|jStd|SdS)z:Return how many seconds are left until the timeout expiresrN)r,r+r/r.r-max)r1Zdeltarrrr3s zTimeout.time_leftcCs||_|||_dS)zb Restart a timeout, only supported if a timeout was already set up before. N)r-r.r/r0rrrrestartszTimeout.restartN) rrrr'hasattrtimer*r.r2r5r3r7rrrrr)fs  r)c @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/dIdJZ0dxdLdMZ1dNdOZ2dPdQZ3dRdSZ4dydTdUZ5dzdWdXZ6d{dYdZZ7d[d\Z8d]d^Z9d_d`Z:dadbZ;dcddZe>jdjdiZ>dkdlZ?dmdnZ@dodpZAdqdrZBeCdfdsdtZDdudvZEdS)| SerialBasezh Serial port base class. Provides __init__ function and properties to get/set port settings. )2Kni,iXiii` i%iKiiiiii iii@Bii`ii%&i-ig5i =NrAFc Ks d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_||_||_||_||_||_||_| |_||_||_| |_| |_| |_d| vr| d|_d| vr| d|_| rt d!| |dur|"dS)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)r1rWrXrYrZr[r\r^r_r]r`rarbkwargsrrrr2sN   zSerialBase.__init__cCs|jS)z{ Get the current port setting. The value that was passed on init or using setPort() is passed back. )rGr4rrrrWszSerialBase.portcCsZ|dur$t|ts$tdt||j}|r6|||_||_|j|_ |rV| dS)z! Change the port. Nz'"port" must be None or a string, not {}) r basestringrdrtyperDcloserErGrFre)r1rWZwas_openrrrrWscCs|jS)z"Get the current baud rate setting.)rHr4rrrrXszSerialBase.baudratecCs\z t|}Wn ty,td|Yn,0|dkrDtd|||_|jrX|dS)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)intrrdrrHrD_reconfigure_port)r1rXr rrrrXs  cCs|jS)z"Get the current byte size setting.)rIr4rrrrY)szSerialBase.bytesizecCs0||jvrtd|||_|jr,|dS)zChange byte size.zNot a valid byte size: {!r}N) BYTESIZESrdrrIrDrk)r1rYrrrrY.s  cCs|jS)z)Get the current exclusive access setting.)rVr4rrrrb7szSerialBase.exclusivecCs||_|jr|dS)z$Change the exclusive access setting.N)rVrDrk)r1rbrrrrb<scCs|jS)zGet the current parity setting.)rJr4rrrrZCszSerialBase.paritycCs0||jvrtd|||_|jr,|dS)zChange parity setting.zNot a valid parity: {!r}N)PARITIESrdrrJrDrk)r1rZrrrrZHs  cCs|jS)z"Get the current stop bits setting.)rKr4rrrr[QszSerialBase.stopbitscCs0||jvrtd|||_|jr,|dS)zChange stop bits size.zNot a valid stop bit size: {!r}N)STOPBITSrdrrKrDrk)r1r[rrrr[Vs  cCs|jSz Get the current timeout setting.)rLr4rrrr\_szSerialBase.timeoutcCsd|durLz |dWn ty4td|Yn0|dkrLtd|||_|jr`|dS)Change timeout setting.NrNot a valid timeout: {!r}r)rrdrrLrDrkr1r\rrrr\ds  cCs|jSro)rMr4rrrr]rszSerialBase.write_timeoutcCsd|durL|dkrtd|z |dWn tyJtd|Yn0||_|jr`|dS)rpNrrqr)rdrrrMrDrkrrrrrr]ws  cCs|jS)z0Get the current inter-character timeout setting.)rQr4rrrraszSerialBase.inter_byte_timeoutcCsd|durL|dkrtd|z |dWn tyJtd|Yn0||_|jr`|dS)z"Change inter-byte timeout setting.Nrrqr)rdrrrQrDrk)r1Z ic_timeoutrrrras  cCs|jS)z!Get the current XON/XOFF setting.)rNr4rrrr^szSerialBase.xonxoffcCs||_|jr|dS)zChange XON/XOFF setting.N)rNrDrk)r1r^rrrr^scCs|jS)z-Get the current RTS/CTS flow control setting.)rOr4rrrr_szSerialBase.rtsctscCs||_|jr|dS)z$Change RTS/CTS flow control setting.N)rOrDrk)r1r_rrrr_scCs|jS)z-Get the current DSR/DTR flow control setting.)rPr4rrrr`szSerialBase.dsrdtrcCs*|dur|j|_n||_|jr&|dS)z#Change DsrDtr flow control setting.N)rOrPrDrk)r1r`rrrr`s  cCs|jSN)rSr4rrrrtsszSerialBase.rtscCs||_|jr|dSrs)rSrDZ_update_rts_stater1valuerrrrtscCs|jSrs)rTr4rrrdtrszSerialBase.dtrcCs||_|jr|dSrs)rTrDZ_update_dtr_staterurrrrwscCs|jSrs)rUr4rrrbreak_conditionszSerialBase.break_conditioncCs||_|jr|dSrs)rUrDZ_update_break_staterurrrrxscCs|jS)z Enable RS485 mode and apply new settings, set to None to disable. See serial.rs485.RS485Settings for more info about the value. )rRr4rrr rs485_modeszSerialBase.rs485_modecCs||_|jr|dSrs)rRrDrk)r1Zrs485_settingsrrrrys) rXrYrZr[r^r`r_r\r]racstfddjDS)zb Get current port settings as a dictionary. For use with apply_settings(). csg|]}|td|fqS)_)getattr).0keyr4rr z+SerialBase.get_settings..)dict_SAVED_SETTINGSr4rr4r get_settingsszSerialBase.get_settingscCs>|jD]2}||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. rzN)rr{setattr)r1dr}rrrapply_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}))rFidp)r __class__rrr4rrr__repr__ szSerialBase.__repr__cCsdSNTrr4rrrreadableszSerialBase.readablecCsdSrrr4rrrwritableszSerialBase.writablecCsdS)NFrr4rrrseekableszSerialBase.seekablec Cs||t|}t|}z||d|<WnPtyv}z8ddl}t||jsN||d||d|<WYd}~n d}~00|S)Nrr )readlenrarrayr )r1r datanerrrrrrreadinto s *zSerialBase.readintocCs|js||Srs)rDrer4rrr __enter__/szSerialBase.__enter__cOs |dSrs)ri)r1argsrfrrr__exit__4szSerialBase.__exit__?cCs$|js td|_t|d|_dS)za Send break condition. Timed, returns to idle state after given duration. TFN)rDportNotOpenErrorrxr9sleepr0rrr send_break9s  zSerialBase.send_breakcCs |dSrs)Zreset_input_bufferr4rrr flushInputGszSerialBase.flushInputcCs |dSrs)Zreset_output_bufferr4rrr flushOutputJszSerialBase.flushOutputcCs|jSrs) in_waitingr4rrr inWaitingMszSerialBase.inWaitingcCs||dSrs)rr0rrr sendBreakPszSerialBase.sendBreakrcCs ||_dSrs)rtrurrrsetRTSSszSerialBase.setRTScCs ||_dSrs)rwrurrrsetDTRVszSerialBase.setDTRcCs|jSrs)Zctsr4rrrgetCTSYszSerialBase.getCTScCs|jSrs)Zdsrr4rrrgetDSR\szSerialBase.getDSRcCs|jSrs)Zrir4rrrgetRI_szSerialBase.getRIcCs|jSrs)Zcdr4rrrgetCDbszSerialBase.getCDcCs ||_dSrs)rW)r1rWrrrsetPorteszSerialBase.setPortcCs|jSrsr]r4rrrrBhszSerialBase.writeTimeoutcCs ||_dSrsrrrrrrrBlscCs|jSrsrar4rrrrCpszSerialBase.interCharTimeoutcCs ||_dSrsr)r1rCrrrrCtscCs|Srs)rr4rrrgetSettingsDictxszSerialBase.getSettingsDictcCs||dSrs)r)r1rrrrapplySettingsDict{szSerialBase.applySettingsDictcCs|jSrs)rDr4rrrisOpen~szSerialBase.isOpencCs ||jS)zL Read all bytes currently available in the buffer of the OS. )rrr4rrrread_allszSerialBase.read_allcCspt|}t}t|j}|d}|rh||7}|| d|krBqh|dur\t||kr\qhnqh|rqhqt|S)z Read until a termination sequence is found (' ' by default), the size is exceeded or until timeout occurs. rN)rrr)rLrr5r)r1 terminatorsizeZlentermliner\crrr read_untils  zSerialBase.read_untilcos"|j|i|}|sq|VqdS)zs Read lines, implemented as generator. It will raise StopIteration on timeout (empty read). N)r)r1rrfrrrr iread_untilszSerialBase.iread_until)N)r)r)r)r)Frrrr' BAUDRATESFIVEBITSSIXBITS SEVENBITS EIGHTBITSrl PARITY_NONE PARITY_EVEN PARITY_ODD PARITY_MARK PARITY_SPACErm STOPBITS_ONESTOPBITS_ONE_POINT_FIVE STOPBITS_TWOrnr2propertyrWsetterrXrYrbrZr[r\r]rar^r_r`rtrwrxryrrrrrrrrrrrrrrrrrrrrrrrBrCrrrrLFrrrrrrr:s   J                                          r:__main__zport name: {} zbaud rates: {} zbyte sizes: {} zparities: {} zstop bits: {} z{} )/ior9r NameErrorAttributeErrorobjectrstrrgrrZXONZXOFFZCRrrrrrrrrrrrrrZ PARITY_NAMESIOErrorr%r(ZwriteTimeoutErrorrr) RawIOBaser:rsyssstdoutwriterrFrrlrmrnrrrr s^         ;