问题咨询及项目源码下载请加群:
群名:IT项目交流群
群号:245022761
说明:本代码经本人测试,stm89c51等开发板传感器采集数据发送到串口,实现读取并写入数据库,不懂之处大家留言,看见会及时回复大家。
1:读取串口数据写入csv文件:
#!/usr/bin/python3
from PyQt5.QtCore import QTimer, QByteArray, QIODevice
from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo
from PyQt5.QtWidgets import *
import sys
from SaveData import *
class SerialWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.__data = QByteArray()
self.__serial = QSerialPort()
self.__timer = QTimer(self)
for info in QSerialPortInfo.availablePorts():
if info.description() == "USB-SERIAL CH340":
self.__serial = QSerialPort(info)
print(self.__serial.portName())
break
self.__serial.readyRead.connect(self.__read_data)
self.__timer.timeout.connect(self.__timer_update_com)
self.__temperature = 0
self.__humidity = 0
self.__co2 = 0
self.__tvoc = 0
self.__pm25 = 0
self.__pm10 = 0
self.__o2 = 0
if self.__serial.open(QIODevice.ReadWrite):
print("open success")
else:
print("open fail")
self.__auto_save_thread = AutoSave(self)
self.__auto_save_thread.start()
def closeEvent(self, QCloseEvent):
self.__auto_save_thread.kill()
super().closeEvent(QCloseEvent)
def __read_data(self):
self.__timer.start(40)
self.__data.append(self.__serial.readAll())
def __timer_update_com(self):
self.__timer.stop()
length = self.__data.length()
i = 0
while i < length:
num = ord(self.__data[i])
if num =