OpenCores
URL https://opencores.org/ocsvn/openmsp430/openmsp430/trunk

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [fpga/] [xilinx_diligent_s3board/] [software/] [ta_uart/] [miniterm.py] - Blame information for rev 211

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 olivier.gi
#!/usr/bin/env python
2
#very simple serial terminal
3
#http://pyserial.sf.net package required
4
#input characters are sent directly, received characters are displays as is
5
#baudrate and echo configuartion is done through globals:
6
 
7
#<cliechti@gmx.net>
8
 
9
import sys, os, serial, threading, getopt
10
#EXITCHARCTER = '\x1b'   #ESC
11
EXITCHARCTER = '\x04'   #ctrl+d
12
 
13
#first choosea platform dependant way to read single characters from the console
14
if os.name == 'nt': #sys.platform == 'win32':
15
    import msvcrt
16
    def getkey():
17
        while 1:
18
            if echo:
19
                z = msvcrt.getche()
20
            else:
21
                z = msvcrt.getch()
22
            if z == '\0' or z == '\xe0':    #functions keys
23
                msvcrt.getch()
24
            else:
25
                return z
26
 
27
elif os.name == 'posix':
28
    #XXX: Untested code derrived from the Python FAQ....
29
#    import termios, TERMIOS, sys, os
30
    import termios, sys, os
31
    fd = sys.stdin.fileno()
32
    old = termios.tcgetattr(fd)
33
    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)
38
    s = ''    # We'll save the characters typed and add them to the pool.
39
    def getkey():
40
        c = os.read(fd, 1)
41
        if echo: sys.stdout.write(c)
42
        return c
43
    def clenaup_console():
44
        termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
45
    sys.exitfunc = clenaup_console  #terminal modes have to be restored on exit...
46
 
47
else:
48
    raise "Sorry no implementation for your platform (%s) available." % sys.platform
49
 
50
 
51
def reader():
52
    """loop forever and copy serial->console"""
53
    while 1:
54
        sys.stdout.write(s.read())
55
 
56
def writer():
57
    """loop forever and copy console->serial"""
58
    while 1:
59
        c = getkey()
60
        if c == EXITCHARCTER: break   #exit on esc
61
        s.write(c)              #send character
62
        if convert_outgoing_cr and c == '\r':
63
            s.write('\n')
64
            if echo: sys.stdout.write('\n')
65
 
66
 
67
#print a short help message
68
def usage():
69
    print >>sys.stderr, """USAGE: %s [options]
70
    Simple Terminal Programm for the serial port.
71
 
72
    options:
73
    -p, --port=PORT: port, a number, defualt = 0 or a device name
74
    -b, --baud=BAUD: baudrate, default 9600
75
    -r, --rtscts:    enable RTS/CTS flow control (default off)
76
    -x, --xonxoff:   enable software flow control (default off)
77
    -e, --echo:      enable local echo (default off)
78
    -c, --cr:        disable CR -> CR+LF translation
79
 
80
    """ % sys.argv[0]
81
 
82
if __name__ == '__main__':
83
    #parse command line options
84
    try:
85
        opts, args = getopt.getopt(sys.argv[1:],
86
                "hp:b:rxec",
87
                ["help", "port=", "baud=", "rtscts", "xonxoff", "echo", "cr"])
88
    except getopt.GetoptError:
89
        # print help information and exit:
90
        usage()
91
        sys.exit(2)
92
 
93
    port  = 0
94
    baudrate = 9600
95
    echo = 0
96
    convert_outgoing_cr = 1
97
    rtscts = 0
98
    xonxoff = 0
99
    for o, a in opts:
100
        if o in ("-h", "--help"):   #help text
101
            usage()
102
            sys.exit()
103
        elif o in ("-p", "--port"):   #specified port
104
            try:
105
                port = int(a)
106
            except ValueError:
107
                port = a
108
        elif o in ("-b", "--baud"):   #specified baudrate
109
            try:
110
                baudrate = int(a)
111
            except ValueError:
112
                raise ValueError, "Baudrate must be a integer number"
113
        elif o in ("-r", "--rtscts"):
114
            rtscts = 1
115
        elif o in ("-x", "--xonxoff"):
116
            xonxoff = 1
117
        elif o in ("-e", "--echo"):
118
            echo = 1
119
        elif o in ("-c", "--cr"):
120
            convert_outgoing_cr = 0
121
 
122
    try:
123
        s = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff)
124
    except:
125
        print "could not open port"
126
        sys.exit(1)
127
    print "--- Miniterm --- type Ctrl-D to quit"
128
    #start serial->console thread
129
    r = threading.Thread(target=reader)
130
    r.setDaemon(1)
131
    r.start()
132
    #enter console->serial loop
133
    writer()
134
 
135
    print "\n--- exit ---"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.