2012-11-14 16:57:15 -05:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Based on miniterm sample in pyserial
|
|
|
|
|
2012-11-14 17:13:56 -05:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import serial
|
|
|
|
import threading
|
|
|
|
import traceback
|
|
|
|
import time
|
|
|
|
import colorama
|
|
|
|
|
|
|
|
colorama.init()
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
import msvcrt
|
|
|
|
class Console:
|
2012-11-14 17:00:06 -05:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def cleanup(self):
|
2012-11-14 16:57:15 -05:00
|
|
|
pass
|
|
|
|
def getkey(self):
|
|
|
|
while 1:
|
|
|
|
z = msvcrt.getch()
|
|
|
|
if z == '\0' or z == '\xe0': # function keys
|
|
|
|
msvcrt.getch()
|
|
|
|
else:
|
|
|
|
if z == '\r':
|
|
|
|
return '\n'
|
|
|
|
return z
|
|
|
|
elif os.name == 'posix':
|
|
|
|
import termios, sys, os
|
|
|
|
class Console:
|
2012-11-14 17:00:06 -05:00
|
|
|
def __init__(self):
|
2012-11-14 16:57:15 -05:00
|
|
|
self.fd = sys.stdin.fileno()
|
2012-11-14 17:00:06 -05:00
|
|
|
try:
|
|
|
|
self.old = termios.tcgetattr(self.fd)
|
|
|
|
new = termios.tcgetattr(self.fd)
|
|
|
|
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO & ~termios.ISIG
|
|
|
|
new[6][termios.VMIN] = 1
|
|
|
|
new[6][termios.VTIME] = 0
|
|
|
|
termios.tcsetattr(self.fd, termios.TCSANOW, new)
|
|
|
|
except termios.error:
|
|
|
|
# ignore errors, so we can pipe stuff to this script
|
|
|
|
pass
|
|
|
|
def cleanup(self):
|
|
|
|
try:
|
|
|
|
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
|
|
|
|
except:
|
|
|
|
# ignore errors, so we can pipe stuff to this script
|
|
|
|
pass
|
2012-11-14 16:57:15 -05:00
|
|
|
def getkey(self):
|
|
|
|
c = os.read(self.fd, 1)
|
|
|
|
return c
|
|
|
|
else:
|
2012-11-14 17:00:52 -05:00
|
|
|
raise ("Sorry, no terminal implementation for your platform (%s) "
|
|
|
|
"available." % sys.platform)
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
class Miniterm:
|
2012-11-14 17:13:56 -05:00
|
|
|
"""Normal interactive terminal"""
|
|
|
|
|
2012-11-14 16:57:15 -05:00
|
|
|
def __init__(self, serial):
|
|
|
|
self.serial = serial
|
2012-11-14 17:13:56 -05:00
|
|
|
self.threads = []
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.alive = True
|
2012-11-14 17:13:56 -05:00
|
|
|
# serial->console
|
|
|
|
self.threads.append(threading.Thread(target=self.reader))
|
|
|
|
# console->serial
|
2012-11-14 17:00:06 -05:00
|
|
|
self.console = Console()
|
2012-11-14 17:13:56 -05:00
|
|
|
self.threads.append(threading.Thread(target=self.writer))
|
|
|
|
# start them
|
|
|
|
for thread in self.threads:
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.alive = False
|
|
|
|
|
2012-11-14 17:13:56 -05:00
|
|
|
def join(self):
|
|
|
|
for thread in self.threads:
|
|
|
|
thread.join()
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
def reader(self):
|
|
|
|
"""loop and copy serial->console"""
|
2012-11-14 17:00:06 -05:00
|
|
|
try:
|
|
|
|
while self.alive:
|
|
|
|
data = self.serial.read(1)
|
2012-11-14 17:13:56 -05:00
|
|
|
if not data:
|
|
|
|
continue
|
2012-11-14 16:57:15 -05:00
|
|
|
sys.stdout.write(data)
|
2012-11-14 17:00:06 -05:00
|
|
|
sys.stdout.flush()
|
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
|
|
|
self.console.cleanup()
|
|
|
|
os._exit(1)
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
def writer(self):
|
|
|
|
"""loop and copy console->serial until ^C"""
|
2012-11-14 17:00:06 -05:00
|
|
|
try:
|
2012-11-14 16:57:15 -05:00
|
|
|
while self.alive:
|
|
|
|
try:
|
2012-11-14 17:00:06 -05:00
|
|
|
c = self.console.getkey()
|
2012-11-14 16:57:15 -05:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
c = '\x03'
|
|
|
|
if c == '\x03':
|
|
|
|
self.stop()
|
2012-11-14 17:00:06 -05:00
|
|
|
return
|
|
|
|
elif c == '':
|
|
|
|
# EOF on input. Wait a tiny bit so we can
|
|
|
|
# flush the remaining input, then stop.
|
|
|
|
time.sleep(0.25)
|
|
|
|
self.stop()
|
|
|
|
return
|
2012-11-14 16:57:15 -05:00
|
|
|
else:
|
|
|
|
self.serial.write(c) # send character
|
2012-11-14 17:00:06 -05:00
|
|
|
except Exception as e:
|
|
|
|
traceback.print_exc()
|
|
|
|
self.console.cleanup()
|
|
|
|
os._exit(1)
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
saved_timeout = self.serial.timeout
|
|
|
|
self.serial.timeout = 0.1
|
|
|
|
self.start()
|
|
|
|
self.join()
|
2012-11-14 17:00:06 -05:00
|
|
|
print ""
|
|
|
|
self.console.cleanup()
|
2012-11-14 17:00:52 -05:00
|
|
|
self.serial.timeout = saved_timeout
|
2012-11-14 16:57:15 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import argparse
|
|
|
|
|
2012-11-14 17:13:56 -05:00
|
|
|
parser = argparse.ArgumentParser(description="Simple serial terminal. "
|
|
|
|
"If two devices are provided, output from "
|
|
|
|
"the first is shown in yellow, second in "
|
|
|
|
"cyan, and input is discarded.")
|
2012-11-14 16:57:15 -05:00
|
|
|
parser.add_argument('device', metavar='DEVICE',
|
2012-11-14 17:13:56 -05:00
|
|
|
help='serial device 1')
|
2012-11-14 16:57:15 -05:00
|
|
|
parser.add_argument('baudrate', metavar='BAUDRATE', type=int, nargs='?',
|
2012-11-14 17:13:56 -05:00
|
|
|
help='baud rate 1', default=115200)
|
|
|
|
parser.add_argument('device2', metavar="DEVICE2", nargs='?',
|
|
|
|
help='serial device 2', default=None)
|
|
|
|
parser.add_argument('baudrate2', metavar='BAUDRATE2', type=int, nargs='?',
|
|
|
|
help='baud rate 2', default=115200)
|
2012-11-14 16:57:15 -05:00
|
|
|
args = parser.parse_args()
|
2012-11-14 17:00:52 -05:00
|
|
|
try:
|
|
|
|
dev = serial.Serial(args.device, args.baudrate)
|
|
|
|
except serial.serialutil.SerialException:
|
|
|
|
sys.stderr.write("error opening %s\n" % args.device)
|
|
|
|
raise SystemExit(1)
|
2012-11-14 17:13:56 -05:00
|
|
|
print args.device + ", " + str(args.baudrate) + " baud"
|
|
|
|
print "^C to exit"
|
|
|
|
print "--"
|
2012-11-14 17:00:52 -05:00
|
|
|
term = Miniterm(dev)
|
2012-11-14 16:57:15 -05:00
|
|
|
term.run()
|
|
|
|
|