#!/usr/bin/env python3 # -*- coding: utf_8 -*- """ spawned from TSF file write Aim is to get serial ascii Baro data into tsf format """ # Only works with python 3 import serial import os.path import time import datetime # PORT name -- Change as needed for your host. #PORT_NAME = '/dev/ttyACM0' #PORT_NAME = '/dev/ttyUSB0' PORT_NAME = 'COM4' BAUD_RATE ='9600' serialPort = serial.Serial( port=PORT_NAME, baudrate=BAUD_RATE, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE ) #TSF file name, header, UT etc def LOG_file_name(): # uses the same utc.now from timing loop todayUTyear=myut.year todayUTmonth=myut.month todayUTday=myut.day logFileName =("%4.2d%2.2d%2.2d%s") % (todayUTyear,todayUTmonth,todayUTday, "_baro.tsf") #print(logFileName) return (logFileName) def UT_string(): myut=datetime.datetime.utcnow() # uses the same utc.now from timing loop todayUTyear=myut.year todayUTmonth=myut.month todayUTday=myut.day todayUThour=myut.hour todayUTminute=myut.minute todayUTsecond=myut.second myutstring="%4.2d %2.2d %2.2d %2.2d %2.2d %2.2d " % ( todayUTyear,todayUTmonth,todayUTday,todayUThour,todayUTminute,todayUTsecond) #print(myutstring) return (myutstring) def createHeader(): file_exists = os.path.exists(logFileName) # print(file_exists) # if it doesnt, open a file and write header if file_exists == False: myfile1 = open(logFileName, 'a') myfile1.write("[TSF-file] v01.0\n") myfile1.write("\n") myfile1.write("\n") myfile1.write("[UNDETVAL] 99999\n") myfile1.write("\n") myfile1.write("[TIMEFORMAT] DATETIME\n") myfile1.write("\n") myfile1.write("[INCREMENT] 1\n") #1 seconds myfile1.write("\n") myfile1.write("[CHANNELS]\n") myfile1.write(" South Aust:TPSO:barometer\n") myfile1.write(" South Aust:TPSO:temperature\n") myfile1.write(" South Aust:TPSO:humidity\n") myfile1.write("\n") myfile1.write("[UNITS]\n") myfile1.write(" Pa\n") myfile1.write(" tC\n") myfile1.write(" %\n") myfile1.write("\n") myfile1.write("[COMMENT]\n") myfile1.write("\n") myfile1.write("[DATA]\n") # data in format date,year, ,month, ,day, ,hh, ,mm, ,ss, data(nT) :- # 2022 06 03 00 00 00 21.4 47.5 # close the log file myfile1.close() print("created ",logFileName) return serialString = "" # Used to hold data coming over UART while 1: # Wait until there is data waiting in the serial buffer if serialPort.in_waiting > 0: # Read data out of the buffer until a carraige return / new line is found serialString = serialPort.readline() myut=datetime.datetime.utcnow() # Print the contents of the serial data try: logFileName = LOG_file_name() # get a file name for 'today' file_exists = os.path.exists(logFileName) # print(file_exists) # if it doesnt, open a file and write header if file_exists == False: createHeader() myBaroStr= serialString.decode("Ascii") myBaroStr=myBaroStr.rstrip() ##rstrip() removes all white space at end of string (crlf).. #print(UT_string(),myBaroStr.rstrip()) print(UT_string(),myBaroStr) myfile1 = open(logFileName,'a') myfile1.write(UT_string()) #.write only has one assignment, so the need to write each string #myfile1.write(myBaroStr.rstrip()) #rstrip() removes all white space at end of string (crlf).. myfile1.write(myBaroStr) myfile1.write("\n") myfile1.close() #myut=datetime.datetime.utcnow() # 'MASTER' get time, used in UT_string() and LOG_file_name() #print(serialString.decode("Ascii")) #print(UT_string(),serialString.decode("Ascii")) #myut=datetime.datetime.utcnow() # 'MASTER' get time, used in UT_string() and LOG_file_name() #print(myut) # to test how often it goes through loop except: pass