#!/usr/bin/env python
|
#!/usr/bin/env python
|
#very simple serial terminal
|
#very simple serial terminal
|
#http://pyserial.sf.net package required
|
#http://pyserial.sf.net package required
|
#input characters are sent directly, received characters are displays as is
|
#input characters are sent directly, received characters are displays as is
|
#baudrate and echo configuartion is done through globals:
|
#baudrate and echo configuartion is done through globals:
|
|
|
#<cliechti@gmx.net>
|
#<cliechti@gmx.net>
|
|
|
import sys, os, serial, threading, getopt
|
import sys, os, serial, threading, getopt
|
#EXITCHARCTER = '\x1b' #ESC
|
#EXITCHARCTER = '\x1b' #ESC
|
EXITCHARCTER = '\x04' #ctrl+d
|
EXITCHARCTER = '\x04' #ctrl+d
|
|
|
#first choosea platform dependant way to read single characters from the console
|
#first choosea platform dependant way to read single characters from the console
|
if os.name == 'nt': #sys.platform == 'win32':
|
if os.name == 'nt': #sys.platform == 'win32':
|
import msvcrt
|
import msvcrt
|
def getkey():
|
def getkey():
|
while 1:
|
while 1:
|
if echo:
|
if echo:
|
z = msvcrt.getche()
|
z = msvcrt.getche()
|
else:
|
else:
|
z = msvcrt.getch()
|
z = msvcrt.getch()
|
if z == '\0' or z == '\xe0': #functions keys
|
if z == '\0' or z == '\xe0': #functions keys
|
msvcrt.getch()
|
msvcrt.getch()
|
else:
|
else:
|
return z
|
return z
|
|
|
elif os.name == 'posix':
|
elif os.name == 'posix':
|
#XXX: Untested code derrived from the Python FAQ....
|
#XXX: Untested code derrived from the Python FAQ....
|
# import termios, TERMIOS, sys, os
|
# import termios, TERMIOS, sys, os
|
import termios, sys, os
|
import termios, sys, os
|
fd = sys.stdin.fileno()
|
fd = sys.stdin.fileno()
|
old = termios.tcgetattr(fd)
|
old = termios.tcgetattr(fd)
|
new = termios.tcgetattr(fd)
|
new = termios.tcgetattr(fd)
|
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
|
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
|
new[6][TERMIOS.VMIN] = 1
|
new[6][TERMIOS.VMIN] = 1
|
new[6][TERMIOS.VTIME] = 0
|
new[6][TERMIOS.VTIME] = 0
|
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
|
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
|
s = '' # We'll save the characters typed and add them to the pool.
|
s = '' # We'll save the characters typed and add them to the pool.
|
def getkey():
|
def getkey():
|
c = os.read(fd, 1)
|
c = os.read(fd, 1)
|
if echo: sys.stdout.write(c)
|
if echo: sys.stdout.write(c)
|
return c
|
return c
|
def clenaup_console():
|
def clenaup_console():
|
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
|
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
|
sys.exitfunc = clenaup_console #terminal modes have to be restored on exit...
|
sys.exitfunc = clenaup_console #terminal modes have to be restored on exit...
|
|
|
else:
|
else:
|
raise "Sorry no implementation for your platform (%s) available." % sys.platform
|
raise "Sorry no implementation for your platform (%s) available." % sys.platform
|
|
|
|
|
def reader():
|
def reader():
|
"""loop forever and copy serial->console"""
|
"""loop forever and copy serial->console"""
|
while 1:
|
while 1:
|
sys.stdout.write(s.read())
|
sys.stdout.write(s.read())
|
|
|
def writer():
|
def writer():
|
"""loop forever and copy console->serial"""
|
"""loop forever and copy console->serial"""
|
while 1:
|
while 1:
|
c = getkey()
|
c = getkey()
|
if c == EXITCHARCTER: break #exit on esc
|
if c == EXITCHARCTER: break #exit on esc
|
s.write(c) #send character
|
s.write(c) #send character
|
if convert_outgoing_cr and c == '\r':
|
if convert_outgoing_cr and c == '\r':
|
s.write('\n')
|
s.write('\n')
|
if echo: sys.stdout.write('\n')
|
if echo: sys.stdout.write('\n')
|
|
|
|
|
#print a short help message
|
#print a short help message
|
def usage():
|
def usage():
|
print >>sys.stderr, """USAGE: %s [options]
|
print >>sys.stderr, """USAGE: %s [options]
|
Simple Terminal Programm for the serial port.
|
Simple Terminal Programm for the serial port.
|
|
|
options:
|
options:
|
-p, --port=PORT: port, a number, defualt = 0 or a device name
|
-p, --port=PORT: port, a number, defualt = 0 or a device name
|
-b, --baud=BAUD: baudrate, default 9600
|
-b, --baud=BAUD: baudrate, default 9600
|
-r, --rtscts: enable RTS/CTS flow control (default off)
|
-r, --rtscts: enable RTS/CTS flow control (default off)
|
-x, --xonxoff: enable software flow control (default off)
|
-x, --xonxoff: enable software flow control (default off)
|
-e, --echo: enable local echo (default off)
|
-e, --echo: enable local echo (default off)
|
-c, --cr: disable CR -> CR+LF translation
|
-c, --cr: disable CR -> CR+LF translation
|
|
|
""" % sys.argv[0]
|
""" % sys.argv[0]
|
|
|
if __name__ == '__main__':
|
if __name__ == '__main__':
|
#parse command line options
|
#parse command line options
|
try:
|
try:
|
opts, args = getopt.getopt(sys.argv[1:],
|
opts, args = getopt.getopt(sys.argv[1:],
|
"hp:b:rxec",
|
"hp:b:rxec",
|
["help", "port=", "baud=", "rtscts", "xonxoff", "echo", "cr"])
|
["help", "port=", "baud=", "rtscts", "xonxoff", "echo", "cr"])
|
except getopt.GetoptError:
|
except getopt.GetoptError:
|
# print help information and exit:
|
# print help information and exit:
|
usage()
|
usage()
|
sys.exit(2)
|
sys.exit(2)
|
|
|
port = 0
|
port = 0
|
baudrate = 9600
|
baudrate = 9600
|
echo = 0
|
echo = 0
|
convert_outgoing_cr = 1
|
convert_outgoing_cr = 1
|
rtscts = 0
|
rtscts = 0
|
xonxoff = 0
|
xonxoff = 0
|
for o, a in opts:
|
for o, a in opts:
|
if o in ("-h", "--help"): #help text
|
if o in ("-h", "--help"): #help text
|
usage()
|
usage()
|
sys.exit()
|
sys.exit()
|
elif o in ("-p", "--port"): #specified port
|
elif o in ("-p", "--port"): #specified port
|
try:
|
try:
|
port = int(a)
|
port = int(a)
|
except ValueError:
|
except ValueError:
|
port = a
|
port = a
|
elif o in ("-b", "--baud"): #specified baudrate
|
elif o in ("-b", "--baud"): #specified baudrate
|
try:
|
try:
|
baudrate = int(a)
|
baudrate = int(a)
|
except ValueError:
|
except ValueError:
|
raise ValueError, "Baudrate must be a integer number"
|
raise ValueError, "Baudrate must be a integer number"
|
elif o in ("-r", "--rtscts"):
|
elif o in ("-r", "--rtscts"):
|
rtscts = 1
|
rtscts = 1
|
elif o in ("-x", "--xonxoff"):
|
elif o in ("-x", "--xonxoff"):
|
xonxoff = 1
|
xonxoff = 1
|
elif o in ("-e", "--echo"):
|
elif o in ("-e", "--echo"):
|
echo = 1
|
echo = 1
|
elif o in ("-c", "--cr"):
|
elif o in ("-c", "--cr"):
|
convert_outgoing_cr = 0
|
convert_outgoing_cr = 0
|
|
|
try:
|
try:
|
s = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff)
|
s = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff)
|
except:
|
except:
|
print "could not open port"
|
print "could not open port"
|
sys.exit(1)
|
sys.exit(1)
|
print "--- Miniterm --- type Ctrl-D to quit"
|
print "--- Miniterm --- type Ctrl-D to quit"
|
#start serial->console thread
|
#start serial->console thread
|
r = threading.Thread(target=reader)
|
r = threading.Thread(target=reader)
|
r.setDaemon(1)
|
r.setDaemon(1)
|
r.start()
|
r.start()
|
#enter console->serial loop
|
#enter console->serial loop
|
writer()
|
writer()
|
|
|
print "\n--- exit ---"
|
print "\n--- exit ---"
|
|
|