[Tutor] A finished serial line telemetry, data log file generator program. Thanks for the groups help!

Bill Gillespie billintucson@yahoo.com
Sun Apr 20 16:11:01 2003


Hi Folks,

Thanks much for the help I had a couple of months back on ideas for
reading a serial line, and then creating a log file from the data the
microcontroller returns via the serail line.

Here's the code that's working now: any advice on improvements is
always welcome. A question regarding these emails; is this list
archived and searchable from some URL? 

Thanks again,

Bill



#! /usr/local/bin/python

        #**********************************************************#
        #                                                          #
        #      4m OFTC Logging Program, Bill Gillespie, NOAO.      #
        #                    March 20, 2002                        #
        #                                                          #
        #       **********************************************     #
        #                                                          #
        #  This program's basic function is to query and log data #
        #  strings from the 4 meter telescope facilities           #
        #  Oil and Floor Temperature Microcontroller.              #
        #                                                          #
        #**********************************************************#
               
#-------  Fetch modules  ----#
#-------  the pyserial module had to be installed for this program --#

import serial, os, sys, time, string


#-------  Get Time String
---------------------------------------------------#

timenow = time.ctime(time.time())
timenow = timenow[4:25]


#-------- Set up the serial line for R/W -------#

dev=serial.Serial('/dev/tty***', 4800, 7, 1, timeout=1)
dev.flush()


#-------- Fetch the main values with oftc device calls ------#

dev.write('oftv\r')
OFTV = dev.readlines()
OFTV = `OFTV` # OFTV converted from object to a string
dev.flush()

dev.write('osetpoint?\r')
Oset = dev.readlines()
Oset = `Oset` # Oset is now a string (was object)
dev.flush()

dev.write('fsetpoint?\r')
Fset = dev.readlines()
Fset = `Fset`  # ditto above
dev.flush()


#-------  Print the TIME string to the log file --------#

LogFile = open("/usr/local/gui/oftc/oftc-log-file.txt", "a")
LogFile.write("%-22s" % (timenow))


#-------  Clean data strings of unwanted characters --------#

OFTV = OFTV.replace(","," ")
OFTV = OFTV.replace("'"," ")

Oset = Oset.replace(","," ")
Oset = Oset.replace("'"," ")

Fset = Fset.replace(","," ")
Fset = Fset.replace("'"," ")


#-------  Parse strings and print log data to log file -------#

OilSetPt = string.split(Oset)      # splits the string into list based
on white space.
LogFile.write("%5s" % OilSetPt[2]) # prints new list item 1,to log
file.

(OGS, OGR, OSX, OXR, OPR, OSP, AMP, spr, tb1) =
string.split(OFTV)[12:21]
LogFile.write("%5s %5s %5s %5s %5s %5s %5s %5s %8s" % (OGS, OGR, OSX,
OXR, OPR, OSP, AMP, spr, tb1))

FloorSetPt = string.split(Fset)      # splits the string into list
based on white space.
LogFile.write("%5s" % FloorSetPt[2]) # prints new list item 1, to the
log file.

(FGS, FGR, FT1, FT2, FT3, spr, spr, spr, tb2) =
string.split(OFTV)[32:41]
LogFile.write("%5s %5s %5s %5s %5s %5s %5s %5s %8s" % (FGS, FGR, FT1,
FT2, FT3, spr, spr, spr, tb2))

LogFile.write("\n")
LogFile.close()


#-------  flush and close the serial line --------#

dev.flush()
dev.close()

 


__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com