0

i got this 2 python script

import serial
serial = serial.Serial("/dev/ttyUSB0", baudrate=9600)

code = ''

while True:
        data = serial.read()
        if data == '\r':
                print(code)
                code = ''
        else:
                code = code + data

and

import time
import datetime
import MySQLdb

localtime = time.localtime(time.time())

day = localtime.tm_wday
time = localtime.tm_hour

print day
print time

data = 'DOSEN1'

db = MySQLdb.connect("localhost", "root", "", "skripsi")
cur = db.cursor()

cond1 = "SELECT nama_dosen FROM dosen WHERE kode_dosen = '%s'" %data
cur.execute(cond1)
hitung = cur.rowcount
res1 = cur.fetchall()
for row in res1:
        nama_dosen = row[0]
if hitung == 1:
        res1 = nama_dosen
elif hitung != 1:
        print "Dosen tidak Terdaftar"

how can i join this 2 script so that the data = 'DOSEN1' can be replaced with the RFID tag number?

i really new to this programming languange, really need help. thanks

1 Answers1

0

Assuming that print(code) gives you the value for data in the second script, something like this should work:

import serial
import time
import datetime
import MySQLdb

serial = serial.Serial("/dev/ttyUSB0", baudrate=9600)

db = MySQLdb.connect("localhost", "root", "", "skripsi")
cur = db.cursor()

code = ''

while True:
    data = serial.read()
    if data == '\r':
        print(code)

        localtime = time.localtime(time.time())
        day = localtime.tm_wday
        time = localtime.tm_hour
        print day
        print time

        cond1 = "SELECT nama_dosen FROM dosen WHERE kode_dosen = '%s'" %code
        cur.execute(cond1)
        hitung = cur.rowcount
        res1 = cur.fetchall()
        for row in res1:
            nama_dosen = row[0]
        if hitung == 1:
            res1 = nama_dosen
        elif hitung != 1:
            print "Dosen tidak Terdaftar"

        code = ''
    else:
        code = code + data
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • this combine is working, but did the code is string type? the code in the serial is 260010BC1D97, ive create that in my database, but the output still "Dosen tidak terdaftar", that means the %code is not same as serial.read. any ideas? thanks btw – Fadhlulrahman Azis Jun 24 '15 at 19:15
  • You would need to provide further details on the output format of the reader and the value in your table to further debug this. WHat you could do to check the value is to replace the SELECT query with an INSERT query and check what value you find in the database after scanning. – Michael Roland Jun 24 '15 at 19:40
  • after i use INSERT i got his results on my database : kode_dosen | nama_dosen 260010BC1D7 | 260010BC1D7 | DOSEN1 is there blank space on my database ive created before? h – Fadhlulrahman Azis Jun 25 '15 at 06:09