readserial: Improve Python3 compatibility

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-12-19 09:44:13 -05:00
parent 98dd53b994
commit 9ad4399b0e
1 changed files with 11 additions and 17 deletions

View File

@ -10,8 +10,6 @@
import sys, os, time, select, optparse import sys, os, time, select, optparse
from python23compat import as_bytes
# Reset time counter after this much idle time. # Reset time counter after this much idle time.
RESTARTINTERVAL = 60 RESTARTINTERVAL = 60
# Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
@ -20,11 +18,11 @@ BITSPERBYTE = 10
def calibrateserialwrite(outfile, byteadjust): def calibrateserialwrite(outfile, byteadjust):
# Build 4000 bytes of dummy data. # Build 4000 bytes of dummy data.
data = "0123456789" * 4 + "012345678" + "\n" data = b"0123456789" * 4 + b"012345678" + b"\n"
data = data * 80 data = data * 80
while 1: while 1:
st = time.time() st = time.time()
outfile.write(as_bytes(data)) outfile.write(data)
outfile.flush() outfile.flush()
et = time.time() et = time.time()
sys.stdout.write( sys.stdout.write(
@ -84,38 +82,34 @@ def readserial(infile, logfile, byteadjust):
msg = "\n\n======= %s (adjust=%.1fus)\n" % ( msg = "\n\n======= %s (adjust=%.1fus)\n" % (
time.asctime(time.localtime(datatime)), byteadjust * 1000000) time.asctime(time.localtime(datatime)), byteadjust * 1000000)
sys.stdout.write(msg) sys.stdout.write(msg)
logfile.write(as_bytes(msg)) logfile.write(msg.encode())
lasttime = datatime lasttime = datatime
# Translate unprintable chars; add timestamps # Translate unprintable chars; add timestamps
out = as_bytes("") out = bytearray()
for c in d: for oc in bytearray(d):
if isnewline: if isnewline:
delta = datatime - starttime - (charcount * byteadjust) delta = datatime - starttime - (charcount * byteadjust)
out += "%06.3f: " % delta out += b"%06.3f: " % delta
isnewline = 0 isnewline = 0
oc = ord(c)
charcount += 1 charcount += 1
datatime += byteadjust datatime += byteadjust
if oc == 0x0d: if oc == 0x0d:
continue continue
if oc == 0x00: if oc == 0x00:
out += "<00>\n" out += b"<00>\n"
isnewline = 1 isnewline = 1
continue continue
if oc == 0x0a: if oc == 0x0a:
out += "\n" out += b"\n"
isnewline = 1 isnewline = 1
continue continue
if oc < 0x20 or oc >= 0x7f and oc != 0x09: if oc < 0x20 or oc >= 0x7f and oc != 0x09:
out += "<%02x>" % oc out += b"<%02x>" % oc
continue continue
out += c out.append(oc)
if (sys.version_info > (3, 0)): sys.stdout.write(out.decode())
sys.stdout.buffer.write(out)
else:
sys.stdout.write(out)
sys.stdout.flush() sys.stdout.flush()
logfile.write(out) logfile.write(out)
logfile.flush() logfile.flush()