Example #1
0
def sendpacket( data ):
    pkt = mkpkt( data )
    while True:
      com.write( pkt )
      if waitack():
        break
      else:
        print "CRC NACK. RESENDING."
Example #2
0
def upload( data ):
    data = hpstr.toarr( data )
    resp = com.read( 1 )[0]
    if resp != ord( 'D' ):
      cancel()
      print "Device didn't accept file:", hex( resp )
      return False

    pos = 0
    pktnr = 1
    while pos < len( data ):
      sleep(0.1)

      size = len( data ) # len data may change due to padding
      if size-pos >= 1024:
        ptype = 2
        plen = 1024
      if  size-pos >= 128:
        ptype = 1
        plen = 128
      else:
        while size-pos < 128:
          data.append( 0x1a )
          size += 1
        ptype = 1
        plen = 128

      chunk = data[pos:pos+plen]
      if ptype == 1: #SOH
        com.write( [1] )
        #print "SOH",
        sleep( 0.1 )
        com.write( [pktnr, 255-pktnr] )
        #print hpstr.tohexstr( [pktnr, 255-pktnr] ),
        com.write( chunk )
        #print hpstr.tohexstr( chunk ),
        crc = hpcrc( chunk )
        com.write( [crc>>8, crc & 0xff ] )
        if waitack() == True:
          #print "CHKSUM ACK"
          pos += plen
        else:
          print "CHKSUM NACK"
          # TODO: retry counter
        # TODO: cancel checking

    com.write( [4]  ) #EOT
    #print "EOT"
    #waitack()
    #sendack()
    return waitack()
Example #3
0
def cmd( cmd, args=None ):
    if args:
      s = cmd + mkpacket( args )
      #print "CMD:", hpstr.tohexstr( s )
      # TODO: can we send as one chunk?
      com.write( s[0] )
      sleep( 0.1 )
      com.write( s[1:] )
    else:
      s = cmd
      #print "CMD:", hpstr.tohexstr( s )
      com.write( s )
Example #4
0
def cancel():
    com.write( '\x18' ) 
    com.write( '\x18' ) 
    com.write( '\x18' ) 
Example #5
0
def sendnack():
    com.write( '\x15' )
Example #6
0
def sendack():
    com.write( '\x06' )
Example #7
0
def write(data, timeout=1000):
    """Writes data to USB.
       Returns number of bytes written.
    """
    return com.write(data, timeout=timeout)