Advertisement
Guest User

readwirte_text1

a guest
Dec 23rd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import MySQLdb
  4. import serial
  5. import time
  6.  
  7.  
  8. #Datos de conexion
  9. DB_HOST = '172.29.1.20'
  10. DB_USER = 'root'
  11. DB_PASS = 'tech123'
  12. DB_NAME = 'Harnes'
  13.  
  14.  
  15. #conexion y ejecucion de query
  16. def run_query(query=''):
  17.     datos = [DB_HOST, DB_USER, DB_PASS, DB_NAME]
  18.     conn = MySQLdb.connect(*datos) # Conectar a la base de datos
  19.     cursor = conn.cursor()         # Crear un cursor
  20.     cursor.execute(query)          # Ejecutar una consulta
  21.  
  22.     if query.upper().startswith('SELECT'):
  23.         data = cursor.fetchall()   # Traer los resultados de un select
  24.     else:
  25.         conn.commit()              # Hacer efectiva la escritura de datos
  26.         data = None
  27.  
  28.     cursor.close()                 # Cerrar el cursor
  29.     conn.close()                   # Cerrar la conexion
  30.  
  31.     return data
  32.  
  33. #leer sennal del puerto serial
  34. def readSignal(objectSerial):
  35.     chain = objectSerial.readline()
  36.     listChain = chain.split(",")
  37.     if ("i" in listChain[0]) and ("f" in listChain[-1]) and (len(listChain) == 7):
  38.         #cadena valida
  39.         return listChain
  40.     else:
  41.         return 0
  42.  
  43. def celsiusFarenheit(celsius):
  44.     return ((float(celsius)*9)/5)+32
  45.  
  46. #main
  47.  
  48. #objectSerial
  49. serialPort = serial.Serial('/dev/ttyS2', baudrate=9600, timeout=3.0)
  50.  
  51.  
  52. while(1):
  53.     try:
  54.         cadena=readSignal(serialPort)      
  55.         if cadena != 0:
  56.                 idDevice=cadena[1]
  57.                 varTemperature = celsiusFarenheit(cadena[2])
  58.                 if varTemperature>30:
  59.                     varTemperature=str(varTemperature)
  60.                     varPressure = cadena[3]
  61.                     varAlture = cadena[4]
  62.                     varEngage = cadena[5]
  63.  
  64.                     run_query("INSERT INTO Insertion(Altura,Enganche,Presion,Temperatura,idDevice) VALUES ("+str(varAlture)+","+str(varEngage)+","+str(varPressure)+","+str(varTemperature)+","+str(idDevice)+")")
  65.                     run_query("INSERT INTO Temperatura(vaTemperatura,idDevice) VALUES ("+str(varTemperature)+","+str(idDevice)+")")
  66.                     run_query("INSERT INTO Presion(vaPresion,idDevice) VALUES ("+str(varPressure)+","+str(idDevice)+")")
  67.                     run_query("INSERT INTO Altura(vaAltura,idDevice) VALUES ("+str(varAlture)+","+str(idDevice)+")")
  68.                     run_query("INSERT INTO Enganche(vaEnganche,idDevice) VALUES ("+str(varEngage)+","+str(idDevice)+")")
  69.                 else: pass
  70.         else:
  71.                 pass
  72.     except:
  73.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement