timeout fix
[pyfrprog.git] / frprog.py
index 0ca52c57de1fd788e5b06f4af00be107e0549c6d..ce102ee8af94f89f61fe3a9f429d525e9a0cd1d9 100755 (executable)
--- a/frprog.py
+++ b/frprog.py
@@ -2,8 +2,6 @@
 import sys, time
 from SerialPort_linux import *
 
-# serial device to communicate with
-DEVICE="/dev/ttyUSB0"
 # baudrate used for initialization
 INIT_BAUDRATE=9600
 # baudrate used for communication with the internal bootloader after init
@@ -179,14 +177,33 @@ def readMHXFile(filename): # desired mhx filename
        fp.close()
        return retval
 
+def usage(execfile):
+       print "Usage: " + execfile + " <target mhx-file> [-d DEVICE]"
+
 def main(argv=None):
        # check command line arguments
        if argv is None:
                argv = sys.argv
-       if len(argv) != 2:
-               print "Usage: " + argv[0] + " [target mhx-file]"
+
+       if len(argv) == 2 and (argv[1] == "-v" or argv[1] == "--version"):
+               print "Version: %VERSION%"
+               return 0
+
+       if len(argv) != 2 and len(argv) != 4:
+               usage(argv[0])
                return 1
 
+       # standard serial device to communicate with
+       DEVICE="/dev/ttyUSB0"
+
+       # overrule standard device if provided with -d
+       if len(argv) == 4:
+               if argv[2] == "-d":
+                       DEVICE = argv[3]
+               else:
+                       usage(argv[0])
+                       return 1
+
        # read in data from mhx-files before starting
        try:
                try:
@@ -218,7 +235,11 @@ def main(argv=None):
 
        print "OK, trying to set baudrate..."
        # set baudrate
-       bootromBAUDRATE(BOOTLOADER_BAUDRATE)
+       try:
+               bootromBAUDRATE(BOOTLOADER_BAUDRATE)
+       except SerialPortException:
+               print "timeout exception: try again ->"
+               bootromBAUDRATE(BOOTLOADER_BAUDRATE)
        time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
        del tty
        tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)