Помогаем работать и общаться

Без названия

Публично отправил Гость в 20:24 22-02-2012 с типом python и размером 1.94 Kb
Хранить: Вечно, просмотров: 30
В буфер! | Скачать!

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import time
  6. import serial
  7.  
  8. DEVICE_FILE = "/dev/ttyUSB3"
  9. TIMEOUT     = 15.0           # In seconds
  10. PROMPT      = "SKY8860 # "
  11. FW_FILE     = "test.bin"
  12. MEM_ADDR    = 0x00480000
  13.  
  14. ECHO = False
  15.  
  16. BUF_SZ = 64
  17.  
  18. #def readLine(f):
  19. #    l = f.readline()
  20. #    print "dev >", l[0:-1]
  21. #    return l
  22.  
  23. def writeLine(f, l):
  24.     f.write(l + "\n")
  25.     f.flush()
  26.     if ECHO:
  27.         print "[DEV] <", l
  28.  
  29. def waitForPrompt(dev):
  30.     data = ""
  31.     start = time.clock()
  32.     while True:
  33.         b = dev.read(BUF_SZ)
  34.         if not b:
  35.             if (time.clock() - start) > TIMEOUT:
  36.                 return False
  37.         else:
  38.             if ECHO:
  39.                 print "[DEV] >", repr(b)
  40.  
  41.             data += b
  42.             if PROMPT in data:
  43.                 return True
  44.  
  45. def main():
  46.     tty = serial.Serial(DEVICE_FILE, baudrate = 115200, timeout = 0, xonxoff = True, rtscts = False, dsrdtr = False)
  47.  
  48.     # Wait for promt before start executing any commands.
  49.     waitForPrompt(tty)
  50.     # At this time it isn't strictly required.
  51.  
  52.     fw_sz = os.path.getsize(FW_FILE)
  53.     f = open(FW_FILE, "rb")
  54.  
  55.     old_progress = -1
  56.     written = 0
  57.     offset = MEM_ADDR
  58.     start = time.clock()
  59.     old_spd = start
  60.     while written != fw_sz:
  61.         s = f.read(1)
  62.         if not s:
  63.             raise Exception("Unexpected end of file")
  64.  
  65.         cmd = "mw %08X %02X" % (offset, ord(s[0]))
  66.         writeLine(tty, cmd)
  67.  
  68.         written += 1
  69.         offset += 1
  70.  
  71.         progress = int(float(written) / fw_sz * 100.0)
  72.         if progress != old_progress:
  73.             print "!!! PROGRESS: %03d%% !!!" % progress
  74.         old_progress = progress
  75.  
  76.         cur_time = time.clock()
  77.         if cur_time - old_spd >= 1.0:
  78.             old_spd = cur_time
  79.             speed = int(written / (cur_time - start))
  80.             print "!!! SPEED: %d bytes/s !!!" % speed
  81.  
  82.         waitForPrompt(tty)
  83.  
  84. main()
  85.  

Комментируй
Исходный текст