#!/usr/bin/python
import serial, time, sys, fileinput
#open and configure serial port
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=19200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = .1
)
#first, clear out buffer
count = 0
while (count < 3):
count +=1
ser.write("\r".encode())
time.sleep(.1)
#open file for download
file = open("download.mem", "w+")
#send command to start download
ser.write("1SendEram\r\n".encode())
indata =""
Counter = 0
progresscounter = 0
var = 1
while var==1:
indata = ser.readline()
# print indata
if indata[0] =="C": #check for first character of "Complete" and exit loop
break
if(len(indata) == 0 or indata[0] =="-" or indata[0] == "+" or indata[0] =="T"):
Counter = Counter + 1
else:
Counter = 0
ser.write("\r".encode())
file.write(indata)
#LineCount -= 1
progresscounter += 1
progress = progresscounter / 44
if( progress > 100 ) : progress = 100
ser.write("OK\r\n")
print( '\rDownloading: %s (%d%%)' % ("|"*(progress/2), progress)),
sys.stdout.flush()
if Counter > 10:
file.close()
sys.exit("Unit did not respond. Exiting")
print ("\nDownload Complete")
file.close()
sys.exit()The above code was originally written for Python2.7 and I am upgrading to Python3. I'm slowly working my way down the file (I've corrected various errors as I work my way down) but I'm stuck at the line "file.write(indata)". indata is read from the serial port and I want to write it to the file (download.mem). Python3 throws "TypeError: write() argument must be str, not bytes". This is the file.write statement on line 44I'm not a total newbie to Python but I kinda am to Python3. I don't see how to cast "indata" as a string so it will write to the file
