Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I make project read RFID tag using python on raspberry pi and using reader RDM880.
My idea is to take the time in and time out to check with the staff to work on time or not.
I try to add card_ID, time_in, time_out to local mysql and remote mysql (IP: 192.168.137.1) using python.
I have the same table in remote and local mysql.
With my code, If server is not broken, it will add Card_ID, Time_in, Time_out to server and client. But If server broken, it error and stop.
I want when the server is broken, code still works and skip add data to server. It only adds to client.
Please help me. Thanks. (sorry, my english is not good)
Here is my code:
Python
import serial
import time
import RPi.GPIO as GPIO
import MySQLdb
from datetime import datetime
from binascii import hexlify
serial=serial.Serial("/dev/ttyAMA0",                    
                  baudrate=9600,                     
                  parity=serial.PARITY_NONE,                    
                  stopbits=serial.STOPBITS_ONE,                    
                  bytesize=serial.EIGHTBITS,
                  timeout=0.1) 
db_local = MySQLdb.connect("localhost","root","root","luan_van") #connect local
db = MySQLdb.connect("192.168.137.1", "root_a","","luan_van") #connect remote
ID_rong = 128187 # reader respone if no card
chuoi= "\xAA\x00\x03\x25\x26\x00\x00\xBB"
def RFID(str): #function read RFID via uart 
      serial.write(chuoi)
      data = serial.readline()
      tach_5 = data[5]
      tach_6 = data[6]
      hex_5 = hexlify(tach_5)
      hex_6= hexlify(tach_6)
      num_5 = int(hex_5,16)
      num_6 = int(hex_6,16)
      num_a = num_5 * 1000 + num_6
      if(num_a != ID_rong):
            tach_7 = data[7]
            tach_8 = data[7]
            hex_7 = hexlify(tach_7)
            hex_8= hexlify(tach_8)
            num_7 = int(hex_7,16)
            num_8 = int(hex_8,16)
            num = num_8 + num_7 * 1000 + num_6 * 1000000 + num_5 * 1000000000
      else:
            num = num_5 * 1000 + num_6
      return num
def add_database(): # add card_ID and time_in to remote mysql
      with db:
           cur = db.cursor()
           cur.execure("INSERT INTO tt_control(Card_ID,Time_in) VALUES ('%d',NOW()) " %num)
           return
def add_database_local(): # add card_ID and time_in to remote mysql
      with db_local:
           cur = db_local.cursor()
           cur.execure("INSERT INTO tt_control(Card_ID,Time_in) VALUES ('%d',NOW()) " %num)
           return
def have_ID(int): #check ID in table tt_control
       with db_local:
           cur = db_local.cursor(MySQLdb.cursors.DictCursor)
           cur.execute("SELECT * FROM tt_control WHERE Card_ID = '%d'" %num)
           rows = cur.fetchall()
           ID=""
           for row in rows:
               ID = row['Card_ID']
       return ID
def add_time_out(): #add time out to remote mysql
       with db:
           cur = db.cursor(MySQLdb.cursors.DictCursor)
           cur.execute("UPDATE tt_control SET Time_out = NOW() WHERE Card_ID = '%d'" %num)
       return
def add_time_out_local(): #add time out to local mysql
       with db_local:
           cur = db_local.cursor(MySQLdb.cursors.DictCursor)
           cur.execute("UPDATE tt_control SET Time_out = NOW() WHERE Card_ID = '%d'" %num)
       return

  def add_OUT(): #increase Card_ID to distinguish second check
     with db:
           cur = db.cursor(MySQLdb.cursors.DictCursor) 
           cur.execute("UPDATE tt_control SET Card_ID = Card_ID + 1 WHERE Card_ID = '%d'" %num)
     return
  def add_OUT_local(): #increase Card_ID to distinguish second check
     with db_local:
           cur = db_local.cursor(MySQLdb.cursors.DictCursor) 
           cur.execute("UPDATE tt_control SET Card_ID = Card_ID + 1 WHERE Card_ID =  '%d'" %num)
     return
  while 1:
     num = RFID(chuoi)
     time.sleep(1)
     Have_ID =have_ID(num)
     if(num != ID_rong):
            if(Have_ID ==""):
                 add_database() #---> it will error if remote broken, how can i fix it?
                 add_database_local()
            else:
                 add_time_out() #---> it will error if remote broken, how can i skip it and my code still works? 
                 add_time_out_local()
                 add_OUT()#---> it will error if remote broken, how can i skip it and my code still works? 
                 add_OUT_local() 
Posted
Updated 4-Nov-14 20:46pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900