lundi 9 mars 2015

Why Does my code never finish

i have this code, the object is to read one line of serial and then add it to the database if it is valid, the code that sends the serial is terminated with \n when this code was in two scripts it worked as it should, trying to push it together has somehow messed it up heres the code:



#GKPCM Database Test


#outline open database, read serial,write string, repeat
import serial
import sqlite3

ser = serial.Serial('/dev/pts/2', 19200, timeout=0)
print ser.name # check which port was really used

db = sqlite3.connect('Data/telemetry.gkpcm')
cursor = db.cursor()

InsertQuery ="""INSERT INTO vehicletelemetry (date,time,cyclecount,rpm,speed,odometer,oiltemp,airtemp,fuellevel,enginetemp,ind1,ind2,ind3,ind4,ind5,ind6,ind7,ind8) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"""

tablename="vehicletelemetry"
cursor.execute(""" SELECT COUNT(*) FROM sqlite_master WHERE name = ? """, (tablename, ))
QUERY = cursor.fetchone()
print bool(QUERY[0]) # True if exists
if bool(QUERY[0]) !=1:
print('not in DB')
cursor.execute('''CREATE TABLE vehicletelemetry(id INTEGER PRIMARY KEY, date INTEGER,time INTEGER, cyclecount INTEGER, rpm INTEGER, speed INTEGER, odometer INTEGER, oiltemp INTEGER, airtemp INTEGER, fuellevel INTEGER, enginetemp INTEGER, ind1 BOOL, ind2 BOOL, ind3 BOOL, ind4 BOOL, ind5 BOOL, ind6 BOOL, ind7 BOOL, ind8 BOOL)''')
cursor.execute('INSERT INTO vehicletelemetry (date,time,cyclecount,rpm,speed,odometer,oiltemp,airtemp,fuellevel,enginetemp,ind1,ind2,ind3,ind4,ind5,ind6,ind7,ind8) VALUES (031514,013030,18960,3000,22,192768,210,72,98,210,0,0,0,0,0,0,0,0)')
db.commit()
else:
print('DB Table Exists')

#openfile to read


while(1):
DataLine = ser.readline()
Valid = bool(DataLine)
if Valid == True:
print(DataLine)
#Read Database for last record date
LastEntry = cursor.execute('SELECT * FROM vehicletelemetry ORDER BY id DESC LIMIT 1')
for Records in LastEntry:
LastLogDate = Records[1]
LastLogTime = Records[2]
LastLogCycles = Records[3]
DataLine1 = DataLine.strip()
DataLine2 = DataLine1.split(',')
print(DataLine2)
#Check Packet for Correct Length
if len(DataLine2) != 20:
print (len(DataLine2))
print ("Invalid Data Length")
else:
#Check Packet DataQualifiers to ensure proper package introduction and termination
if DataLine2[0] != "7887" and DataLine2[18] != "0420":
print ("Invalid Data Qulifier")
else:
#Remove Qualifiers So data can be stored
PiP = 1
DataLine3 = []
for Packet in DataLine2:
if PiP >= 1 and PiP <= 18:
DataLine3.append(DataLine2[PiP])
PiP = PiP + 1
#Compare Date Time and Cycle Count to Current Record
#print(DataLine3)
if int(DataLine2[1]) >= int(LastLogDate):
if int(DataLine2[2]) >= int(LastLogTime):
if int(DataLine2[3]) > int(LastLogCycles):
cursor.execute(InsertQuery,DataLine3)
db.commit()
print(Records,DataLine2[3],LastLogCycles,"Data Valid")

db.close()


what the code does is it gets to checking the data for length and even if it is the right length to continue down the script the code goes back to ser.readline() nothing is ever wrote to the database.


Aucun commentaire:

Enregistrer un commentaire