diff --git a/boards/opencm9-04/dist/robotis-loader.py b/boards/opencm9-04/dist/robotis-loader.py index 3b13dad0a23a..823166aa38c9 100755 --- a/boards/opencm9-04/dist/robotis-loader.py +++ b/boards/opencm9-04/dist/robotis-loader.py @@ -3,7 +3,7 @@ ''' MIT License -Copyright (c) 2014 Grégoire Passault +Copyright (c) 2014 Gregoire Passault Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -35,11 +35,24 @@ # # https://github.com/Gregwar/robotis-loader -import serial, sys, os, time +import serial +import sys +import os +import time print('~~ Robotis loader ~~') print('') + +# Helper function for bytes conversion +if sys.version_info[:1][0] == 3: + def to_ord(val): + return ord(chr(val)) +else: + def to_ord(val): + return ord(val) + + # Reading command line #if len(sys.argv) != 3: # exit('! Usage: robotis-loader.py ') @@ -49,21 +62,26 @@ port = os.environ["PORT"] binary = os.environ["HEXFILE"] -# Helper that prints a progress bar + def progressBar(percent, precision=65): - threshold=precision*percent/100.0 + """Prints a progress bar.""" + + threshold = precision*percent / 100.0 sys.stdout.write('[ ') - for x in xrange(0, precision): - if x < threshold: sys.stdout.write('#') - else: sys.stdout.write(' ') + for x in range(precision): + if x < threshold: + sys.stdout.write('#') + else: + sys.stdout.write(' ') sys.stdout.write(' ] ') sys.stdout.flush() + # Opening the firmware file try: stat = os.stat(binary) size = stat.st_size - firmware = file(binary, 'rb') + firmware = open(binary, 'rb') print('* Opening %s, size=%d' % (binary, size)) except: exit('! Unable to open file %s' % binary) @@ -79,19 +97,19 @@ def progressBar(percent, precision=65): s.setDTR(False) time.sleep(0.1) s.setRTS(False) -s.write('CM9X') +s.write(b'CM9X') s.close() time.sleep(1.0); print('* Connecting...') s = serial.Serial(port, baudrate=115200) -s.write('AT&LD') +s.write(b'AT&LD') print('* Download signal transmitted, waiting...') # Entering bootloader sequence while True: line = s.readline().strip() - if line.endswith('Ready..'): + if line.endswith(b'Ready..'): print('* Board ready, sending data') cs = 0 pos = 0 @@ -100,23 +118,26 @@ def progressBar(percent, precision=65): if len(c): pos += len(c) sys.stdout.write("\r") - progressBar(100*float(pos)/float(size)) + progressBar(100 * float(pos) / float(size)) s.write(c) - for k in range(0,len(c)): - cs = (cs+ord(c[k]))%256 + for k in range(0, len(c)): + cs = (cs + to_ord(c[k])) % 256 else: + firmware.close() break print('') s.setDTR(True) print('* Checksum: %d' % (cs)) - s.write(chr(cs)) + import struct + s.write(struct.pack('B', cs)) + # s.write('{0}'.format(chr(cs)).encode('ascii')) print('* Firmware was sent') else: - if line == 'Success..': + if line == b'Success..': print('* Success, running the code') print('') - s.write('AT&RST') + s.write(b'AT&RST') s.close() exit() else: - print('Board -> '+line) + print('Board -> {}'.format(line))