1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#!/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() |
I'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