~ubuntu-branches/ubuntu/wily/seabios/wily-proposed

« back to all changes in this revision

Viewing changes to scripts/readserial.py

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2015-06-04 16:06:25 UTC
  • mfrom: (23.2.1 sid)
  • Revision ID: package-import@ubuntu.com-20150604160625-luaye2ktyn264doa
Tags: 1.8.1-2ubuntu1
* New debian release.  Remaining change:
  - Build with -fgnu89-inline for GCC 5 (doko)

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
# This file may be distributed under the terms of the GNU GPLv3 license.
7
7
 
8
8
# Usage:
9
 
#   tools/readserial.py /dev/ttyUSB0 115200
 
9
#   scripts/readserial.py /dev/ttyUSB0 115200
10
10
 
11
 
import sys
12
 
import time
13
 
import select
14
 
import optparse
 
11
import sys, os, time, select, optparse
15
12
 
16
13
from python23compat import as_bytes
17
14
 
65
62
            res = select.select([infile, sys.stdin], [], [])
66
63
        except KeyboardInterrupt:
67
64
            sys.stdout.write("\n")
68
 
            break
 
65
            return -1
69
66
        if sys.stdin in res[0]:
70
67
            # Got keyboard input - force reset on next serial input
71
68
            sys.stdin.read(1)
74
71
                continue
75
72
        d = infile.read(4096)
76
73
        if not d:
77
 
            break
 
74
            return 0
78
75
        datatime = time.time()
79
76
 
80
77
        datatime -= len(d) * byteadjust
128
125
    opts = optparse.OptionParser(usage)
129
126
    opts.add_option("-f", "--file",
130
127
                    action="store_false", dest="serial", default=True,
131
 
                    help="read from file instead of serialdevice")
 
128
                    help="read from unix named pipe instead of serialdevice")
132
129
    opts.add_option("-n", "--no-adjust",
133
130
                    action="store_false", dest="adjustbaud", default=True,
134
131
                    help="don't adjust times by serial rate")
168
165
""")
169
166
            sys.exit(1)
170
167
        ser = serial.Serial(serialport, baud, timeout=0)
171
 
    else:
172
 
        # Read from a file
173
 
        ser = open(serialport, 'rb')
174
 
        import fcntl
175
 
        import os
176
 
        fcntl.fcntl(ser, fcntl.F_SETFL
177
 
                    , fcntl.fcntl(ser, fcntl.F_GETFL) | os.O_NONBLOCK)
178
168
 
179
169
    if options.calibrate_read:
180
170
        calibrateserialread(ser, byteadjust)
185
175
 
186
176
    logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
187
177
    f = open(logname, 'wb')
188
 
    readserial(ser, f, byteadjust)
 
178
    if options.serial:
 
179
        readserial(ser, f, byteadjust)
 
180
    else:
 
181
        # Read from a pipe
 
182
        while 1:
 
183
            ser = os.fdopen(os.open(serialport, os.O_RDONLY|os.O_NONBLOCK), 'rb')
 
184
            res = readserial(ser, f, byteadjust)
 
185
            ser.close()
 
186
            if res < 0:
 
187
                break
189
188
 
190
189
if __name__ == '__main__':
191
190
    main()