1_7ddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZmZGddeZdZej(j+Zedddk(r$ddlZdZd Zd Zd Zd Zd ZdZdZdZdZ GddeZ!nmedk(r GddeZ!n\edddk(rddlZdZ"GddeZ!nBedddk(sedddk(sedddk(sedddk(rGd d!eZ#Gd"deZ!n Gd#deZ!e$ed$d%Z%e$ed&d'Z&e$ed(d)Z'e$ed*d+Z(e$ed,dZ)e$ed-dZ*e$ed.d/Z+e$ed0d1Z,e$ed2d3Z-e$ed4d5Z.e$ed6e,Z/e$ed7e-Z0e1ed8r ejdZ2n e$ed9d:Z2e$ed;d<Z3ejhd=dZ5ejhd=e*Z6ejhd=e)Z7e$ed>d?Z8e$ed@dAZ9GdBdCe e!Z:GdDdEe:Z;GdFdGe:Z(Ff$FFg%F DGGW%8%8#> v !T![![\prs!tuu vsA;B C!B<<Cctjddgdz} tj|jt||dxxt j zcc<|dxxtzcc<|x|d<|d<tj|jt|y#t$r }tdj||d}~wwxYw)NrVr@r+r:r;z'Failed to set custom baud rate ({}): {}) rXrrrTCGETS2rYCBAUDBOTHERTCSETS2r\r]r^)rrr_r`s rrz&PlatformSpecific._set_special_baudrates++cA38,C ` DGGWc2A7==.(A& #++AR DGGWc2 ` !J!Q!QRZ\]!^__ `sBB C'CCctjddgdz} tj|jt||dxxt zcc<||j r|dxxtzcc<n|dxxtzcc<|jr|dxxtzcc<n|dxxtzcc<|jr|dxxtzcc<n|dxxtzcc<|jt|jdz|d<|j!t|jdz|d<nd|d<tj|jt|y#t $r}t#dj%|d}~wwxYw)NrVrr9r*r+zFailed to set RS485 mode: {})rXrrr TIOCGRS485SER_RS485_ENABLEDloopbackSER_RS485_RX_DURING_TXrts_level_for_txSER_RS485_RTS_ON_SENDrts_level_for_rxSER_RS485_RTS_AFTER_SENDdelay_before_txintdelay_before_rx TIOCSRS485r\r]r^)rrr_r`s rrz PlatformSpecific._set_rs485_modesU++cA37+C K DGGZ5A++!-%..A"88A#9"99%66A"77A#8"88%66A"::A#;";;%55A!$^%C%Cd%J!KA%55A!$^%C%Cd%J!KACF DGGZ5 K !?!F!Fq!IJJ KsEE F'FFN)r#r$r%r&rrrr'rrr/r/_s  X  X   X   X  X  X   X   X   X   X   X   X   X   X   X  X!  " X#  $?  D v$ `  Krr/cygwinc (eZdZddddddddd d d d Zy )r/rArBrCrDrErFrGrHrIrJrK) iirLrMrNrOrPrQrRrSrTN)r#r$r%r&r'rrr/r/s+  rr7darwinlTceZdZejdj dZdZdZe eddk\rdZ dZ y ) r/r+.{t zt rr9c~tjd|g}tj|jt|dy)NrVr*)rXrrr IOSSIOSPEED)rrr_s rrz&PlatformSpecific._set_special_baudrates*kk#z2 DGG[#q9rc|jr/tj|jtj ytj|jtj yz[ Set break: Controls TXD. When active, no transmitting is possible. Nrrrrr/rr r!s rr"z$PlatformSpecific._update_break_state?   DGG%5%>%>? DGG%5%>%>?rN) r#r$r%osunamesplit osx_versionrr rrrr"r'rrr/r/sFbhhjm))#.  {1~ ! # :  @rr3bsdr8freebsdnetbsdopenbsdceZdZdZy)ReturnBaudratec|S)Nr')rkeys r __getitem__zReturnBaudrate.__getitem__sJrN)r#r$r%rr'rrrrs rrc(eZdZeZdZdZdZy)r/rzr{c|jr/tj|jtj ytj|jtj yrrr!s rr"z$PlatformSpecific._update_break_staterrN)r#r$r%rr&rr r"r'rrr/r/s,- @rc eZdZy)r/N)r#r$r%r'rrr/r/ s rTIOCMGETiTTIOCMBISiTTIOCMBICiTTIOCMSETiT TIOCM_DTR TIOCM_RTS TIOCM_CTSrW TIOCM_CARrb TIOCM_RNG TIOCM_DSRTIOCM_CDTIOCM_RITIOCINQFIONREADiTTIOCOUTQiTIri'Tr i(TceZdZdZdZddZdZedZddZ dZ dZ d Z d Z d Zd Zd ZddZdZdZedZedZedZedZedZdZddZddZdZy)Serialz Serial port class POSIX implementation. Serial port configuration is done with termios and fcntl. Runs on Linux and many other Un*x like systems. c|j td|jr tdd|_ t j |j tjtjztjz|_d\|_ |_d\|_|_ |j#d |j$s|j'|j(s|j+|j3t j4\|_ |_t j4\|_|_t7j6|jt6j8tjt7j6|jt6j8tjd|_y#t$r<}d|_t|jdj|j|d}~wwxYw#t,$r8}|jtj.tj0fvrYd}~Xd}~wwxYw#t:$r t j<|jn#t>$rYnwxYwd|_|j&t j<|jd|_|j&t j<|jd|_ |j &t j<|j d|_|j&t j<|jd|_wxYw)zo Open port with current settings. This may throw a SerialException if the port cannot be opened.Nz.Port must be configured before it can be used.zPort is already open.zcould not open port {}: {}NNT) force_update) _portris_openrropenportstrO_RDWRO_NOCTTY O_NONBLOCKOSErrorerrnor^pipe_abort_read_rpipe_abort_read_wpipe_abort_write_rpipe_abort_write_w_reconfigure_port_dsrdtr_update_dtr_state_rtscts_update_rts_stater\EINVALENOTTY_reset_input_bufferpiperF_SETFL BaseExceptionclose Exception)rmsgr`s rrz Serial.open7s :: !"RS S <<!"9: : cggdllBII ,Cbmm,STDG :D6 6;E8!8)   " " " 5 ||**,||**,  $ $ &=?WWY :D "D$:?Awwy >?   !  DG%%1//0)-&%%1//0)-&&&2001*.'&&2001*.' - stAG +I>8H6C I H7H  H I-IIII M!!JM! J  M! J  CM!c |j td|j{|jrA tj|jtj tj zn.tj|jtjd}dx}}|jd}t|jdz} tj|j}|\}}} } } } } | tj"tj$zz} | tj&tj(ztj*ztj,ztj.ztj0ztj2zz} dD]'}t5t|s| t7t|z} )|tj8tj:ztj<zz}|tj>tj@ztjBztjDzz}t5td r|tjFz}t5td r|tjHz} t7td j|jJx} } | tjXz} |jZdk(r| tj\z} n|jZdk(r| tj^z} nj|jZdk(r| tj`z} nG|jZdk(r| tjbz} n$tWdj|jZ|jdtfjhk(r| tjjz} n|jdtfjlk(r| tjjz} nU|jdtfjnk(r| tjjz} n$tWdj|jd|tjptjrzz}|jttfjvk(r.| tjxtjzzt|zz} n^|jttfj~k(r0| tjzt|zz} | tjxz} n|jttfjk(r/| t|z} | tjxtjzzz} n|jttfjk(r2t|r,| tjxt|ztjzzz} nv|jttfjk(r5t|r/| tjxt|zz} | tjzz} n$tWdj|jtt5tdrh|jr%|tjtjzz}n|tjtjztjzz}nV|jr%|tjtjzz}n%|tjtjzz}t5tdr5|jr| tjz} nY| tjz} nDt5tdr4|jr| tjz} n| tjz} |dks|dkDrtWdj||| tj<|dks|dkDrtWdj||| tj<|s ||| | | | | g|k7r6tj|jtj||| | | | | g||j||j|j|jyy#t$r5}t|jdj|j|d}~wwxYw#tj $r}tdj|d}~wwxYw#tL$r |jN|jJx} } n#tP$r tRx} } n!#tT$rt7td x} } YnwxYw t|jJ}|dkrUtWd j|jJ#tV$r%tWd j|jJwxYwYnwxYwYwxYw),Set communication parameters on opened port.Nz+Can only operate on a valid file descriptorz&Could not exclusively lock port {}: {}rr*r;Could not configure port: {})ECHOCTLECHOKEIUCLCPARMRKzB{}B38400zInvalid baud rate: {!r}r9r8r7r(zInvalid char len: {!r}z$Invalid stop bit specification: {!r}zInvalid parity: {!r}IXANYCRTSCTS CNEW_RTSCTSzInvalid vmin: {!r}Invalid vtime: {!r})Qrr _exclusiverflockLOCK_EXLOCK_NBr\rr^rLOCK_UN_inter_byte_timeoutrrrY tcgetattrerrorCLOCALCREADICANONECHOECHOEECHOKECHONLISIGIEXTENhasattrgetattrOPOSTONLCROCRNLINLCRIGNCRICRNLIGNBRKrr _baudrateAttributeErrorr&KeyErrorre NameErrorr]CSIZE _bytesizeCS8CS7CS6CS5 _stopbitsserial STOPBITS_ONECSTOPBSTOPBITS_ONE_POINT_FIVE STOPBITS_TWOINPCKISTRIP_parity PARITY_NONEPARENBPARODDCMSPAR PARITY_EVEN PARITY_ODD PARITY_MARK PARITY_SPACE_xonxoffIXONIXOFFrrrrVMINVTIME tcsetattrTCSANOWr _rs485_moder)rrr custom_baudvminvtime orig_attriflagoflagcflaglflagispeedospeedccflags rrzSerial._reconfigure_portxsy 77?!"OP P ?? &wKK)FG DGGU]]3 u  # # /D00256E N))$''2I=F :E5% '..7==01 7>>GLL07==@==!#*>>2<< ")..12 2* 1Dw%''4000 1 7==7==07==@AA 7==7==07==@7>>QRR 7G $ gmm^ #E 7H % gnn_ $E [%gu||DNN/KL LFV0 '-- >>Q  W[[ E ^^q W[[ E ^^q W[[ E ^^q W[[ E5<>V00 0 w~~& &E ^^v== = gnn %E ^^v22 2 gnn %ECJJ4>>Z[ [ 7==7>>122 <<6-- - w~~6?@ @E \\V// / w~~./ /E gnn %E \\V.. . fW E gnnw~~5 6E \\V// /F gnnv-> ?E \\V00 0V gnnv- .E w~~& &E3::4<<HI I 7G $}}',,677<<'--7'--GHH}}',,677<<'--788 7I &||'//*7??++ Wm ,||'--.7..// !8tcz188>? ?7<< 19 299%@A A!7== E5%KyX   ueVVR@ B  "  & &{ 3    '  !1!1 2 (Yw)#))5]5d5deieoeoqt5uvvw}} N!"@"G"G"LM M N* [ ["&"9"9$.."II [ A&,,FV A&-gx&@@FVA["%dnn"5K#Q()B)I)I$..)YZZ "W$%>%E%Ednn%UVVW'! [ [s?`? )b+b5? a=0a88a=b2b--b25 f ?cf  f%c.-f.d  f d  fe%)f.e<<f?f ff f c|jr|jtj|jd|_tj|jtj|j tj|j tj|jd\|_|_d\|_|_d|_yy)z Close portNrF)rrrrrrrrr!s rrz Serial.closes <<ww"!//0//0001001AK>&(>CM@')@ DL rctj|jtt}t j d|dS)z9Return the number of bytes currently in the input buffer.rr)rrrrTIOCM_zero_strstructunpackrss r in_waitingzSerial.in_waiting!s0 KK. 9}}S!$Q''rc|js tt}t|j}t ||kr t j |j|jggg|j\}}}|j|vr,tj|jd t1|S|s t1|Stj|j|t |z }|s td|j||j/r t1|St ||krt1|S#t$r}}|jtj tj"tj$tj&tj(fvrtdj+|Yd}~d}~wt j,$rw}|dtj tj"tj$tj&tj(fvrtdj+|Yd}~?d}~wwxYw) 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. rhzgdevice reports readiness to read but returned no data (device disconnected or multiple access on port?)zread failed: {}Nr)rr bytearrayr _timeoutlenselectrr time_leftrreadrextendrrEAGAINEALREADY EWOULDBLOCK EINPROGRESSEINTRr^rexpiredbytes)rsizer)timeoutready_r_r`s rr)z Serial.read)s ||"$ ${$--($i$" !$mmTWWd6L6L,MrSUW^WhWhWjk q!))U2GGD22D9DT{;8T{7ggdggtc$i'78 *LMM C  T{O$i$NT{5 G775<<ARARTYTeTeglgrgr"ss)*;*B*B1*EFFt<< GQ4 enne>O>OQVQbQbdidodopp)*;*B*B1*EFFq  Gs3A.E ?E ,E IA3GI$A,IIc^|jr!tj|jdyyNx)rrwriterr!s r cancel_readzSerial.cancel_read\s" << HHT++T 2 rc^|jr!tj|jdyyr7)rrr9rr!s r cancel_writezSerial.cancel_write`s" << HHT,,d 3 rc|js tt|}t|x}}t |j }|dkDrr t j|j|}|jr|S|js|jr tdtj|jg|jgg|j\}}} |r!t j |jdn|std|jJtj|jg|jggd\}}} |r!t j |jdnE|s t#d||d}||z}|js|jr td|dkDrr|t|z S#t"$rt$$r}} | j&t&j(t&j*t&j,t&j.t&j0fvrt#dj3| Yd} ~ d} ~ wtj4$rw} | dt&j(t&j*t&j,t&j.t&j0fvrt#dj3| Yd} ~ Nd} ~ wwxYw)z2Output the given byte string over the serial port.rz Write timeoutrhNr*zwrite failed (select)zwrite failed: {})rrrr&r _write_timeoutrr9ris_non_blocking is_infiniter0rr'rr(r)rrrr+r,r-r.r/r^r) rdatadtx_lenlengthr3nabortr4r5r`s rr9z Serial.writedst||"$ $ TNa& $--.qj) HHHTWWa(**H ,,(4_EE&,mmT5L5L4MPTPWPWyZ\^e^o^o^q&rOE5! 7 7> 4_EE",,.666&,mmT5L5L4MPTPWPWyZ\^b&cOE5! 7 7; -.EFFabE! **w/@,_==YqjZA!#  H775<<ARARTYTeTeglgrgr"ss)*<*C*CA*FGGt<< HQ4 enne>O>OQVQbQbdidodopp)*<*C*CA*FGGq  Hs: -G 9B G A3G 7G K!A3IK!*A,KK!cn|js ttj|jy)zb Flush of file like objects. In this case, wait until all data is written. N)rrrYtcdrainrr!s rflushz Serial.flushs$ ||"$ $ rc`tj|jtjyz9Clear input buffer, discarding all that is in the buffer.N)rYtcflushrTCIFLUSHr!s rrzSerial._reset_input_buffers!1!12rcP|js t|jyrK)rrrr!s rreset_input_bufferzSerial.reset_input_buffers||"$ $   "rc|js ttj|jtj y)zs Clear output buffer, aborting the current output and discarding all that is in the buffer. N)rrrYrLrTCOFLUSHr!s rreset_output_bufferzSerial.reset_output_buffers, ||"$ $!1!12rc|js ttj|jt |dz y)za Send break condition. Timed, returns to idle state after given duration. ?N)rrrY tcsendbreakrrr)rdurations r send_breakzSerial.send_breaks1 ||"$ $DGGSD%9:rc|jr*tj|jtt ytj|jt t y)z)Set terminal status line: Request To SendN) _rts_staterrrr TIOCM_RTS_strrr!s rrzSerial._update_rts_state1 ?? KK= 9 KK= 9rc|jr*tj|jtt ytj|jt t y)z-Set terminal status line: Data Terminal ReadyN) _dtr_staterrrr TIOCM_DTR_strrr!s rrzSerial._update_dtr_stater[rc|js ttj|jt t }tjd|dtzdk7S)z(Read terminal status line: Clear To Sendrr) rrrrrrrrrrrs rctsz Serial.ctsJ||"$ $ KK> :}}S!$Q')3q88rc|js ttj|jt t }tjd|dtzdk7S)z)Read terminal status line: Data Set Readyrr) rrrrrrrrrrrs rdsrz Serial.dsrrarc|js ttj|jt t }tjd|dtzdk7S)z)Read terminal status line: Ring Indicatorrr) rrrrrrrrrrrs rriz Serial.riJ||"$ $ KK> :}}S!$Q'(2a77rc|js ttj|jt t }tjd|dtzdk7S)z)Read terminal status line: Carrier Detectrr) rrrrrrrrrrrs rcdz Serial.cdrfrctj|jtt}t j d|dS)z:Return the number of bytes currently in the output buffer.rr)rrrrrrrrs r out_waitingzSerial.out_waitings0 KK> :}}S!$Q''rcF|js t|jS)z For easier use of the serial port instance with select. WARNING: this function is not portable to different platforms! )rrrr!s rfilenoz Serial.filenos ||"$ $wwrc|js t|r/tj|jtj ytj|jtj y)z Manually control flow - when software flow control is enabled. This will send XON (true) or XOFF (false) to the other device. WARNING: this function is not portable to different platforms! N)rrrYtcflowrTCIONTCIOFFrenables rset_input_flow_controlzSerial.set_input_flow_controlC ||"$ $  NN477GMM 2 NN477GNN 3rc|js t|r/tj|jtj ytj|jtj y)z Manually control flow of outgoing data - when hardware or software flow control is enabled. WARNING: this function is not portable to different platforms! N)rrrYrnrTCOONTCOOFFrqs rset_output_flow_controlzSerial.set_output_flow_control rtrc8ddl}|jdty)zDEPRECATED - has no userNz0nonblocking() has no effect, already nonblocking)warningswarnDeprecationWarning)rrzs r nonblockingzSerial.nonblockings HJ\]rN)Fr*)rTT)r#r$r%__doc__rrrpropertyr!r)r:r<r9rIrrOrRrWrrr`rcrerhrjrlrsrxr}r'rrrr0s ?BW3r ! ((1f344l!3# 3;::99998888((  4 4^rrceZdZdZddZy)PosixPollSerialz Poll based read implementation. Not all systems support poll properly. However this one has better handling of errors, such as a device disconnecting while it's in use (e.g. USB-serial unplugged). c|js tt}t|j}t j }|j|jt jt jzt jzt jz|j|jt jt jzt jzt jz|dkDrYt||krJ|j |jrdn|j!dzD]V\}}||jk(rnB|t jt jzt jzzsMt#d|jk(r,t%j&|jd t/|St%j&|j|t|z }|j)||j+s|j,|j,dkDr|s t/|St||krJt/|S)r#rNrhzdevice reports error (poll))rrr$r r%r'pollregisterrPOLLINPOLLERRPOLLHUPPOLLNVALrr&r@r(rrr)r*r0rr1)rr2r)r3rreventr_s rr)zPosixPollSerial.read&s ||"$ ${$--({{} dggv}}v~~=NQWQ`Q``a d,,fmmfnn.Lv~~.]`f`o`o.op !8d)d""&73F3F4WM^M^M`cgMg!iMIBT333 ?&// QR-.KLL M///GGD22D9 T{ ggdggtc$i'78 C ??$ 44@TE]E]`aEaknT{%d)d"$T{rNr~)r#r$r%rr)r'rrrrs  rrc:eZdZdZdfd ZddZeZxZS) VTIMESerialab Implement timeout using vtime of tty device instead of using select. This means that no inter character timeout can be specified and that the error handling is degraded. Overall timeout is disabled when inter-character timeout is used. Note that this implementation does NOT support cancel_read(), it will just ignore that. c tt| tj|jtj d|j d}t|j dz}n+|jd}d}nd}t|jdz} tj|j}|\}}}}} } } |dks|dkDrtdj||| tj <|| tj"<tj$|jtj&||||| | | gy#tj$r)} tjdj| d} ~ wwxYw)rrNr*r;rrr)superrrrrrrrrr%rYrrrrr^r]r r r r )rrrrrrrrrrrrr __class__s rrzVTIMESerial._reconfigure_portUsN k424 DGGU]]A.  # # /D00256E ]] "DED *+E U))$''2I=F :E5% 19 299%@A A!7==7<<ueVVR@ B}} U(()G)N)Ns)ST T Us)EE<$E77E<c(|js tt}t||krZt j |j |t|z }|s t|S|j|t||krZt|S)r#) rrr$r&rr)rr*r1)rr2r)r_s rr)zVTIMESerial.readss| ||"$ ${$i$''$''4#d)#34CT{ KK  $i$ T{rrr~) r#r$r%rrr)rr: __classcell__)rs@rrrIs B<"*Krr)= __future__rrrrr'rsysrYrserial.serialutilrrrrrr objectr rplatformlowerplatrXrcrfrerirtrjrnrprlr/r}rrrrrrrrrrrrrrrrrpackrrZr^rr rrrr'rrrs:'  66+6+0   ||8wFGG FJJ"&)'_K/_KD X  /   "1XK@/@* "1X "1X "1X "1X@/@( / 7J / 7J / 7J / 7J / G[% 0 G[% 0  G[% 0 G[% 0 G[% 0 G[% 0 7J 2 7J 2 7IooGgz62G 7J /S!$ C+  C+ 7J / 7J /l^Z)l^^'f'T;&;r