Browse Source

Initial firmware upload support.. but its a mess.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
fbaf100dac
3 changed files with 80 additions and 10 deletions
  1. +1
    -0
      .gitignore
  2. +58
    -0
      pyad2usb/util.py
  3. +21
    -10
      test.py

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
*.pyc
tmp

+ 58
- 0
pyad2usb/util.py View File

@@ -1,5 +1,63 @@
import ad2usb
import time

class NoDeviceError(Exception):
pass

class CommError(Exception):
pass

class Firmware(object):
def __init__(self):
pass

def __del__(self):
pass

@staticmethod
def upload(dev, filename):
def do_upload():
with open(filename) as f:
print 'firmwaring this mofo!'
for line in f:
line = line.rstrip()
if line[0] == ':':
print "> {0}".format(line)
dev.write(line + "\r")
crap = dev.read_line()
print "< {0}".format(crap)

time.sleep(0.05)

def read_until(data):
buf = ''
position = 0

while True:
try:
char = dev.read()

if char is not None and char != '':
if char == data[position]:
position = position + 1
if position == len(data):
return True
else:
position = 0
except Exception, err:
pass

if dev is None:
raise NoDeviceError('No device specified for firmware upload.')

dev.close_reader()

time.sleep(1)

dev.write("=\r\n")
read_until('!boot')

dev.write("=\r\n")
read_until('!load')

do_upload()

+ 21
- 10
test.py View File

@@ -41,31 +41,42 @@ try:

#a2u = overseer.get_device()

a2u = pyad2usb.ad2usb.Overseer.create()
#a2u = pyad2usb.ad2usb.Overseer.create()

#dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
#dev = pyad2usb.ad2usb.devices.USBDevice()#serial='A101A429', description='FT232R USB UART')

#a2u = pyad2usb.ad2usb.AD2USB(dev)
a2u.on_open += handle_open
a2u.on_close += handle_close
a2u.on_read += handle_read
a2u.on_write += handle_write
#a2u.on_open += handle_open
#a2u.on_close += handle_close
#a2u.on_read += handle_read
#a2u.on_write += handle_write

a2u.open()
dev.on_open += handle_open
dev.on_close += handle_close
#dev.on_read += handle_read
dev.on_write += handle_write


#a2u.open()
#a2u.open(baudrate=19200, interface='/dev/ttyUSB0')

#dev.open(baudrate=115200, interface='/dev/ttyUSB5')
#dev.open(baudrate=19200, interface='/dev/ttyUSB0')
#dev.open()
dev.open()

pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex')

while running:
time.sleep(0.1)

a2u.close()
#dev.close()
print 'wat'

#a2u.close()
dev.close()
#overseer.close()

except Exception, err:
#print 'Error: {0}'.format(str(err))
print 'wtf'
traceback.print_exc(err)

Loading…
Cancel
Save