You can use a simple timer to stop the loop. I cleaned up the resource management a bit, context managers are really useful.
import threading
from contextlib import closing
import serial
continue_looping = True
def stopper():
global continue_looping
continue_looping = False
timer = threading.Timer(60, stopper)
timer.start()
with open("/Users/macproretina/Desktop/data.txt", 'w') as out_file:
with closing(serial.Serial('/dev/tty.usbmodem1411', 9600, timeout=1)) as ser:
while continue_looping:
line = ser.readline() # read a '\n' terminated line
out_file.write(line.decode('utf-8')
out_file.flush()
It might be off a bit due to serial timeouts. Notice that you get the output written to the file if you call f.flush()
in case you need that.