1
1
#!/usr/bin/env python
3
#very simple serial terminal
4
#(C)2002 Chris Liechti >cliecht@gmx.net>
3
# Very simple serial terminal
4
# (C)2002-2004 Chris Liechti <cliecht@gmx.net>
6
#input characters are sent directly, received characters are displays as is
7
#baudrate and echo configuartion is done through globals
6
# Input characters are sent directly (only LF -> CR/LF/CRLF translation is
7
# done), received characters are displayed as is (or as trough pythons
8
# repr, useful for debug purposes)
9
# Baudrate and echo configuartion is done through globals
10
12
import sys, os, serial, threading, getopt
11
#EXITCHARCTER = '\x1b' #ESC
12
EXITCHARCTER = '\x04' #ctrl+d
14
#first choosea platform dependant way to read single characters from the console
14
EXITCHARCTER = '\x04' #ctrl+D
16
#first choose a platform dependant way to read single characters from the console
15
17
if os.name == 'nt':
23
25
if z == '\0' or z == '\xe0': #functions keys
28
32
elif os.name == 'posix':
29
#XXX: Untested code drrived from the Python FAQ....
30
import termios, TERMIOS, sys, os
33
import termios, sys, os
31
34
fd = sys.stdin.fileno()
32
35
old = termios.tcgetattr(fd)
33
36
new = termios.tcgetattr(fd)
34
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
35
new[6][TERMIOS.VMIN] = 1
36
new[6][TERMIOS.VTIME] = 0
37
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
37
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
38
new[6][termios.VMIN] = 1
39
new[6][termios.VTIME] = 0
40
termios.tcsetattr(fd, termios.TCSANOW, new)
38
41
s = '' # We'll save the characters typed and add them to the pool.
42
if echo: sys.stdout.write(c)
44
#~ c = sys.stdin.read(1)
45
if echo: sys.stdout.write(c); sys.stdout.flush()
44
47
def clenaup_console():
45
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
46
sys.exitfunc = clenaup_console #terminal modes have to be restored on exit...
48
termios.tcsetattr(fd, termios.TCSAFLUSH, old)
49
sys.exitfunc = clenaup_console #terminal modes have to be restored on exit...
49
52
raise "Sorry no implementation for your platform (%s) available." % sys.platform
53
59
"""loop forever and copy serial->console"""
55
sys.stdout.write(s.read())
63
sys.stdout.write(repr(data)[1:-1])
65
sys.stdout.write(data)
58
69
"""loop and copy console->serial until EOF character is found"""
61
if c == EXITCHARCTER: break #exit on esc
62
s.write(c) #send character
63
if convert_outgoing_cr and c == '\r':
65
if echo: sys.stdout.write('\n')
75
if convert_outgoing == CONVERT_CRLF:
76
s.write('\r\n') #make it a CR+LF
77
elif convert_outgoing == CONVERT_CR:
78
s.write('\r') #make it a CR
79
elif convert_outgoing == CONVERT_LF:
80
s.write('\n') #make it a LF
82
s.write(c) #send character
68
85
#print a short help message
70
print >>sys.stderr, """USAGE: %s [options]
71
Simple Terminal Programm for the serial port.
87
sys.stderr.write("""USAGE: %s [options]
88
Miniterm - A simple terminal program for the serial port.
74
-p, --port=PORT: port, a number, defualt = 0 or a device name
91
-p, --port=PORT: port, a number, default = 0 or a device name
75
92
-b, --baud=BAUD: baudrate, default 9600
76
93
-r, --rtscts: enable RTS/CTS flow control (default off)
77
94
-x, --xonxoff: enable software flow control (default off)
78
95
-e, --echo: enable local echo (default off)
79
-c, --cr: disable CR -> CR+LF translation
96
-c, --cr: do not send CR+LF, send CR only
97
-n, --newline: do not send CR+LF, send LF only
98
-D, --debug: debug received data (escape nonprintable chars)
100
""" % (sys.argv[0], ))
83
102
if __name__ == '__main__':
103
#initialize with defaults
107
convert_outgoing = CONVERT_CRLF
84
112
#parse command line options
86
114
opts, args = getopt.getopt(sys.argv[1:],
88
["help", "port=", "baud=", "rtscts", "xonxoff", "echo", "cr"])
116
["help", "port=", "baud=", "rtscts", "xonxoff", "echo",
117
"cr", "newline", "debug"]
89
119
except getopt.GetoptError:
90
120
# print help information and exit:
97
convert_outgoing_cr = 1
100
124
for o, a in opts:
101
if o in ("-h", "--help"): #help text
125
if o in ("-h", "--help"): #help text
104
elif o in ("-p", "--port"): #specified port
128
elif o in ("-p", "--port"): #specified port
107
131
except ValueError:
109
elif o in ("-b", "--baud"): #specified baudrate
133
elif o in ("-b", "--baud"): #specified baudrate
111
135
baudrate = int(a)
112
136
except ValueError:
113
raise ValueError, "Baudrate must be a integer number"
137
raise ValueError, "Baudrate must be a integer number, not %r" % a
114
138
elif o in ("-r", "--rtscts"):
116
140
elif o in ("-x", "--xonxoff"):
118
142
elif o in ("-e", "--echo"):
120
144
elif o in ("-c", "--cr"):
121
convert_outgoing_cr = 0
145
convert_outgoing = CONVERT_CR
146
elif o in ("-n", "--newline"):
147
convert_outgoing = CONVERT_LF
148
elif o in ("-D", "--debug"):
124
153
s = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff)
126
print "could not open port"
155
sys.stderr.write("Could not open port\n")
128
print "--- Miniterm --- type Ctrl-D to quit"
157
sys.stderr.write("--- Miniterm --- type Ctrl-D to quit\n")
129
158
#start serial->console thread
130
159
r = threading.Thread(target=reader)
133
#enter console->serial loop
162
#and enter console->serial loop
136
print "\n--- exit ---"
165
sys.stderr.write("\n--- exit ---\n")