RA
Без названия
Публично отправил Гость в 20:24 22-02-2012 с типом python и размером 1.94 KbХранить: Вечно, просмотров: 30
В буфер! | Скачать!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import os
- import time
- import serial
- DEVICE_FILE = "/dev/ttyUSB3"
- TIMEOUT = 15.0 # In seconds
- PROMPT = "SKY8860 # "
- FW_FILE = "test.bin"
- MEM_ADDR = 0x00480000
- ECHO = False
- BUF_SZ = 64
- #def readLine(f):
- # l = f.readline()
- # print "dev >", l[0:-1]
- # return l
- def writeLine(f, l):
- f.write(l + "\n")
- f.flush()
- if ECHO:
- print "[DEV] <", l
- def waitForPrompt(dev):
- data = ""
- start = time.clock()
- while True:
- b = dev.read(BUF_SZ)
- if not b:
- if (time.clock() - start) > TIMEOUT:
- return False
- else:
- if ECHO:
- print "[DEV] >", repr(b)
- data += b
- if PROMPT in data:
- return True
- def main():
- tty = serial.Serial(DEVICE_FILE, baudrate = 115200, timeout = 0, xonxoff = True, rtscts = False, dsrdtr = False)
- # Wait for promt before start executing any commands.
- waitForPrompt(tty)
- # At this time it isn't strictly required.
- fw_sz = os.path.getsize(FW_FILE)
- f = open(FW_FILE, "rb")
- old_progress = -1
- written = 0
- offset = MEM_ADDR
- start = time.clock()
- old_spd = start
- while written != fw_sz:
- s = f.read(1)
- if not s:
- raise Exception("Unexpected end of file")
- cmd = "mw %08X %02X" % (offset, ord(s[0]))
- writeLine(tty, cmd)
- written += 1
- offset += 1
- progress = int(float(written) / fw_sz * 100.0)
- if progress != old_progress:
- print "!!! PROGRESS: %03d%% !!!" % progress
- old_progress = progress
- cur_time = time.clock()
- if cur_time - old_spd >= 1.0:
- old_spd = cur_time
- speed = int(written / (cur_time - start))
- print "!!! SPEED: %d bytes/s !!!" % speed
- waitForPrompt(tty)
- main()
Комментируй
Исходный текст
Джек Сойер
Андрей
Jeron
etapasvi.com