import serial
import binascii
import datetime
import time
import re
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'
port_set = "com13"
baudrate_set = 115200
timeout_set = 1
ser = serial.Serial(port = port_set, baudrate = baudrate_set, timeout = timeout_set) ##串口参数配置
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(["编号", "时间", "RH1(%)", "Temp1(℃)", "RH2(%)", "Temp2(℃)", "RH3(%)", "Temp3(℃)", "RH4(%)", "Temp4(℃)", "RH5(%)", "Temp5(℃)" ]) ##写入表格首行 标题行
cishu = 1 ##初始坐标
x = [0]
RH1_Y = [0]
Temp1_Y = [0]
RH2_Y = [0]
Temp2_Y = [0]
RH3_Y = [0]
Temp3_Y = [0]
RH4_Y = [0]
Temp4_Y = [0]
RH5_Y = [0]
Temp5_Y = [0]
##while True:
data = ser.read(250)
data1 = str(data)
data2 = data1.replace("b'" , "").replace("'" , "").replace("\\t" , "").replace("\\r" , "").replace("\\n" , "").replace("\\xa1" , "").replace("\\xe3" , "").replace("C" , "").replace("%" , "").replace(" " ,"")
print(data2)
while True:
if cishu < 30000:
ceshi_time = str(cishu)
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)
a = data2.find("1stRH")
b = data2.find("1stTemp")
RH1_data = data2[(a+5) : b]
print(RH1_data)
c = data2.find("2ndRH")
Temp1_data = data2[(b+7) : c]
print(Temp1_data)
d = data2.find("2ndTemp")
RH2_data = data2[(c+5) : d]
print(RH2_data)
e = data2.find("3rdRH")
Temp2_data = data2[(d+7) : e]
print(Temp2_data)
f = data2.find("3rdTemp")
RH3_data = data2[(e+5) : f]
print(RH3_data)
g = data2.find("4thRH")
Temp3_data = data2[(f+7) : g]
print(Temp3_data)
h = data2.find("4thTemp")
RH4_data = data2[(g+5) : h]
print(RH4_data)
i = data2.find("5thRH")
Temp4_data = data2[(h+7) : i]
print(Temp4_data)
j = data2.find("5thTemp")
RH5_data = data2[(i+5) : j]
print(RH5_data)
Temp5_data = data2[(j+7) : ]
print(Temp5_data)
print("=========================")
save_test_data.append([cishu, now1, RH1_data, Temp1_data, RH2_data, Temp2_data, RH3_data, Temp3_data, RH4_data, Temp4_data, RH5_data, Temp5_data])
test_data.save("{}.xlsx".format(time_excel_name)) ##表格数据保存
##update_line()
time.sleep(0.8)
cishu = cishu + 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", "温度", "湿度"])
cishu = 0