summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-07-02 18:38:05 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-07-02 18:38:05 +0000
commit7197a892d83199f32d4d2a7fecaaa64240d2be9a (patch)
tree15a8c52b81b8cfa4d4b73e6de7793146db7c295c
parentadded return codes for proto definition (diff)
set_serial now suports baudrate
git-svn-id: https://svn.spreadspace.org/pic/trunk@21 a09c6847-51d9-44de-8ef2-e725cf50f3c7
-rwxr-xr-xdownloader/downloader.py38
1 files changed, 31 insertions, 7 deletions
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()