Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import serial
  2. import time
  3. import _mysql
  4. import sys
  5.  
  6. ser = serial.Serial('COM6', 9600, timeout=0)
  7.  
  8. try:
  9.      con = _mysql.connect('localhost', 'temp', 'temp', 'temp')
  10. except _mysql.Error, e:
  11.      print "Error %d: %s" % (e.args[0], e.args[1])
  12.      sys.exit(1)
  13.  
  14. while 1:
  15.      try:
  16.           data = ser.readline()
  17.  
  18.      except ser.SerialTimeoutException:
  19.           print('Data could not be read')
  20.           time.sleep(1)
  21.  
  22.      if data:
  23.           data = data.replace('\n', '')
  24.           data_array = data.split(',')
  25.                                                          
  26.           print('Temp: ' + data_array[0])
  27.           print('Addr: ' + data_array[1])
  28.           print('Type: ' + data_array[2])
  29.  
  30.           query = 'INSERT INTO temp (temp,adresse,type) VALUES ("'+data_array[0]+'","'+data_array[1]+'","'+data_array[2]+'")'
  31.           #print(query)
  32.           con.query(query)
  33.              
  34.      time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement