2024-07-03 12:01:11 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Created on Tue May 28 15:40:48 2024
|
|
|
|
|
|
|
|
|
|
@author: WANGXIBAO
|
|
|
|
|
"""
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-04 13:31:26 +08:00
|
|
|
|
import time,math,os
|
2024-07-03 16:31:41 +08:00
|
|
|
|
import re,csv,datetime
|
2024-07-03 12:01:11 +08:00
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
from PyQt5.QtCore import QThread, pyqtSignal,Qt
|
|
|
|
|
from PyQt5.QtWidgets import QMessageBox
|
|
|
|
|
from PyQt5.QtChart import QChart, QValueAxis, QSplineSeries,QLineSeries
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 波形显示
|
|
|
|
|
class QChartViewPlot(QChart):
|
|
|
|
|
|
|
|
|
|
# 相位振动波形
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super(QChartViewPlot, self).__init__(parent)
|
|
|
|
|
self.window = parent
|
2024-07-03 16:31:41 +08:00
|
|
|
|
self.xRange = 60
|
2024-07-03 12:01:11 +08:00
|
|
|
|
self.counter = 0
|
|
|
|
|
self.seriesList = []
|
|
|
|
|
self.legend().show()
|
|
|
|
|
|
|
|
|
|
self.axisX = QValueAxis()
|
|
|
|
|
self.axisX.setRange(0, self.xRange)
|
|
|
|
|
self.addAxis(self.axisX, Qt.AlignBottom)
|
|
|
|
|
# self.setAxisX(self.axisX, series)
|
|
|
|
|
self.y_min = 0
|
|
|
|
|
self.y_max = 100
|
|
|
|
|
self.axisY = QValueAxis()
|
|
|
|
|
self.axisY.setRange(self.y_min, self.y_max)
|
|
|
|
|
self.addAxis(self.axisY, Qt.AlignLeft)
|
|
|
|
|
# self.setAxisY(self.axisY, series)
|
|
|
|
|
|
|
|
|
|
#self.series = QSplineSeries()
|
|
|
|
|
self.series = QLineSeries()
|
|
|
|
|
self.series.setName("波形")
|
|
|
|
|
self.series.setUseOpenGL(True)
|
|
|
|
|
self.addSeries(self.series)
|
|
|
|
|
self.series.attachAxis(self.axisX)
|
|
|
|
|
self.series.attachAxis(self.axisY)
|
|
|
|
|
|
|
|
|
|
def handle_update(self, ydata):
|
|
|
|
|
# 更新y值
|
|
|
|
|
if self.counter < self.xRange:
|
|
|
|
|
self.series.append(self.counter, ydata)
|
|
|
|
|
self.counter += 1
|
|
|
|
|
points = self.series.pointsVector()
|
|
|
|
|
self.y_min = min(points, key=lambda point: point.y()).y()
|
|
|
|
|
self.y_max = max(points, key=lambda point: point.y()).y()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
points = self.series.pointsVector()
|
|
|
|
|
for i in range(self.xRange - 1):
|
|
|
|
|
points[i].setY(points[i + 1].y())
|
|
|
|
|
points[-1].setY(ydata)
|
|
|
|
|
self.y_min = min(points, key=lambda point: point.y()).y()
|
|
|
|
|
self.y_max = max(points, key=lambda point: point.y()).y()
|
|
|
|
|
self.series.replace(points)
|
|
|
|
|
|
2024-07-04 13:31:26 +08:00
|
|
|
|
self.axisY.setRange(math.floor(self.y_min/1.2),math.ceil( self.y_max*1.2))
|
|
|
|
|
# def handle_update(self, ydata):
|
|
|
|
|
# # 滚动更新数据点
|
|
|
|
|
# if len(self.series.pointsVector()) >= self.xRange:
|
|
|
|
|
# # 移除最旧的数据点
|
|
|
|
|
# self.series.remove(0, 1)
|
|
|
|
|
# # 添加新的数据点
|
|
|
|
|
# self.series.append(self.counter, ydata)
|
|
|
|
|
# else:
|
|
|
|
|
# self.series.append(self.counter, ydata)
|
|
|
|
|
|
|
|
|
|
# # 更新计数器
|
|
|
|
|
# self.counter += 1
|
|
|
|
|
|
|
|
|
|
# # 动态调整Y轴范围
|
|
|
|
|
# points = self.series.pointsVector()
|
|
|
|
|
# if points:
|
|
|
|
|
# self.y_min = min(points, key=lambda point: point.y()).y()
|
|
|
|
|
# self.y_max = max(points, key=lambda point: point.y()).y()
|
|
|
|
|
# self.axisY.setRange(math.floor(self.y_min * 0.9), math.ceil(self.y_max * 1.1))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#print("更新",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
2024-07-03 12:01:11 +08:00
|
|
|
|
#print(round(self.y_min/1.2),math.ceil( self.y_max*1.2))
|
|
|
|
|
|
|
|
|
|
# 使用线程不断更新波形数据
|
|
|
|
|
|
|
|
|
|
class UpdateDataThread(QThread):
|
|
|
|
|
_signal_update = pyqtSignal(float) # 信号
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super(UpdateDataThread, self).__init__(parent)
|
|
|
|
|
self.is_exit = False
|
|
|
|
|
self.x_range = 1024
|
|
|
|
|
self.getDateQx = GetDataQX()
|
|
|
|
|
self.flag = 0
|
2024-07-04 13:31:26 +08:00
|
|
|
|
|
2024-07-03 12:01:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
print("run start")
|
|
|
|
|
while not self.is_exit: # 直接在循环中检查退出标志
|
|
|
|
|
# for i in range(self.x_range):
|
|
|
|
|
# print(i,self.sin.get_data(i))
|
|
|
|
|
|
|
|
|
|
# self._signal_update.emit(str(self.sin.get_data(i))) # 发射信号
|
|
|
|
|
# time.sleep(0.01) # 实际应用中推荐使用QTimer代替
|
|
|
|
|
|
|
|
|
|
# 这里应该有一个 flag 默认是 0 外部调用可以传紧来1,没循环完自己置零,while 只起到监控左右
|
2024-07-04 13:31:26 +08:00
|
|
|
|
|
2024-07-03 12:01:11 +08:00
|
|
|
|
if self.flag == 1:
|
|
|
|
|
#print("flag",self.flag)
|
|
|
|
|
|
2024-07-04 13:31:26 +08:00
|
|
|
|
#data = self.setReceiveData
|
|
|
|
|
#print("发数",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
2024-07-03 16:31:41 +08:00
|
|
|
|
#print("data receive ",data)
|
2024-07-04 13:31:26 +08:00
|
|
|
|
# print(f"{data:.3f}")
|
2024-07-03 12:01:11 +08:00
|
|
|
|
try:
|
2024-07-03 16:31:41 +08:00
|
|
|
|
|
|
|
|
|
self._signal_update.emit(self.setReceiveData)
|
2024-07-03 12:01:11 +08:00
|
|
|
|
self.flag = 0
|
2024-07-03 16:31:41 +08:00
|
|
|
|
|
2024-07-03 12:01:11 +08:00
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
QMessageBox.critical(self, '数据异常', '数据格式不对,不能绘图')
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def SetReceiveData(self,data_in):
|
|
|
|
|
self.setReceiveData = data_in
|
|
|
|
|
|
|
|
|
|
def SetFlag(self,flag):
|
|
|
|
|
self.flag = flag
|
|
|
|
|
|
|
|
|
|
def SetFilenameCsv(self,filename):
|
|
|
|
|
self.filenameCsv = filename
|
|
|
|
|
|
|
|
|
|
def SetPlotItem(self,ind): #设置显示项目
|
|
|
|
|
self.ind = ind
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
|
self.is_exit = True # 设置退出标志
|
|
|
|
|
print("停止绘图")
|
|
|
|
|
def restart(self):
|
|
|
|
|
self.is_exit = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GetDataQX(): #读取请芯数据
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.regex = re.compile(r'(A\+|B\+|\s)+') # 编译正则表达式
|
|
|
|
|
self.TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
|
|
|
|
def Transdata(self,data):
|
|
|
|
|
parts = re.split(self.regex,data)
|
|
|
|
|
# 过滤掉空字符串
|
|
|
|
|
parts = [part for part in parts if part]
|
|
|
|
|
#print(parts)
|
|
|
|
|
methane = float(parts[1])
|
|
|
|
|
airTemp = float(parts[3])
|
|
|
|
|
laserTemp = float( parts[5])/1
|
|
|
|
|
laerIntensity = float(parts[7])*10
|
|
|
|
|
timeCrvt=time.strftime(self.TIME_FORMAT, time.localtime())
|
|
|
|
|
self.data2csv =[(timeCrvt,methane,airTemp,laserTemp,laerIntensity)]
|
|
|
|
|
#print(self.data2csv)
|
|
|
|
|
|
|
|
|
|
if self.indOfReturn == 0:
|
|
|
|
|
returnData = methane
|
|
|
|
|
elif self.indOfReturn == 1:
|
|
|
|
|
returnData = airTemp
|
|
|
|
|
elif self.indOfReturn == 2:
|
|
|
|
|
returnData = laserTemp
|
|
|
|
|
elif self.indOfReturn == 3:
|
|
|
|
|
returnData = laerIntensity
|
|
|
|
|
print(returnData)
|
|
|
|
|
return returnData
|
|
|
|
|
|
|
|
|
|
def SaveCsv(self,filenameCsv):
|
|
|
|
|
# 打开一个文件用于写入,如果文件不存在则创建
|
2024-07-04 13:31:26 +08:00
|
|
|
|
if os.path.isfile(filenameCsv) == 0:
|
|
|
|
|
# 文件为空,需要写入表头
|
|
|
|
|
with open(filenameCsv, mode='w', newline='') as file:
|
|
|
|
|
writer = csv.writer(file)
|
|
|
|
|
writer.writerow(('time', 'Methane', 'Air Temp', 'Laser Temp', 'Laser Intensity'))
|
|
|
|
|
else:
|
|
|
|
|
with open(filenameCsv, mode='a', newline='') as file:
|
2024-07-03 12:01:11 +08:00
|
|
|
|
# 创建一个写入器对象
|
2024-07-04 13:31:26 +08:00
|
|
|
|
writer = csv.writer(file)
|
|
|
|
|
# 写入数据行
|
|
|
|
|
writer.writerows(self.data2csv)
|
2024-07-03 12:01:11 +08:00
|
|
|
|
|
|
|
|
|
def IndOfReturn(self,ind):
|
|
|
|
|
self.indOfReturn = ind
|
|
|
|
|
|