I am using a rpi pico to collect temperature measurements which are saved to a local file. I additionally want,when the pico is connected via usb to PC, to output them on stdout. I have tried this:
f=open('temps.txt','a+')
while True:
temps=getTemperatures()
f.write(temps)
print(temps)
If the print(temps)
line is commented out, measurements are collected as long as pico is powered, whether or not the PC is on or in standby. But with the print
line uncommented, measurements are saved only if the PC is on. It seems the print
blocks waiting for the connection to be re-established. How can I detect that the connection is active, so that print
is executed only when it will not block?