好久没有更新核酸检测系统帖子了,因为上个星期四到单位实用,在保存数方面比较慢,究其原因是读写excel占用了太多的时间,这个星期一直在考虑如何解决这个问题,再加上最近的评测作品比较多,今年花了一天的时间对项目进行的修正,基本达到实用效果,现在跟大家汇报如下:
- import sys
- import time
- from PySide2.QtWidgets import *
- from PySide2.QtCore import *
- from PySide2.QtGui import *
- from PySide2.QtSerialPort import *
- from excel import *
- from Test_main import Ui_MainWindow
- from threading import Thread
- import qtmodern.styles
- import qtmodern.windows
- import pythoncom
- from PySide2.QtCore import Signal,QObject
- cmd_read_phone = bytes([0x5a, 0xa5, 0x04, 0x83, 0x10, 0x60, 0x13])
- cmd_read_ID_number = bytes([0x5a, 0xa5, 0x04, 0x83, 0x10, 0x20, 0x18])
- cmd_write_to_enter_page = bytes([0x5a, 0xa5, 0x07, 0x82, 0x00, 0x84, 0x5a, 0x01, 0x00, 0x10])
- cmd_write_to_main_page = bytes([0x5a, 0xa5, 0x07, 0x82, 0x00, 0x84, 0x5a, 0x01, 0x00, 0x00])
- cmd_write_to_waite_page = bytes([0x5a, 0xa5, 0x07, 0x82, 0x00, 0x84, 0x5a, 0x01, 0x00, 0x12])
- send_list = [0x5a, 0xa5, 0x00, 0x00, 0x00, 0x00]
- all_range = None
- phone_code = None
- xls_ID_code = None
- dict_in_data = {
- 'tube_number': '',
- 'name': '',
- 'phone': '',
- 'ID': '',
- 'sex': '',
- 'company': '',
- 'test_date': ''
- }
- class MainWindow(Ui_MainWindow, QMainWindow):
- global all_range, my_xls, save_xls, phone_code
- def __init__(self):
- super(MainWindow, self).__init__()
- self.setupUi(self)
- self.setWindowTitle("核酸检测登记管理系统")
- self.comboBaudrate = 115200
- serial_list = QSerialPortInfo.availablePorts()
- for port in serial_list:
- self.comMainPort.addItem(port.portName())
- self.comSlavePort.addItem(port.portName())
- self.serialMain = QSerialPort()
- self.serialSlave = QSerialPort()
- self.bntOpen.clicked.connect(self.open_serial)
- self.serialMain.readyRead.connect(self.MainPortReadData)
- self.serialSlave.readyRead.connect(self.SlavePortReadData)
- self.bntExit.clicked.connect(self.exit_save_data)
- self.reade_excel()
-
- def reade_excel(self):
-
- global my_xls, save_xls, all_range
- try:
- path = sys.path[0]
- print(path)
- my_xls_path = path + '\核酸数据库.xlsx'
- my_save_xls_path = path + '\送检单.xlsx'
- pythoncom.CoInitialize()
- my_xls = easyExcel(my_xls_path)
- cols = my_xls.getCols('Sheet1')
- print("总列数为:" + str(cols))
- rows = my_xls.getRows('Sheet1') + 1
- print("总行数为:" + str(rows))
-
- all_range = my_xls.getRange('Sheet1', 2, 1, rows - 1, cols)
- save_xls = easyExcel(my_save_xls_path)
- pythoncom.CoUninitialize()
- except Exception as e:
- print("打开EXCEL出错啦:" + str(e))
-
-
- def save_infor_file(self,data):
- global my_xls
- try:
- rows = my_xls.getRows('Sheet1') + 1
- my_xls.setCell('Sheet1', rows, 1, rows)
- my_xls.setCell('Sheet1', rows, 2, data['name'])
- my_xls.setCell('Sheet1', rows, 5, data['sex'])
- my_xls.setCell('Sheet1', rows, 3, data['phone'])
- my_xls.setCell('Sheet1', rows, 4, data['ID'])
- my_xls.setCell('Sheet1', rows, 6, data['company'])
- my_xls.save()
- return 1
- except Exception as e:
- print("保存基本信息出错:" + str(e))
-
- def save_test_file(self,data):
- global save_xls
-
- try:
- max_row = save_xls.getRows('Sheet1')
- if max_row == 1:
- print("当天第一次写数据,初始化一下")
-
- data['tube_number'] = 1
- this_row = 1
- tube = 1
- else:
- tube = save_xls.getCell('Sheet1', max_row, 2)
- if ((max_row - 1) % 10) == 0:
- tube = tube + 1
-
- max_row = max_row + 1
- save_xls.setCell('Sheet1', max_row, 1, max_row - 1)
- save_xls.setCell('Sheet1', max_row, 2, tube)
- save_xls.setCell('Sheet1', max_row, 3, data['name'])
- save_xls.setCell('Sheet1', max_row, 4, data['sex'])
- save_xls.setCell('Sheet1', max_row, 5, data['phone'])
- save_xls.setCell('Sheet1', max_row, 6, data['ID'])
- save_xls.setCell('Sheet1', max_row, 7, data['company'])
- save_xls.setCell('Sheet1', max_row, 8, time.strftime("%Y-%m-%d", time.localtime()))
- save_xls.save()
- print("保存数据成功")
- xh = max_row - 1
- return tube, xh
- except Exception as e:
- print("保存数据出错:" + str(e))
- return 0
-
-
- def open_serial(self):
- self.serialMain.setPortName(self.comMainPort.currentText())
- self.serialSlave.setPortName(self.comSlavePort.currentText())
- if self.bntOpen.text() == "打开":
- self.serialMain.open(QIODevice.ReadWrite)
- self.serialMain.setBaudRate(self.comboBaudrate)
- self.serialSlave.open(QIODevice.ReadWrite)
- self.serialSlave.setBaudRate(self.comboBaudrate)
- self.bntOpen.setText("关闭")
- else:
- self.serialMain.close()
- self.serialSlave.close()
- self.bntOpen.setText("打开")
-
-
- def creat_gbk_list(self, mydata, my_commad, H_address, L_address):
-
- send_list = [0x5a, 0xa5, 0x00, 0x00, 0x00, 0x00]
- send_len = 1 + 2 + 2 + len(mydata) * 2
- send_list[2] = send_len
-
- send_list[3] = my_commad
- send_list[4] = H_address
- send_list[5] = L_address
- mysend_list = bytes(send_list) + mydata.encode("gbk") + b'\xFF\xFF'
- print(mysend_list)
- self.MaiNwritePort(mysend_list)
-
-
- def clear_show_data(self):
- global dict_in_data
- dict_in_data['name'] = ''
- dict_in_data['sex'] = ''
- dict_in_data['company'] = ''
- dict_in_data['phone'] = ''
- dict_in_data['phone'] = ''
- dict_in_data['ID'] = ''
- self.creat_gbk_list(dict_in_data['sex'], 0x82, 0x11, 0x20)
- self.creat_gbk_list(dict_in_data['phone'], 0x82, 0x10, 0x60)
- self.creat_gbk_list(dict_in_data['company'], 0x82, 0x11, 0x40)
-
-
- self.creat_gbk_list(dict_in_data['name'], 0x82, 0x11, 0x60)
- self.creat_gbk_list(dict_in_data['ID'], 0x82, 0x10, 0x20)
-
- def creat_gbk_list_nu(self, mydata, my_commad, H_address, L_address):
-
- send_list = [0x5a, 0xa5, 0x00, 0x00, 0x00, 0x00]
- send_len = 1 + 2 + 2 + len(mydata) * 2
- send_list[2] = send_len
-
- send_list[3] = my_commad
- send_list[4] = H_address
- send_list[5] = L_address
- mysend_list = bytes(send_list) + mydata.encode("gbk") + b'\xFF\xFF\xFF'
- print(mysend_list)
- self.SlaveWritePort(mysend_list)
-
-
- def SlaveWritePort(self, text):
- if self.serialSlave.isOpen():
- self.serialSlave.write(text)
-
-
- def MaiNwritePort(self, text):
- if self.serialMain.isOpen():
- self.serialMain.write(text)
-
- def MainPortReadData(self):
- global all_range, dict_in_data, phone_code, xls_ID_code
- ba = self.serialMain.readAll()
- main_port_recv = QTextCodec.codecForLocale().toUnicode(ba.data())
- self.textInfor.appendPlainText(main_port_recv)
- STRGLO = ba.data()
- try:
- if STRGLO[0] == 0x5a and STRGLO[1] == 0xa5:
-
- addres = STRGLO[4] << 8 | STRGLO[5]
- commd = STRGLO[3]
- recv_len = STRGLO[2]
- print("地址为:" + hex(addres) + " 长度为:" + str(recv_len) + " 命令为:" + hex(commd))
- try:
- if recv_len == 0x06 and commd == 0x83:
- if addres == 0x1080:
- print("按手机号码查询")
-
-
- my_index = None
- try:
- for index, value in enumerate(all_range):
- xls_phone_code = str(int(value[2]))
- if phone_code == xls_phone_code:
- my_index = index
- break
- if xls_phone_code == phone_code and (my_index is not None):
- print("查到数据")
- dict_in_data['name'] = all_range[my_index][1]
- dict_in_data['sex'] = all_range[my_index][4]
- dict_in_data['company'] = all_range[my_index][5]
- dict_in_data['ID'] = all_range[my_index][3]
- self.creat_gbk_list(dict_in_data['sex'], 0x82, 0x11, 0x20)
- self.creat_gbk_list(dict_in_data['ID'], 0x82, 0x10, 0x20)
- self.creat_gbk_list(dict_in_data['company'], 0x82, 0x11, 0x40)
- self.creat_gbk_list(dict_in_data['name'], 0x82, 0x11, 0x60)
-
- self.creat_gbk_list(dict_in_data['name'], 0x82, 0x11, 0x60)
-
- self.MaiNwritePort(cmd_write_to_enter_page)
- self.MaiNwritePort(cmd_write_to_enter_page)
- else:
- print("没有查到信息")
- except Exception as e:
- print("按手机号码查询出错啦:" + str(e))
- elif addres == 0x1090:
-
-
- try:
- my_index = None
- for index, value in enumerate(all_range):
- xls_ID_code = value[3]
- if dict_in_data['ID'] == xls_ID_code:
- my_index = index
- break
- dict_in_data['name'] = all_range[my_index][1]
- dict_in_data['sex'] = all_range[my_index][4]
- dict_in_data['company'] = all_range[my_index][5]
- dict_in_data['phone'] = str(int(all_range[my_index][2]))
- self.creat_gbk_list(dict_in_data['sex'], 0x82, 0x11, 0x20)
- self.creat_gbk_list(dict_in_data['phone'], 0x82, 0x10, 0x60)
- self.creat_gbk_list(dict_in_data['company'], 0x82, 0x11, 0x40)
- self.creat_gbk_list(dict_in_data['name'], 0x82, 0x11, 0x60)
- self.creat_gbk_list(dict_in_data['company'], 0x82, 0x11, 0x40)
- self.creat_gbk_list(dict_in_data['name'], 0x82, 0x11, 0x60)
-
-
-
- self.MaiNwritePort(cmd_write_to_enter_page)
- self.MaiNwritePort(cmd_write_to_enter_page)
- except Exception as e:
- print("出错啦:" + str(e))
-
- elif addres == 0x1200:
-
- print("确认登记数据")
- tub, xh = self.save_test_file(dict_in_data)
- if tub is not None:
- self.MaiNwritePort(cmd_write_to_waite_page)
- self.creat_gbk_list_nu(dict_in_data['name'], 0x82, 0x20, 0x20)
- self.creat_gbk_list_nu(str(int(tub)), 0x82, 0x20, 0x40)
- self.creat_gbk_list_nu(str(int(tub)), 0x82, 0x20, 0x40)
- self.creat_gbk_list_nu(str(xh), 0x82, 0x20, 0x60)
- self.creat_gbk_list_nu(str(xh), 0x82, 0x20, 0x60)
- else:
-
- pass
-
- elif addres == 0x1210:
- print('保存数据')
- if dict_in_data['name'] == "" or \
- dict_in_data['sex'] == "" or\
- dict_in_data['company'] == "" or\
- dict_in_data['phone'] == "" or\
- dict_in_data['ID'] == "":
- print("data ERROR")
- else:
- save_state = self.save_infor_file(dict_in_data)
- cols = my_xls.getCols('Sheet1')
- print("总列数为:" + str(cols))
- rows = my_xls.getRows('Sheet1') + 1
- print("总行数为:" + str(rows))
-
- all_range = my_xls.getRange('Sheet1', 2, 1, rows - 1, cols)
- self.MaiNwritePort(cmd_write_to_enter_page)
- elif commd == 0x83 and recv_len == 0x12 and addres == 0x1060:
- print("接收到手机号码")
- recv_phone_len = STRGLO[6]
- if recv_phone_len == 0x07:
- str_ph = STRGLO[7:18]
- phone_code = str_ph.decode()
-
- dict_in_data['phone'] = phone_code
- else:
- print("接收到的电话号码长度不对 长度为:" + str((recv_phone_len - 8)))
- elif commd == 0x83 and recv_len == 24 and addres == 0x1020:
- print("接收到身份证号码")
- recv_ID_len = STRGLO[6]
- if recv_ID_len == 10:
- dict_in_data['ID'] = STRGLO[7:25].decode()
- else:
- print("接收到的身份证长度不对 长度为:" + str((recv_ID_len - 8)))
- elif commd == 0x83 and addres == 0x1160:
-
- recv_name_len = STRGLO[6] - 1
- if 0 < recv_name_len < 5:
- dict_in_data['name'] = (STRGLO[7:(7 + recv_name_len * 2)]).decode("gbk")
- else:
- print("姓名的长度不符合")
- print(dict_in_data['name'])
- elif commd == 0x83 and addres == 0x1140:
-
- recv_company_len = STRGLO[6] - 1
- if 1 < recv_company_len < 6:
- dict_in_data['company'] = (STRGLO[7:(7 + recv_company_len * 2)]).decode("gbk")
- else:
- print("单位的长度不符合")
- print(dict_in_data['company'])
- elif commd == 0x83 and addres == 0x1120:
-
- recv_sex_len = STRGLO[6]
- if recv_sex_len == 2:
- str_sex = (STRGLO[7:9]).decode("gbk")
- if str_sex == '男' or str_sex == '女':
- dict_in_data['sex'] = str_sex
- else:
- print("性别必须为男或者女")
- dict_in_data['sex'] = ''
- else:
- print("姓名的长度不符合")
- print(dict_in_data['sex'])
- except Exception as e:
- print("解释命令出错" + str(e))
- else:
- print("命令不认识")
- except Exception as e:
- print(str(e))
- def SlavePortReadData(self):
- ba = self.serialSlave.readAll()
- main_port_recv = QTextCodec.codecForLocale().toUnicode(ba.data())
- self.textInfor.appendPlainText(main_port_recv)
- STRGLO = ba.data()
- try:
- if STRGLO[0] == 0x5a and STRGLO[1] == 0xa5:
-
- addres = STRGLO[4] << 8 | STRGLO[5]
- commd = STRGLO[3]
- recv_len = STRGLO[2]
- print("地址为:" + hex(addres) + " 长度为:" + str(recv_len) + " 命令为:" + hex(commd))
- if addres == 0x2004:
- self.clear_show_data()
- self.MaiNwritePort(cmd_write_to_main_page)
- self.creat_gbk_list_nu('', 0x82, 0x11, 0x60)
-
- except Exception as e:
- print("接收护士站错据出错:" + str(e))
-
- def save_exec_data(self):
- try:
- my_xls.save()
- save_xls.save()
- except Exception as e:
- print("保存数据出错:" + str(e))
-
- def exit_save_data(self):
- try:
- my_xls.save()
- my_xls.close()
- save_xls.save()
- save_xls.close()
- sys.exit(1)
- except Exception as e:
- print("保存数据出错:" + str(e))
-
- if __name__ == "__main__":
- app = QApplication(sys.argv)
-
- qtmodern.styles.dark(app)
-
- app.setWindowIcon(QIcon("../monkey.ico"))
-
- mwin = MainWindow()
-
- win = qtmodern.windows.ModernWindow(mwin)
-
- win.show()
-
- sys.exit(app.exec_())
-
-