Skip to content

Instantly share code, notes, and snippets.

@jrleeman
Last active August 8, 2018 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrleeman/b3e9c238c239e9c5831ca541fdd05143 to your computer and use it in GitHub Desktop.
Save jrleeman/b3e9c238c239e9c5831ca541fdd05143 to your computer and use it in GitHub Desktop.
import serial
from datetime import datetime
def get_infrasound_reading():
data = int(ser.readline())
time = datetime.strftime(datetime.utcnow(), '%m-%d-%YT%H:%M:%S.%f')
return time, data
def get_new_hourly_file():
current_file_time = datetime.utcnow()
fname = datetime.strftime(current_file_time, '%Y%m%dT%H.txt')
print(fname)
return open(fname, 'a'), current_file_time
ser = serial.Serial('COM10')
ser.flush()
f, current_file_time = get_new_hourly_file()
# Read/write main loop
try:
while True:
try:
time, data = get_infrasound_reading()
f.write('%s, %d\n' %(time, data))
except ValueError:
print('Format Error')
# If it's time for a new hourly file
if datetime.utcnow().hour != current_file_time.hour:
f.close()
f, current_file_time = get_new_hourly_file()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment