资源简介
python实时读取串口数据,运用多线程实现整点数据自动保存至excel,可扩展pyqt5界面;串口数据可参照我的stm32源码资源
代码片段和文件信息
#!/usr/bin/python3
import threading
import time
import os
from openpyxl import Workbook load_workbook
from openpyxl.styles import Font
class SaveData:
def __init__(self my_serial):
self.serial = my_serial
self.__dirpath = “../Data/“
if not os.path.exists(self.__dirpath):
os.makedirs(self.__dirpath)
@staticmethod
def check_path(filepath):
if not os.path.exists(filepath):
new_workbook = Workbook()
new_workbook.save(filepath)
@staticmethod
def insert_headings(current_sheet):
columns = [“Time“ “Temperature“ “Humidity“ “CO2“ “TVOC“ “O2“ “PM2.5“ “PM10“]
for i in range(8):
cell = current_sheet.cell(column=i + 1 row=1 value=columns[i])
cell.font = Font(size=12 bold=True)
def save_to_local(self):
localtime = time.localtime(time.time())
filepath = self.__dirpath + time.strftime(“%Y_%m“ localtime) + “.xlsx“
self.check_path(filepath)
current_workbook = load_workbook(filepath)
sheetname = time.strftime(“%m-%d“ localtime)
current_sheet = current_workbook.active
for sheet in current_workbook:
if sheet.title == “Sheet“:
sheet.title = sheetname
self.insert_headings(sheet)
current_workbook.save(filepath)
current_sheet = sheet
break
elif sheet.title == sheetname:
current_sheet = sheet
break
if current_sheet.title != sheetname:
current_sheet = current_workbook.create_sheet(sheetname)
self.insert_headings(current_sheet)
current_workbook.save(filepath)
current_time = time.strftime(“%H:%M“ localtime)
temperature = self.serial.get_temperature()
humidity = self.serial.get_humidity()
co2 = self.serial.get_co2()
tvoc = self.serial.get_tvoc()
o2 = self.serial.get_o2()
pm25 = self.serial.get_pm25()
pm10 = self.serial.get_pm10()
new_data = [current_time temperature humidity co2 tvoc o2 pm25 pm10]
current_sheet.append(new_data)
current_workbook.save(filepath)
class AutoSave(threading.Thread):
def __init__(self my_serial):
threading.Thread.__init__(self)
self.name = “auto_save_data“
self.serial = my_serial
self.__save_data = SaveData(self.serial)
self.__saved = False
self.__closed = False
def run(self):
while True:
if self.__closed:
break
localtime = time.localtime(time.time())
if localtime.tm_min == 0 and not self.__saved:
self.__save_data.save_to_local()
self.__saved = True
elif localtime.tm_min != 0:
self.__saved = False
def ki
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3146 2019-05-06 15:57 SaveData.py
文件 3894 2019-05-06 16:08 Serial.py
----------- --------- ---------- ----- ----
7040 2
- 上一篇:A*算法解决八数码问题
- 下一篇:python实现AI五子棋
相关资源
- python+pyqt5+百度AI+车牌识别.rar
- Python3.x+Pyqt5实现界面编程浏览网页
- Python-用pyqt5和parametrics实现很酷的动画
- PyQt5 Python 桌面应用程序源码.zip
- python pyqt5 计时器源代码
- pyqt5_python_Gui入门教程.docx
- 利用摄像头拍照并保存照片程序pyth
- QT5 Python GUI Programming Cookbook - 2018
- Python3.4 PyQt5 32位安装版PyQt5-5.5.1-gpl-
- PyQt5 5.3.2 gpl Py3.4 Qt5.3.1 x32.exe
- PyQt5-5.12-5.12.1_a-cp35.cp36.cp37.cp38-none-w
- Serial assistant.rar
- PyQt5快速开发与实战pdf+源码.zip
- 创建画板,手写体实时在线识别
- PyQt5_Tools-5.7.dev1-py3-none-any.whl
- PyQt5快速开发与实战.zip
- PyQt5-5.4-gpl-Py3.4-Qt5.4.0-x64
- Python + PyQt5 + MySQL模拟QQ的聊天与娱乐
- 创建画板,实时在线手写体识别
- PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x32
- pyqt5实现自动获取IP软件
- python-vlc二次封装,可用于pyqt
- tesseract V2.0()
- python聊天室---pyqt5+socket+Thread聊天室
- Pyqt5 按钮事件绘制图形
-
PyQ5 Mtaplotlib Datafr
ame画一条随鼠标移 - pyqt5做一个一个时钟demo(python3)
-
pyQt5_wavepla
yer python计算声音分贝 语 - 实现小学三年级口算题生成器
- 续Python3.x+Pyqt5实现主窗体里QToolBox导航
评论
共有 条评论