[MCU] 【迪文串口屏】实战应用

lugl4313820   2022-5-25 23:09 楼主

【迪文串口屏】核酸采样登记系统之一 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)

【迪文串口屏】核酸采样登记系统之二:准备背景图片 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)

【迪文串口屏】核酸采样登记系统之三 连上迪文云 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)

【迪文串口屏】核酸采样登记系统之四 查询数据并显示 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)

【迪文串口屏】核酸采样登记系统之五 数据保存 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)

【迪文串口屏】核酸采样登记系统之六 增加采样工作站 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)

       好久没有更新核酸检测系统帖子了,因为上个星期四到单位实用,在保存数方面比较慢,究其原因是读写excel占用了太多的时间,这个星期一直在考虑如何解决这个问题,再加上最近的评测作品比较多,今年花了一天的时间对项目进行的修正,基本达到实用效果,现在跟大家汇报如下:

1、更新了界面,用Pyside设计界面:

image.png 2、代码:

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  # 导入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("当天第一次写数据,初始化一下")
                # 试号管为 1
                data['tube_number'] = 1
                this_row = 1
                tube = 1
            else:
                tube = save_xls.getCell('Sheet1', max_row, 2)
                if ((max_row - 1) % 10) == 0:  # 10混1
                    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):
        # 发送的list
        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['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):
        # 发送的list
        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("按手机号码查询")
                            # # 读取手机号码数据
                            # print("手机号码为:" + dict_in_data['phone'])
                            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['company'], 0x82, 0x11, 0x40)
                                    self.creat_gbk_list(dict_in_data['name'], 0x82, 0x11, 0x60)
                                    # 页面转移到确认页面 为什么要写两遍,BUG——
                                    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:
                            # print("按身份证查询")
                            # print("身份证号码为:" + dict_in_data['ID'])
                            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)
                                # 页面转移到确认页面 为什么要写两遍,BUG——
                                # 页面转移到确认页面 为什么要写两遍,BUG——

                                self.MaiNwritePort(cmd_write_to_enter_page)
                                self.MaiNwritePort(cmd_write_to_enter_page)
                            except Exception as e:
                                print("出错啦:" + str(e))
                            # DWritePort(ser, cmd_read_ID_number)
                        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_())

 3、应用的视频:



总结:总的来说基本功能实现了,还有一些需要进一步完善的地方,慢慢来吧。

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复