-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialSample.py
55 lines (48 loc) · 1.29 KB
/
serialSample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/python
import serial
import string
import MySQLdb
db = MySQLdb.connect("192.168.2.5","raspberry","1234","rpi")
curs=db.cursor()
if(db):
print "Database Connection O.K."
else:
print "Databse Connection Fail"
try:
curs.execute ("INSERT INTO log(date,time,exp) values(CURRENT_DATE(),NOW(),'RPI-Connection-Established')")
db.commit()
except:
print "Error : Rollback"
db.rollback()
port = serial.Serial("/dev/ttyAMA0",9600,timeout=1)
port.open()
port.write('HelloArduino')
def readLine(port):
str = ""
while 1:
ch = port.read(1)
if(ch == '\n' or ch == '\r' or ch==''):
break
str += ch
return str
while True:
line = readLine(port)
print line
if(line == 'Password O.K.'):
try:
curs.execute ("INSERT INTO log(date,time,exp) values(CURRENT_DATE(),NOW(),'Password Correct Alarm Disabled')")
db.commit()
except:
print "Error : Rollback"
db.rollback()
else:
if(line == 'stop'):
print "Stoped"
port.close()
if(line == 'Password Fail'):
try:
curs.execute ("INSERT INTO log(date,time,exp) values(CURRENT_DATE(),NOW(),'Acces Failure Alarm Actived')")
db.commit()
except:
print "Error : Rollback"
db.rollback()