3 MÊ]W@#ã @s¨ddlZddlZddlZy ddlZWnek rBddljZYnXdd„Zdd„ZGdd„de ƒZ Gdd „d e ƒZ Gd d „d ej ƒZ e d kr¤e dƒZd e_eeƒdS)éNccs´d}xrtj|ƒD]d}djt|ƒƒd|ko2dknrB|jdƒndfV|d7}|dkrbdVq|d krdVd}qW|dkr°x(|d kr¨|d7}|dkr dVdVq‚WdVd S)z• yield tuples of hex and ASCII display in multiples of 16. Includes a space after 8 bytes and (None, None) after 16 bytes and at the end. rz{:02X} ó óÚasciiÚ.ééú ÚéNú )rr )NN)rr )r r)NN)ÚserialZ iterbytesÚformatÚordÚdecode)ÚdataÚnÚb©rú"/usr/lib/python3.6/protocol_spy.pyÚsixteen"s 6  rccs€g}g}d}xnt|ƒD]b\}}|dkrd|djdj|ƒdj|ƒgƒfV|dd…=|dd…=|d7}q|j|ƒ|j|ƒqWdS)z yield lines with hexdump of datarNrr r )rÚjoinÚappend)rÚvaluesrÚoffsetÚhÚarrrÚhexdump9s     rc@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú FormatRawz&Forward only RX and TX data to output.cCs||_||_d|_d|_dS)Nzz)ÚoutputÚcolorÚrx_colorÚtx_color)ÚselfrrrrrÚ__init__LszFormatRaw.__init__cCs.|jr|jj|jƒ|jj|ƒ|jjƒdS)zshow received dataN)rrÚwriter Úflush)r"rrrrÚrxRs z FormatRaw.rxcCs.|jr|jj|jƒ|jj|ƒ|jjƒdS)zshow transmitted dataN)rrr$r!r%)r"rrrrÚtxYs z FormatRaw.txcCsdS)z(do not) show control callsNr)r"ÚnameÚvaluerrrÚcontrol`szFormatRaw.controlN)Ú__name__Ú __module__Ú __qualname__Ú__doc__r#r&r'r*rrrrrIs rc@s:eZdZdZdd„Zddd„Zdd„Zd d „Zd d „Zd S)Ú FormatHexdumpax Create a hex dump of RX ad TX data, show when control lines are read or written. output example:: 000000.000 Q-RX flushInput 000002.469 RTS inactive 000002.773 RTS active 000003.001 TX 48 45 4C 4C 4F HELLO 000003.102 RX 48 45 4C 4C 4F HELLO cCs,tjƒ|_||_||_d|_d|_d|_dS)Nzzz)ÚtimeÚ start_timerrr r!Ú control_color)r"rrrrrr#ts  zFormatHexdump.__init__r cCs&|jjdj||||ƒƒ|jjƒdS)Nz{:010.3f} {:4} {}{} )rr$r r%)r"Z timestampZlabelr)Zvalue2rrrÚ write_line|szFormatHexdump.write_linecCsl|jr|jj|jƒ|rPxNt|ƒD](\}}|jtjƒ|jddj|ƒ|ƒq"Wn|jtjƒ|jddƒdS)zshow received data as hex dumpZRXz{:04X} zN) rrr$r rr3r0r1r )r"rrÚrowrrrr&€s &zFormatHexdump.rxcCsN|jr|jj|jƒx4t|ƒD](\}}|jtjƒ|jddj|ƒ|ƒqWdS)z!show transmitted data as hex dumpZTXz{:04X} N) rrr$r!rr3r0r1r )r"rrr4rrrr'ŠszFormatHexdump.txcCs0|jr|jj|jƒ|jtjƒ|j||ƒdS)zshow control callsN)rrr$r2r3r0r1)r"r(r)rrrr*‘szFormatHexdump.controlN)r ) r+r,r-r.r#r3r&r'r*rrrrr/es    r/csTeZdZdZ‡fdd„Zejjjdd„ƒZdd„Z ‡fdd „Z d,‡fd d „ Z e ejd ƒrd‡fdd„Z e ejdƒr|‡fdd„Ze‡fdd„ƒZ‡fdd„Z‡fdd„Z‡fdd„Zd-‡fdd„ Zejjjdd„ƒZejjjd d!„ƒZejjjd"d#„ƒZejjj‡fd$d%„ƒZejjj‡fd&d'„ƒZejjj‡fd(d)„ƒZejjj‡fd*d+„ƒZ‡ZS).ÚSerialzc Inherit the native Serial port implementation and wrap all the methods and attributes. cs"tt|ƒj||Žd|_d|_dS)NF)Úsuperr5r#Ú formatterÚshow_all)r"ÚargsÚkwargs)Ú __class__rrr#ŸszSerial.__init__cCs"|dk rtjjj||j|ƒƒdS)N)r r5ÚportÚ__set__Úfrom_url)r"r)rrrr<¤sz Serial.portc Csütj|ƒ}|jdkr&tjdj|jƒƒ‚t}d}tj}yvxptj |j dƒj ƒD]Z\}}|dkrjt |ddƒ}qJ|dkrxd}qJ|d kr†t }qJ|d kr–d|_qJtd j|ƒƒ‚qJWWn2tk rÜ}ztjd j|ƒƒ‚WYd d }~XnX|||ƒ|_dj|j|jgƒS)z(extract host and port from an URL stringZspyzmexpected a string in the form "spy://port[?option[=value][&option[=value]]]": not starting with spy:// ({!r})FTÚfilerÚwrÚrawÚallzunknown option: {!r}zPexpected a string in the form "spy://port[?option[=value][&option[=value]]]": {}Nr )ÚurlparseZurlsplitÚschemer ZSerialExceptionr r/ÚsysÚstderrZparse_qsZqueryÚitemsÚopenrr8Ú ValueErrorr7rZnetlocÚpath) r"ZurlÚpartsr7rrZoptionrÚerrrr>©s2    zSerial.from_urlcs|jj|ƒtt|ƒj|ƒS)N)r7r'r6r5r$)r"r')r;rrr$Ès z Serial.writercs*tt|ƒj|ƒ}|s|jr&|jj|ƒ|S)N)r6r5Úreadr8r7r&)r"Úsizer&)r;rrrMÌs  z Serial.readÚ cancel_readcs |jjddƒtt|ƒjƒdS)NzQ-RXrO)r7r*r6r5rO)r")r;rrrOÓszSerial.cancel_readÚ cancel_writecs |jjddƒtt|ƒjƒdS)NzQ-TXrP)r7r*r6r5rP)r")r;rrrPØszSerial.cancel_writecs*tt|ƒj}|jr&|jjddj|ƒƒ|S)NzQ-RXzin_waiting -> {})r6r5Ú in_waitingr8r7r*r )r"r)r;rrrQÜs zSerial.in_waitingcs |jjddƒtt|ƒjƒdS)NzQ-TXr%)r7r*r6r5r%)r")r;rrr%ãsz Serial.flushcs |jjddƒtt|ƒjƒdS)NzQ-RXÚreset_input_buffer)r7r*r6r5rR)r")r;rrrRçszSerial.reset_input_buffercs |jjddƒtt|ƒjƒdS)NzQ-TXÚreset_output_buffer)r7r*r6r5rS)r")r;rrrSëszSerial.reset_output_bufferçÐ?cs(|jjddj|ƒƒtt|ƒj|ƒdS)NÚBRKzsend_break {}s)r7r*r r6r5Ú send_break)r"Zduration)r;rrrVïszSerial.send_breakcCs*|jjd|rdndƒtjjj||ƒdS)NrUÚactiveÚinactive)r7r*r r5Úbreak_conditionr=)r"ÚlevelrrrrYószSerial.break_conditioncCs*|jjd|rdndƒtjjj||ƒdS)NZRTSrWrX)r7r*r r5Úrtsr=)r"rZrrrr[øsz Serial.rtscCs*|jjd|rdndƒtjjj||ƒdS)NZDTRrWrX)r7r*r r5Údtrr=)r"rZrrrr\ýsz Serial.dtrcs&tt|ƒj}|jjd|rdndƒ|S)NZCTSrWrX)r6r5Úctsr7r*)r"rZ)r;rrr]s z Serial.ctscs&tt|ƒj}|jjd|rdndƒ|S)NZDSRrWrX)r6r5Údsrr7r*)r"rZ)r;rrr^s z Serial.dsrcs&tt|ƒj}|jjd|rdndƒ|S)NZRIrWrX)r6r5Úrir7r*)r"rZ)r;rrr_s z Serial.rics&tt|ƒj}|jjd|rdndƒ|S)NZCDrWrX)r6r5Úcdr7r*)r"rZ)r;rrr`s z Serial.cd)r)rT)r+r,r-r.r#r r5r<Úsetterr>r$rMÚhasattrrOrPÚpropertyrQr%rRrSrVrYr[r\r]Úgetterr^r_r`Ú __classcell__rr)r;rr5˜s,         r5Ú__main__zspy:///dev/ttyS0)rEr0r rCÚ ImportErrorZ urllib.parseÚparserrÚobjectrr/r5r+Zserr<ÚprintrrrrÚs  3