文章目录
- test.py
- 界面展示
- pf.py
- fpga.py
test.py
import subprocess
import os
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit, QWidget, QLabel
import mysql.connector
from PySide2.QtGui import QPixmap, QPalette, QColor
conn = mysql.connector.connect(
host="localhost",
user="root",
password="root",
database="test1"
)
app = QApplication([])
def handleCalc():
cursor = conn.cursor()
info1 = textEdit.toPlainText()
info2 = textEdit1.toPlainText()
info3 = textEdit2.toPlainText()
info4 = textEdit3.toPlainText()
info5 = textEdit4.toPlainText()
query = "INSERT INTO yangexaminations (name, tel, sfid, sex, birthday) VALUES (%s, %s, %s, %s, %s)"
data = (info1, info2, info3, info4, info5)
cursor.execute(query, data)
conn.commit()
subprocess.run(["python", "F:\\python628\\fpgack.py"])
def handleCalc1():
subprocess.run(["python", "F:\\python628\\pf.py"])
cursor = conn.cursor()
cursor.execute("SELECT score FROM yangscore")
results = cursor.fetchall()
score = [str(result[0]) for result in results]
score_text = '\n'.join(score)
textEdit5.setPlainText(score_text)
window = QMainWindow()
window.resize(500, 400)
window.move(300, 310)
window.setWindowTitle('驾驶考证倒车入库自动评分系统')
background_image_path = os.path.join("F:\\python628", "S.jpg")
background_image = QPixmap(background_image_path)
background_label = QLabel(window)
background_label.setPixmap(background_image)
background_label.setGeometry(0, 0, window.width(), window.height())
textEdit = QPlainTextEdit(window)
textEdit.setPlaceholderText("姓名")
textEdit.move(5,5)
textEdit.resize(50,50)
textEdit1 = QPlainTextEdit(window)
textEdit1.setPlaceholderText("电话")
textEdit1.move(100,5)
textEdit1.resize(50,50)
textEdit2 = QPlainTextEdit(window)
textEdit2.setPlaceholderText("身份证")
textEdit2.move(200,5)
textEdit2.resize(50,50)
textEdit3 = QPlainTextEdit(window)
textEdit3.setPlaceholderText("性别")
textEdit3.move(300,5)
textEdit3.resize(50,50)
textEdit4 = QPlainTextEdit(window)
textEdit4.setPlaceholderText("出生日期")
textEdit4.move(400,5)
textEdit4.resize(50,50)
textEdit5 = QPlainTextEdit(window)
textEdit5.setPlaceholderText("成绩")
textEdit5.move(200,300)
textEdit5.resize(100,50)
button = QPushButton('考试', window)
button.move(200,100)
button.clicked.connect(handleCalc)
button1 = QPushButton('查看成绩', window)
button1.move(200,200)
button1.clicked.connect(handleCalc1)
window.show()
app.exec_()
界面展示
pf.py
import mysql.connector
import datetime
import serial
import socket
import time
import threading
import _thread
import threading
# 连接数据库
conn = mysql.connector.connect(
host="localhost",
user="root",
password="root",
database="test1"
)
value1 = 0
value2 = 0
value3 = 0
while True:
cursor = conn.cursor()
query = "SELECT id FROM yangdatetest1 WHERE id = 540"
cursor.execute(query)
# 获取查询结果
result = cursor.fetchone()
# 判断是否存在指定的 id
if result is not None:
# 设置第一次成绩所需要的变量
prev_distance = float("inf") # 初始化为正无穷大
is_decreasing = True # 初始状态为递减
is_valid_range = True # 初始状态为满足范围条件
# 进行第一次成绩判断
for id_value in range(1, 181):
# 执行查询语句
query = "SELECT distance FROM yangdatetest1 WHERE id = %s"
cursor.execute(query, (id_value,))
# 获取查询结果
result = cursor.fetchone()
if result is not None:
distance = result[0]
if distance > prev_distance:
is_decreasing = False # 递减状态更新为False
prev_distance = distance
if distance <= 2 or distance >= 100000:
is_valid_range = False
if is_decreasing and is_valid_range:
value1 = 3
print("第一次成绩为:优秀")
elif is_decreasing or is_valid_range:
value1 = 2
print("第一次成绩为:合格")
else:
value1 = 1
print("第一次成绩为:异常")
# 设置第二次成绩所需要的变量
prev_distance2 = float("inf") # 初始化为正无穷大
is_decreasing2 = True # 初始状态为递减
is_valid_range2 = True # 初始状态为满足范围条件
# 进行第二次成绩判断
for id_value in range(182, 361):
# 执行查询语句
query = "SELECT distance FROM yangdatetest1 WHERE id = %s"
cursor.execute(query, (id_value,))
# 获取查询结果
result = cursor.fetchone()
if result is not None:
distance = result[0]
if distance > prev_distance2:
is_decreasing2 = False # 递减状态更新为False
prev_distance2 = distance
if distance <= 2 or distance >= 100000:
is_valid_range2 = False
if is_decreasing2 and is_valid_range2:
value2 = 3
print("第二次成绩为:优秀")
elif is_decreasing2 or is_valid_range2:
value2 = 2
print("第二次成绩为:合格")
else:
value2 = 1
print("第二次成绩为:异常")
# 设置第三次成绩所需要的变量
# 进行第三次成绩判断
prev_distance3 = float("inf") # 初始化为正无穷大
is_decreasing3 = True # 初始状态为递减
is_valid_range3 = True # 初始状态为满足范围条件
for id_value in range(362, 541):
# 执行查询语句
query = "SELECT distance FROM yangdatetest1 WHERE id = %s"
cursor.execute(query, (id_value,))
# 获取查询结果
result = cursor.fetchone()
if result is not None:
distance = result[0]
if distance > prev_distance3:
is_decreasing3 = False # 递减状态更新为False
prev_distance3 = distance
if distance <= 2 or distance >= 100000:
is_valid_range3 = False
if is_decreasing3 and is_valid_range3:
value3 = 3
print("第三次成绩为:优秀")
elif is_decreasing3 or is_valid_range3:
value3 = 2
print("第三次成绩为:合格")
else:
value3 = 1
print("第三次成绩为:异常")
# 进行最终成绩判定,并且导入数据库
max_value = max(value1, value2, value3)
if max_value == 1:
# 执行插入语句
query = "INSERT INTO yangscore (score) VALUES (%s)"
value = '异常'
cursor.execute(query, (value,))
conn.commit()
print("最终成绩为异常")
if max_value == 2:
# 执行插入语句
query = "INSERT INTO yangscore (score) VALUES (%s)"
value = '合格'
cursor.execute(query, (value,))
conn.commit()
print("最终成绩为合格")
if max_value == 3:
# 执行插入语句
query = "INSERT INTO yangscore (score) VALUES (%s)"
value = '优秀'
cursor.execute(query, (value,))
conn.commit()
print("最终成绩为优秀")
break
cursor.close()
conn.close()
代码解释:先设置变量value1,value2,value3,分别为每次考试的成绩,然后在一个大的whlie循环内进行if判断,如果考试结束,也就是id=540,那么开始执行,进行评分算法从而改变value1/2/3的值,如果value1/2/3的值为3则表示优秀,2则表示合格,1则表示异常。最后进行总的成绩判断,也就是取value1/2/3里的最值,作为最好成绩,然后录入数据库。
评分算法:180个数据,如果呈现递减且无异常数据(x<2或x>100000)则为优秀,将对应的value值设为3,如果满足递减或无异常数据(2<x<100000)中一项则为合格,将对应的value值设为2,如果都不满足则为异常,将对应的value值设为1
fpga.py
import mysql.connector
import datetime
import serial
import socket
import time
import threading
import _thread
import threading
#连接数据库
conn = mysql.connector.connect(
host="localhost",
user="root",
password="root",
database="test1"
)
#串口数据读取
ser = serial.Serial('COM5', 128000,timeout=2.0) # 假设您的串口是 COM1,波特率为 9600
ser.flushInput() # 清空输入缓冲区
value =10
#考试时间变量设置
start_time = datetime.datetime.now()
add_time = datetime.timedelta(minutes=1.5)
end_time = start_time + add_time
# 创建游标对象
cursor = conn.cursor()
# 数据存入数据库
while True:
if ser.in_waiting: # 如果串口接收到了数据
data = ser.read(ser.in_waiting) # 读取所有可用的数据
num_chars = len(data) # 获取收到的字符个数
hex_value = data.hex() # 将字节数据转换为十六进制字符串
decimal_value = int(hex_value, 16) # 将十六进制字符串转换为十进制数
#print("com: ", decimal_value)
float_value = decimal_value / 10.0 # 将十进制数转换为浮点数并除以10
print("com: ", float_value)
value=float_value
cursor.execute("INSERT INTO yangdatetest1 (distance) VALUES (%s)", (value,))
conn.commit()
if datetime.datetime.now() >=end_time:
break # 考试时间等于结束时间,退出循环
# 关闭游标和连接
cursor.close()
conn.close()
代码解释:获取数据录入数据库,一次为1.5分钟