【迪文串口屏】核酸采样登记系统之一 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)
【迪文串口屏】核酸采样登记系统之二:准备背景图片 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)
【迪文串口屏】核酸采样登记系统之三 连上迪文云 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)
【迪文串口屏】核酸采样登记系统之四 查询数据并显示 - 国产芯片交流 - 电子工程世界-论坛 (eeworld.com.cn)
昨晚睡得早,早早就醒了,起来继续写代码:
优化了python代码,实现了查数的查询,数据的保存:
import time
import serial # 导入模块
import threading
from win32com.client import Dispatch
import pythoncom
from excel import *
STRGLO = "" # 读取的数据
BOOL = True # 读取标志位
import win32com.client
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])
send_list = [0x5a, 0xa5, 0x00, 0x00, 0x00, 0x00]
all_range = None
"""
# 等待接收命令: wait_infor
# 查询状态 query_infor
# 确认信息: confirmation_infor
# 保存信息: save_infor
"""
work_state = 'wait_enter'
dict_in_data = {
'tube_number': '',
'name': '',
'phone': '',
'ID': '',
'sex': '',
'company': '',
'test_date': ''
}
# 姓名数据组装
def creat_gbk_list(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)
DWritePort(ser, mysend_list)
def ReadData(ser):
global all_range, work_state
while BOOL:
if ser.in_waiting:
# STRGLO = ser.read(ser.in_waiting).decode("gbk")
STRGLO = ser.read(ser.in_waiting)
# print(STRGLO)
# print("长度为:" + str(len(STRGLO)))
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 recv_len == 0x06 and commd == 0x83:
if addres == 0x1080:
print("按身手机号码查询")
# 读取手机号码数据
work_state = 'query_infor'
DWritePort(ser, cmd_read_phone)
elif addres == 0x1090:
print("按身份证查询")
work_state = 'query_infor'
DWritePort(ser, cmd_read_ID_number)
elif addres == 0x1200 and work_state == 'wait_enter':
# 增加时间,保存数据
pythoncom.CoInitialize()
save_test_file(my_data)
pythoncom.CoUninitialize()
print("保存数据")
work_state = 'query_infor'
my_data = dict_in_data
# 转到主页面
DWritePort(ser, cmd_write_to_main_page)
elif commd == 0x83 and recv_len == 42 and addres == 0x1060 and work_state == 'query_infor':
# print("接收到手机号码")
recv_phone_len = STRGLO[6]
if (recv_phone_len == 0x13):
str_ph = STRGLO[7:18]
phone_code = str_ph.decode()
# 开始查找是否有手机号存在
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):
my_data = dict_in_data
my_data['name'] = all_range[my_index][1]
my_data['sex'] = all_range[my_index][4]
my_data['company'] = all_range[my_index][5]
my_data['phone'] = xls_phone_code
my_data['ID'] = all_range[my_index][3]
creat_gbk_list(my_data['sex'], 0x82, 0x11, 0x20)
creat_gbk_list(my_data['ID'], 0x82, 0x10, 0x20)
creat_gbk_list(my_data['company'], 0x82, 0x11, 0x40)
creat_gbk_list(my_data['name'], 0x82, 0x11, 0x60)
creat_gbk_list(my_data['company'], 0x82, 0x11, 0x40)
creat_gbk_list(my_data['name'], 0x82, 0x11, 0x60)
# 页面转移到确认页面 为什么要写两遍,BUG——
DWritePort(ser, cmd_write_to_enter_page)
DWritePort(ser, cmd_write_to_enter_page)
work_state = 'wait_enter'
except Exception as e:
print("出错啦:" + str(e))
else:
print("接收到的电话号码长度不对 长度为:" + str((recv_phone_len - 8)))
# 查找手机号
elif commd == 0x83 and recv_len == 52 and addres == 0x1020 and work_state == 'query_infor':
print("接收到身份证号码")
recv_ID_len = STRGLO[6]
if (recv_ID_len == 0x18):
str_ID = STRGLO[7:25]
str_ID = str_ID.decode()
# 开始查找是否有手机号存在
try:
my_index = None
for index, value in enumerate(all_range):
xls_ID_code = str(int(value[3]))
if str_ID == xls_ID_code:
my_index = index
break
if xls_ID_code == str_ID:
# 写性别
my_data = dict_in_data
my_data['name'] = all_range[my_index][1]
my_data['sex'] = all_range[my_index][4]
my_data['company'] = all_range[my_index][5]
my_data['phone'] = all_range[my_index][2]
my_data['ID'] = xls_ID_code
creat_gbk_list(my_data['sex'], 0x82, 0x11, 0x20)
creat_gbk_list(my_data['phone'], 0x82, 0x10, 0x60)
creat_gbk_list(my_data['company'], 0x82, 0x11, 0x40)
creat_gbk_list(my_data['name'], 0x82, 0x11, 0x60)
creat_gbk_list(my_data['company'], 0x82, 0x11, 0x40)
creat_gbk_list(my_data['name'], 0x82, 0x11, 0x60)
# 页面转移到确认页面 为什么要写两遍,BUG——
DWritePort(ser, cmd_write_to_enter_page)
DWritePort(ser, cmd_write_to_enter_page)
work_state = 'wait_enter'
except Exception as e:
work_state = 'wait_enter'
print("出错啦:" + str(e))
else:
work_state = 'wait_enter'
print("接收到的电话号码长度不对 长度为:" + str((recv_phone_len - 8)))
else:
print("非法数据")
# 打开串口
# 端口,GNU / Linux上的/ dev / ttyUSB0 等 或 Windows上的 COM3 等
# 波特率,标准值之一:50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200
# 超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)
def DOpenPort(portx, bps, timeout):
ret = False
try:
# 打开串口,并得到串口对象
ser = serial.Serial(portx, bps, timeout=timeout)
# 判断是否打开成功
if (ser.is_open):
ret = True
threading.Thread(target=ReadData, args=(ser,)).start()
except Exception as e:
print("---异常---:", e)
return ser, ret
# 关闭串口
def DColsePort(ser):
global BOOL
BOOL = False
ser.close()
# 写数据
def DWritePort(ser, text):
result = ser.write(text) # 写数据
return result
# 读数据
def DReadPort():
global STRGLO
str = STRGLO
STRGLO = "" # 清空当次读取
return str
def reade_excel():
global all_range
# 循环接收数据,此为死循环,可用线程实现
try:
my_xls = easyExcel(r'H:\疫情登记项目\pythonpro\核酸数据库.xlsx')
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)
my_xls.close()
except Exception as e:
print("出错啦:" + str(e))
# 保存数据
def save_test_file(data):
# 先检查是否有文件,没有新建一个,有测打开
"""
:param data: dict_in_data = {
'tube_number': '',
'name': '',
'phone': '',
'ID': '',
'sex': '',
'company': '',
'test_date': ''
}
:return: None
"""
try:
save_xls = easyExcel(r'H:\疫情登记项目\pythonpro\送检单.xlsx')
max_row = save_xls.getRows('Sheet1')
if max_row == 1:
print("当天第一次写数据,初始化一下")
# 试号管为 1
data['tube_number'] = 1
this_row = 1
else:
tube = save_xls.getCell('Sheet1', max_row, 2)
this_row = save_xls.getCell('Sheet1', max_row, 2)
if (this_row % 10) == 0: # 10混1
tube = tube + 1
# 表行号增加一行
max_row = max_row + 1
save_xls.setCell('Sheet1', max_row, 1, this_row) # 增加序号
save_xls.setCell('Sheet1', max_row, 2, data['tube_number']) # 增加管号
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()
save_xls.close()
except Exception as e:
save_xls.close()
print("保存数据出错:" + str(e))
if __name__ == "__main__":
ser, ret = DOpenPort("COM18", 115200, None)
if (ret == True): # 判断串口是否成功打开
print("打开串口成功1")
reade_excel()
修改了数据确认页面:
基本功能实现50%了,展示一下:
查到数据的界面
没有添加数据的表格:
成功录入数据以后的表格:
下一步,新建信息的录入,还有一些弹出菜单提示等,还得需要很长的时间,争取下一个星期可以实际应用。
|