import binascii
import serial
import datetime
import time
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import Font
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['font.family'] = 'SimHei'
import keyboard
def update_line(): ##动态更新折线图(3行3列)
plt.figure(num= '测试数据', figsize= (18, 9))
x.append(i)
CO2_data_Y.append(co2_data)
CH2O_data_Y.append(ch2o_data)
TVOC_data_Y.append(tvoc_data)
PM25_data_Y.append(pm25_data)
PM10_data_Y.append(pm10_data)
TEMP_data_Y.append(temp_data)
HUMI_data_Y.append(humi_data)
plt.subplot(3, 3, 1)
plt.plot(x, CO2_data_Y, 'b')
plt.xlabel('测试次数')
plt.ylabel('二氧化碳测试数据')
plt.title('二氧化碳测试数据图折线图')
plt.subplot(3, 3, 2)
plt.plot(x, CH2O_data_Y, 'r')
plt.xlabel('测试次数')
plt.ylabel('CH2o测试值')
plt.title('CH2O测试数据折线图')
plt.subplot(3, 3, 3)
plt.plot(x, TVOC_data_Y, 'b')
plt.xlabel('测试次数')
plt.ylabel('VOC测试数据')
plt.title('TVOC测试数据图折线图')
plt.subplot(3, 3, 4)
plt.plot(x, PM25_data_Y, 'r')
plt.xlabel('测试次数')
plt.ylabel('PM2.5测试值')
plt.title('PM2.5测试数据折线图')
plt.subplot(3, 3, 5)
plt.plot(x, PM10_data_Y, 'r')
plt.xlabel('测试次数')
plt.ylabel('PM10测试值')
plt.title('PM10测试数据折线图')
plt.subplot(3, 3, 6)
plt.plot(x, TEMP_data_Y, 'b')
plt.xlabel('测试次数')
plt.ylabel('湿度测试数据')
plt.title('湿度测试数据图折线图')
plt.subplot(3, 3, 7)
plt.plot(x, TEMP_data_Y, 'r')
plt.xlabel('测试次数')
plt.ylabel('温度测试值')
plt.title('温度测试数据折线图')
plt.pause(0.1)
port_set = "com4"
baudrate_set = 9600
timeout_set = 2
ser = serial.Serial(port = port_set, baudrate = baudrate_set, timeout = timeout_set) ##串口参数配置
i = 1 ##初始坐标
x = [0]
CO2_data_Y = [0]
CH2O_data_Y = [0]
TVOC_data_Y = [0]
PM25_data_Y = [0]
PM10_data_Y = [0]
TEMP_data_Y = [0]
HUMI_data_Y = [0]
now = datetime.datetime.now() ##系统时间获取
test_data = openpyxl.Workbook()
time_excel_name =now.strftime("%Y%m%d%H%M%S")
save_test_data = test_data.create_sheet(title= time_excel_name, index=0) ##按照系统时间创建工作表
header_font = Font(bold=True)
save_test_data.append(["编号", "时间", "二氧化碳", "甲醛", "TVOC", "PM2.5", "PM10", "温度", "湿度"]) ##写入表格首行 标题行
print('请按键盘字母“g”, 启动测试,按键盘按键“ESC”, 终止测试')
keyboard.wait('g')
while True:
data = ser.read(17)
if i < 30000:
ceshi_time = str(i)
print("\033[1;37;42m第{}次测试\033[0m".format(ceshi_time)) ##测试次数获取
data_hex = binascii.hexlify(data).decode('utf-8')
now1 = datetime.datetime.now() ##系统时间获取
test_time = now1.strftime("%H:%M:%S")
print(test_time)
co2_com_data = data_hex[4:8]
co2_data = int(co2_com_data, 16) ##二氧化碳数据获取
print(f"co2={co2_data}")
ch2o_com_data = data_hex[8:12]
ch2o_data = int(ch2o_com_data, 16) ##甲醛数据获取
print(f"ch2o={ch2o_data}")
tvoc_com_data = data_hex[12:16]
tvoc_data = int(tvoc_com_data, 16) ##挥发物数据获取
print(f"\033[1;33;40mtvoc={tvoc_data}\033[0m")
pm25_com_data = data_hex[16:20]
pm25_data = int(pm25_com_data, 16) ##PM2.5数据获取
print(f"\033[1;33;40mpm2.5={pm25_data}\033[0m")
pm10_com_data = data_hex[20:24]
pm10_data = int(pm10_com_data, 16) ##PM10数据获取
print(f"pm10={pm10_data}")
temp1_com_data = data_hex[24:26]
temp1_data = int(temp1_com_data, 16)
temp2_com_data = data_hex[26:28]
temp2_data = int(temp2_com_data, 16)
temp_data = str(temp1_data) + "." + str(temp2_data) ##温度数据获取
temp = float(temp_data)
print(f"temperature= {temp}")
humi1_com_data = data_hex[28:30]
humi1_data = int(humi1_com_data, 16)
humi2_com_data = data_hex[30:32]
humi2_data = int(humi2_com_data, 16)
humi_data = str(humi1_data) + "." + str(humi2_data)
humi = float(humi_data)
print(f"humidity={humi}") ##湿度数据获取
print("=========================")
save_test_data.append([i, now1, co2_data, ch2o_data, tvoc_data, pm25_data, pm10_data, temp, humi])
test_data.save("{}.xlsx".format(time_excel_name)) ##表格数据保存
update_line()
i = i + 1
else:
time_excel_name =now.strftime("%Y%m%d%H%M%S")
save_test_data = test_data.create_sheet(title= time_excel_name, index=0)
header_font = Font(bold=True)
save_test_data.append(["编号", "时间", "二氧化碳", "甲醛", "TVOC", "PM2.5", "PM10", "温度", "湿度"])
i = 0
else:
print("error")