parameters: (self, cmd, dataOutstr, length)
docstring: Sends the command cmd with the data given in dataOut to the
         microcontroller and expects either an acknowledge or a frame
         with result from the microcontroller.  The results are stored
         in dataIn (if not a NULL pointer is passed).
         In this routine all the necessary protocol stuff is handled.
         Returns zero if the function was successful.
docstring_summary: Sends the command cmd with the data given in dataOut to the
         microcontroller and expects either an acknowledge or a frame
         with result from the microcontroller.  The results are stored
         in dataIn (if not a NULL pointer is passed).
         In this routine all the necessary protocol stuff is handled.
         Returns zero if the function was successful.
docstring_tokens: Sends the command cmd with the data given in dataOut to the microcontroller and expects either an acknowledge or a frame with result from the microcontroller . The results are stored in dataIn ( if not a NULL pointer is passed ) . In this routine all the necessary protocol stuff is handled . Returns zero if the function was successful .
function: def comTxRx(self, cmd, dataOutstr, length): # shed skin: change name of dataOut
         """Sends the command cmd with the data given in dataOut to the
         microcontroller and expects either an acknowledge or a frame
         with result from the microcontroller.  The results are stored
         in dataIn (if not a NULL pointer is passed).
         In this routine all the necessary protocol stuff is handled.
         Returns zero if the function was successful."""
         if DEBUG > 1: sys.stderr.write("* comTxRx()\n")
         #txFrame     = [] # shed skin 
         rxHeader    = 0
         rxNum       = 0
  
         dataOut = list(dataOutstr)     #convert to a list for simpler data fill in
         #Transmitting part ----------------------------------------
         #Prepare data for transmit
         # shed skin : put chr around (0xff) and (0), or it cannot work (because of string.join below)
         if (length % 2) != 0:
             #/* Fill with one byte to have even number of bytes to send */
             if self.protocolMode == MODE_BSL:
                 dataOut.append(chr(0xFF))  #fill with 0xFF
             else:
                 dataOut.append(chr(0))     #fill with zero
  
         txFrame = "%c%c%c%c" % (DATA_FRAME | self.seqNo, cmd, len(dataOut), len(dataOut))
  
         self.reqNo = (self.seqNo + 1) % MAX_FRAME_COUNT
  
         txFrame = txFrame + string.join(dataOut,'')
         checksum = self.calcChecksum(txFrame, length + 4)
         txFrame = txFrame + chr(checksum & 0xff)
         txFrame = txFrame + chr((checksum >> 8) & 0xff)
  
         accessAddr = (0x0212 + (checksum^0xffff)) & 0xfffe  #0x0212: Address of wCHKSUM
         if self.BSLMemAccessWarning and accessAddr < BSL_CRITICAL_ADDR:
             sys.stderr.write("WARNING: This command might change data at address %04x or %04x!\n" % (accessAddr, accessAddr + 1))
  
         self.serialport.flushInput()                #clear receiving queue
         #TODO: Check after each transmitted character,
         #TODO: if microcontroller did send a character (probably a NAK!).
         for c in txFrame:
             self.serialport.write(c)
             if DEBUG > 3: sys.stderr.write("\ttx %02x" % ord(c))
             #if self.serialport.inWaiting(): break  #abort when BSL replies, probably NAK
         else:
             if DEBUG > 1: sys.stderr.write( "  comTxRx() transmit OK\n")
  
         #Receiving part -------------------------------------------
         rxHeader, rxNum = self.comRxHeader()        #receive header
         if DEBUG > 1: sys.stderr.write("  comTxRx() rxHeader=0x%02x, rxNum=%d, seqNo=%d, reqNo=%s\n" % (rxHeader, rxNum, self.seqNo, self.reqNo))
         if rxHeader == DATA_ACK:               #acknowledge/OK
             if DEBUG > 2: sys.stderr.write("  comTxRx() DATA_ACK\n")
             if rxNum == self.reqNo:
                 self.seqNo = self.reqNo
                 if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_ACK OK\n")
                 return          #Acknowledge received correctly => next frame
             raise BSLException(ERR_FRAME_NUMBER)
         elif rxHeader == DATA_NAK:             #not acknowledge/error
             if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_NAK\n")
             raise BSLException(ERR_RX_NAK)
         elif rxHeader == DATA_FRAME:           #receive data
             if DEBUG > 2: sys.stderr.write("* comTxRx() DATA_FRAME\n")
             if rxNum == self.reqNo:
                 rxFrame = self.comRxFrame(rxNum)
                 return rxFrame
             raise BSLException(ERR_FRAME_NUMBER)
         elif rxHeader == CMD_FAILED:           #Frame ok, but command failed.
             if DEBUG > 2: sys.stderr.write("*  comTxRx() CMD_FAILED\n")
             raise BSLException(ERR_CMD_FAILED)
  
         raise BSLException("Unknown header 0x%02x\nAre you downloading to RAM into an old device that requires the patch? Try option -U" % rxHeader)
function_tokens: def comTxRx ( self , cmd , dataOutstr , length ) : # shed skin: change name of dataOut if DEBUG > 1 : sys . stderr . write ( "* comTxRx()\n" ) #txFrame     = [] # shed skin  rxHeader = 0 rxNum = 0 dataOut = list ( dataOutstr ) #convert to a list for simpler data fill in #Transmitting part ---------------------------------------- #Prepare data for transmit # shed skin : put chr around (0xff) and (0), or it cannot work (because of string.join below) if ( length % 2 ) != 0 : #/* Fill with one byte to have even number of bytes to send */ if self . protocolMode == MODE_BSL : dataOut . append ( chr ( 0xFF ) ) #fill with 0xFF else : dataOut . append ( chr ( 0 ) ) #fill with zero txFrame = "%c%c%c%c" % ( DATA_FRAME | self . seqNo , cmd , len ( dataOut ) , len ( dataOut ) ) self . reqNo = ( self . seqNo + 1 ) % MAX_FRAME_COUNT txFrame = txFrame + string . join ( dataOut , '' ) checksum = self . calcChecksum ( txFrame , length + 4 ) txFrame = txFrame + chr ( checksum & 0xff ) txFrame = txFrame + chr ( ( checksum >> 8 ) & 0xff ) accessAddr = ( 0x0212 + ( checksum ^ 0xffff ) ) & 0xfffe #0x0212: Address of wCHKSUM if self . BSLMemAccessWarning and accessAddr < BSL_CRITICAL_ADDR : sys . stderr . write ( "WARNING: This command might change data at address %04x or %04x!\n" % ( accessAddr , accessAddr + 1 ) ) self . serialport . flushInput ( ) #clear receiving queue #TODO: Check after each transmitted character, #TODO: if microcontroller did send a character (probably a NAK!). for c in txFrame : self . serialport . write ( c ) if DEBUG > 3 : sys . stderr . write ( "\ttx %02x" % ord ( c ) ) #if self.serialport.inWaiting(): break  #abort when BSL replies, probably NAK else : if DEBUG > 1 : sys . stderr . write ( "  comTxRx() transmit OK\n" ) #Receiving part ------------------------------------------- rxHeader , rxNum = self . comRxHeader ( ) #receive header if DEBUG > 1 : sys . stderr . write ( "  comTxRx() rxHeader=0x%02x, rxNum=%d, seqNo=%d, reqNo=%s\n" % ( rxHeader , rxNum , self . seqNo , self . reqNo ) ) if rxHeader == DATA_ACK : #acknowledge/OK if DEBUG > 2 : sys . stderr . write ( "  comTxRx() DATA_ACK\n" ) if rxNum == self . reqNo : self . seqNo = self . reqNo if DEBUG > 2 : sys . stderr . write ( "* comTxRx() DATA_ACK OK\n" ) return #Acknowledge received correctly => next frame raise BSLException ( ERR_FRAME_NUMBER ) elif rxHeader == DATA_NAK : #not acknowledge/error if DEBUG > 2 : sys . stderr . write ( "* comTxRx() DATA_NAK\n" ) raise BSLException ( ERR_RX_NAK ) elif rxHeader == DATA_FRAME : #receive data if DEBUG > 2 : sys . stderr . write ( "* comTxRx() DATA_FRAME\n" ) if rxNum == self . reqNo : rxFrame = self . comRxFrame ( rxNum ) return rxFrame raise BSLException ( ERR_FRAME_NUMBER ) elif rxHeader == CMD_FAILED : #Frame ok, but command failed. if DEBUG > 2 : sys . stderr . write ( "*  comTxRx() CMD_FAILED\n" ) raise BSLException ( ERR_CMD_FAILED ) raise BSLException ( "Unknown header 0x%02x\nAre you downloading to RAM into an old device that requires the patch? Try option -U" % rxHeader )