dbfdg 3 M]WJ"@sddlZddlZyeWn(eefk r@GdddeZYnXyeWneefk rfeZYnXyeWneefk refZYnXddZ ddZ e dgZ e d gZ e d gZ e d gZd,\ZZZZZd-\ZZZd.\ZZZZededededediZGdddeZGdd d eZed!Zed"Z Gd#d$d$ej!Z"e#d%krddl$Z$e"Z%e$j&j'd&j(e%j)e$j&j'd'j(e%j*e$j&j'd(j(e%j+e$j&j'd)j(e%j,e$j&j'd*j(e%j-e$j&j'd+j(e%dS)/Nc@s eZdZdS) memoryviewN)__name__ __module__ __qualname__rr /usr/lib/python3.6/serialutil.pyrsrccsFt|tr|j}d}x*|||d}|d7}|r<|VqPqWdS)z=Iterate over bytes, returning bytes instead of ints (python3)rN) isinstancertobytes)biarrr iterbytes&s rcCst|tr|St|tr t|St|tr2|jSt|trLtdj|n8t}x(|D] }t|trnt|}|j |qXWt|SdS)z"convert a sequence to a bytes typez?unicode strings are not supported, please encode to bytes: {!r}N) r bytes bytearrayrr unicode TypeErrorformatordappend)seqr itemrrrto_bytes6s      r NEOMSr?NoneZEvenZOddZMarkZSpacec@seZdZdZdS)SerialExceptionz.Base class for serial port related exceptions.N)rrr__doc__rrrrr)_sr)c@seZdZdZdS)SerialTimeoutExceptionz Write timeouts give an exceptionN)rrrr*rrrrr+csr+z Write timeoutz)Attempting to use a port that is not openc@seZdZdZdZeeeefZ e e e e efZeeefZd dee ed d!d!d d!d f d"d#Zed$d%Zejd&d%Zed'd(Zejd)d(Zed*d+Zejd,d+Zed-d.Zejd/d.Zed0d1Zejd2d1Zed3d4Zejd5d4Zed6d7Zejd8d7Zed9d:Zejd;d:Zedd=Zed?d@Z e jdAd@Z edBdCZ!e!jddDdCZ!edEdFZ"e"jdGdFZ"edHdIZ#e#jdJdIZ#edKdLZ$e$jdMdLZ$edNdOZ%e%jdPdOZ%dZ&d[d\Z'd]d^Z(d_d`Z)dadbZ*dcddZ+dedfZ,dgdhZ-didjZ.dkdlZ/ddndoZ0dpdqZ1drdsZ2dtduZ3ddvdwZ4ddydzZ5dd{d|Z6d}d~Z7ddZ8ddZ9ddZ:ddZ;eddZddZ?ddZ@ddZAeBd fddZCddZDd S) SerialBasezh Serial port base class. Provides __init__ function and properties to get/set port settings. 2Kn,X` %K @B`逄%&-g5 =NFc Ksd|_d|_d|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_d|_d|_d|_||_||_||_||_||_||_| |_||_||_| |_| |_d| kr| jd|_d| kr| jd|_| rtdj| |dk r|j 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_stateportbaudratebytesizeparitystopbitstimeout write_timeoutxonxoffrtsctsdsrdtrinter_byte_timeoutpop ValueErrorropen) selfr_r`rarbrcrdrfrgrerhrikwargsrrr__init__zsJ  zSerialBase.__init__cCs|jS)z{ Get the current port setting. The value that was passed on init or using setPort() is passed back. )rP)rmrrrr_szSerialBase.portcCs\|dk r&t|t r&tdjt||j}|r8|j||_||_|j|_ |rX|j dS)z! Change the port. Nz'"port" must be None or a string, not {}) r basestringrkrtyperMcloserNrPrOrl)rmr_Zwas_openrrrr_scCs|jS)z"Get the current baud rate setting.)rQ)rmrrrr`szSerialBase.baudratec Cs^y t|}Wn"tk r.tdj|Yn,X|dkrFtdj|||_|jrZ|jdS)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)intrrkrrQrM_reconfigure_port)rmr`r rrrr`s cCs|jS)z"Get the current byte size setting.)rR)rmrrrraszSerialBase.bytesizecCs0||jkrtdj|||_|jr,|jdS)zChange byte size.zNot a valid byte size: {!r}N) BYTESIZESrkrrRrMrt)rmrarrrras  cCs|jS)zGet the current parity setting.)rS)rmrrrrbszSerialBase.paritycCs0||jkrtdj|||_|jr,|jdS)zChange parity setting.zNot a valid parity: {!r}N)PARITIESrkrrSrMrt)rmrbrrrrbs  cCs|jS)z"Get the current stop bits setting.)rT)rmrrrrc szSerialBase.stopbitscCs0||jkrtdj|||_|jr,|jdS)zChange stop bits size.zNot a valid stop bit size: {!r}N)STOPBITSrkrrTrMrt)rmrcrrrrcs  cCs|jS)z Get the current timeout setting.)rU)rmrrrrdszSerialBase.timeoutc Csf|dk rNy |dWn"tk r6tdj|YnX|dkrNtdj|||_|jrb|jdS)zChange timeout setting.NrzNot a valid timeout: {!r}r)rrkrrUrMrt)rmrdrrrrds cCs|jS)z Get the current timeout setting.)rV)rmrrrre,szSerialBase.write_timeoutc Csf|dk rN|dkrtdj|y |dWn"tk rLtdj|YnX||_|jrb|jdS)zChange timeout setting.NrzNot a valid timeout: {!r}r)rkrrrVrMrt)rmrdrrrre1s cCs|jS)z0Get the current inter-character timeout setting.)rZ)rmrrrri@szSerialBase.inter_byte_timeoutc Csf|dk rN|dkrtdj|y |dWn"tk rLtdj|YnX||_|jrb|jdS)z"Change inter-byte timeout setting.NrzNot a valid timeout: {!r}r)rkrrrZrMrt)rmZ ic_timeoutrrrriEs cCs|jS)z!Get the current XON/XOFF setting.)rW)rmrrrrfTszSerialBase.xonxoffcCs||_|jr|jdS)zChange XON/XOFF setting.N)rWrMrt)rmrfrrrrfYscCs|jS)z-Get the current RTS/CTS flow control setting.)rX)rmrrrrg`szSerialBase.rtsctscCs||_|jr|jdS)z$Change RTS/CTS flow control setting.N)rXrMrt)rmrgrrrrgescCs|jS)z-Get the current DSR/DTR flow control setting.)rY)rmrrrrhlszSerialBase.dsrdtrcCs*|dkr|j|_n||_|jr&|jdS)z#Change DsrDtr flow control setting.N)rXrYrMrt)rmrhrrrrhqs  cCs|jS)N)r\)rmrrrrts}szSerialBase.rtscCs||_|jr|jdS)N)r\rMZ_update_rts_state)rmvaluerrrrxscCs|jS)N)r])rmrrrdtrszSerialBase.dtrcCs||_|jr|jdS)N)r]rMZ_update_dtr_state)rmryrrrrzscCs|jS)N)r^)rmrrrbreak_conditionszSerialBase.break_conditioncCs||_|jr|jdS)N)r^rMZ_update_break_state)rmryrrrr{scCs|jS)z Enable RS485 mode and apply new settings, set to None to disable. See serial.rs485.RS485Settings for more info about the value. )r[)rmrrr rs485_modeszSerialBase.rs485_modecCs||_|jr|jdS)N)r[rMrt)rmZrs485_settingsrrrr|sr`rarbrcrfrhrgrdrericstfddjDS)zb Get current port settings as a dictionary. For use with apply_settings(). csg|]}|td|fqS)_)getattr).0key)rmrr sz+SerialBase.get_settings..)dict_SAVED_SETTINGS)rmr)rmr get_settingsszSerialBase.get_settingscCsBx<|jD]2}||kr||t|d|krt||||qWdS)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. r}N)rr~setattr)rmdrrrrapply_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}))rOidp)r __class__rr)rmrrr__repr__szSerialBase.__repr__cCsdS)NTr)rmrrrreadableszSerialBase.readablecCsdS)NTr)rmrrrwritableszSerialBase.writablecCsdS)NFr)rmrrrseekableszSerialBase.seekablecCsz|jt|}t|}y||d|<WnNtk rt}z2ddl}t||jsP||jd||d|<WYdd}~XnX|S)Nrr )readlenrarrayr )rmr datanerrrrrrreadintos &zSerialBase.readintocCs|S)Nr)rmrrr __enter__szSerialBase.__enter__cOs |jdS)N)rr)rmargsrnrrr__exit__szSerialBase.__exit__?cCs$|js td|_tj|d|_dS)za Send break condition. Timed, returns to idle state after given duration. TFN)rMportNotOpenErrorr{timeZsleep)rmdurationrrr send_breaks  zSerialBase.send_breakcCs |jdS)N)Zreset_input_buffer)rmrrr flushInputszSerialBase.flushInputcCs |jdS)N)Zreset_output_buffer)rmrrr flushOutputszSerialBase.flushOutputcCs|jS)N) in_waiting)rmrrr inWaitingszSerialBase.inWaitingcCs|j|dS)N)r)rmrrrr sendBreakszSerialBase.sendBreakrcCs ||_dS)N)rx)rmryrrrsetRTS szSerialBase.setRTScCs ||_dS)N)rz)rmryrrrsetDTRszSerialBase.setDTRcCs|jS)N)Zcts)rmrrrgetCTSszSerialBase.getCTScCs|jS)N)Zdsr)rmrrrgetDSRszSerialBase.getDSRcCs|jS)N)Zri)rmrrrgetRIszSerialBase.getRIcCs|jS)N)Zcd)rmrrrgetCDszSerialBase.getCDcCs ||_dS)N)r_)rmr_rrrsetPortszSerialBase.setPortcCs|jS)N)re)rmrrrrK szSerialBase.writeTimeoutcCs ||_dS)N)re)rmrdrrrrK$scCs|jS)N)ri)rmrrrrL(szSerialBase.interCharTimeoutcCs ||_dS)N)ri)rmrLrrrrL,scCs|jS)N)r)rmrrrgetSettingsDict0szSerialBase.getSettingsDictcCs|j|dS)N)r)rmrrrrapplySettingsDict3szSerialBase.applySettingsDictcCs|jS)N)rM)rmrrrisOpen6szSerialBase.isOpencCs |j|jS)zL Read all bytes currently available in the buffer of the OS. )rr)rmrrrread_all<szSerialBase.read_allcCs`t|}t}xH|jd}|rR||7}|| d|kr:P|dk rTt||krTPqPqWt|S)z Read until a termination sequence is found (' ' by default), the size is exceeded or until timeout occurs. rN)rrrr)rm terminatorsizeZlentermlinecrrr read_untilBs zSerialBase.read_untilcos"x|j||}|sP|VqWdS)zs Read lines, implemented as generator. It will raise StopIteration on timeout (empty read). N)r)rmrrnrrrr iread_untilUs  zSerialBase.iread_until)r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrCrDrErFrGrHrIrJ)N) r`rarbrcrfrhrgrdreri)r)r)r)r)Errrr* BAUDRATESFIVEBITSSIXBITS SEVENBITS EIGHTBITSru PARITY_NONE PARITY_EVEN PARITY_ODD PARITY_MARK PARITY_SPACErv STOPBITS_ONESTOPBITS_ONE_POINT_FIVE STOPBITS_TWOrwropropertyr_setterr`rarbrcrdrerirfrgrhrxrzr{r|rrrrrrrrrrrrrrrrrrrrrrrKrLrrrrLFrrrrrrr,ks   ;                       r,__main__zport name: {} zbaud rates: {} zbyte sizes: {} zparities: {} zstop bits: {} z{} )rrrr r!)rr"r#)r$r%r&r').iorr NameErrorAttributeErrorobjectrstrrprrZXONZXOFFZCRrrrrrrrrrrrrrZ PARITY_NAMESIOErrorr)r+ZwriteTimeoutErrorr RawIOBaser,rsyssstdoutwriterrOrrurvrwrrrr sX        z