From 7197a892d83199f32d4d2a7fecaaa64240d2be9a Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 2 Jul 2013 18:38:05 +0000 Subject: set_serial now suports baudrate git-svn-id: https://svn.spreadspace.org/pic/trunk@21 a09c6847-51d9-44de-8ef2-e725cf50f3c7 --- downloader/downloader.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'downloader/downloader.py') diff --git a/downloader/downloader.py b/downloader/downloader.py index 49ffe03..cc1c97c 100755 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -25,10 +25,30 @@ VERSION = '0.1' -def open_serial(device): +def open_serial(device, baud): + import os + import tty + import termios + + baudrates = { 50: termios.B50, 75: termios.B75, 110: termios.B110, 134: termios.B134, + 150: termios.B150, 200: termios.B200, 300: termios.B300, 600: termios.B600, + 1200: termios.B1200, 1800: termios.B1800, 2400: termios.B2400, 4800: termios.B4800, + 9600: termios.B9600, 19200: termios.B19200, 38400: termios.B38400, + 57600: termios.B57600, 115200: termios.B115200, 230400: termios.B230400 } + + baudreate = termios.B19200 + try: + baudrate = baudrates[int(baud)] + except (KeyError, ValueError): + print "ERROR: invalid baudrate" + sys.exit(3) + try: - dev = os.open(device, os.O_RDWR | os.O_NOCTTY); + dev = os.open(device, os.O_RDWR | os.O_NOCTTY) tty.setraw(dev, termios.TCSAFLUSH) + tio = termios.tcgetattr(dev) + tio[4] = tio[5] = baudrate + termios.tcsetattr(dev, termios.TCSAFLUSH, tio) termios.tcflush(dev, termios.TCIFLUSH) return dev @@ -39,7 +59,10 @@ def open_serial(device): print "ERROR: configuring serial device: %s" % msg sys.exit(3) + def load_hex(file): + import os + fin = file if fin == '-': fin = sys.stdin @@ -62,10 +85,7 @@ def load_hex(file): if __name__ == '__main__': import getopt - import os import sys - import termios - import tty from intelhex import IntelHex @@ -81,13 +101,15 @@ Options: -h, --help this help message. -v, --version version info. --device=N the serial port to use (default: /dev/ttyUSB0). + --baud=N baudrate to use (default: 19200). ''' device = "/dev/ttyUSB0" + baudrate = 19200 try: opts, args = getopt.getopt(sys.argv[1:], "hv", - ["help", "version", "device="]) + ["help", "version", "device=", "baud="]) for o, a in opts: if o in ("-h", "--help"): @@ -98,6 +120,8 @@ Options: sys.exit(0) elif o in ("--device"): device = a + elif o in ("--baud"): + baudrate = a if not args: raise getopt.GetoptError('Input file is not specified') @@ -110,7 +134,7 @@ Options: print usage sys.exit(2) - dev = open_serial(device) + dev = open_serial(device, baudrate) data = load_hex(args[0]) addrs = data.keys() -- cgit v1.2.3