0319更新

main
parent 6b5f1d4d12
commit 6a3be48317

@ -156,3 +156,32 @@ QTabWidget#areaTabWidget{
} }
QLabel#batteryLabel{
font-size: 20px;
font: bold;
border: none;
}
QProgressBar {
border: 2px solid grey;
border-radius: 5px;
text-align: center;
color: white;
}
QProgressBar::chunk {
background-color: green;
width: 20px;
}

@ -8,7 +8,7 @@ import subprocess
import qtawesome import qtawesome
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QDockWidget, QToolBar, QAction, QTabWidget, QGridLayout, QWidget, QHBoxLayout, QStackedWidget\ from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QDockWidget, QToolBar, QAction, QTabWidget, QGridLayout, QWidget, QHBoxLayout, QStackedWidget\
,QVBoxLayout ,QVBoxLayout, QProgressBar, QLabel
from PyQt5.QtCore import Qt, QTimer, QSize from PyQt5.QtCore import Qt, QTimer, QSize
from PyQt5.QtGui import QIcon, QWindow from PyQt5.QtGui import QIcon, QWindow
from utils.DBModels.BaseModel import * from utils.DBModels.BaseModel import *
@ -16,6 +16,29 @@ from model.ClientModel.Client import Client
from UI.DeviceWidget import DeviceTab from UI.DeviceWidget import DeviceTab
from model.ProjectModel.DeviceManage import DevicesManange from model.ProjectModel.DeviceManage import DevicesManange
from protocol.BatterySerial.BatteryProtocol import BatteryManange
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt, QRect, QSize
from PyQt5.QtGui import QPainter, QFont, QColor
class CustomProgressBar(QProgressBar):
def __init__(self, parent=None):
super().__init__(parent)
self.setTextVisible(False)
self.setFont(QFont("Arial", 10)) # 设置字体大小和类型
def paintEvent(self, event):
super().paintEvent(event)
painter = QPainter(self)
value = self.value()
percentage = round(value / self.maximum() * 100, 1)
text = f"剩余电量{percentage}%"
painter.drawText(self.rect(), Qt.AlignCenter, text)
def getHwndByPid(pid): def getHwndByPid(pid):
def callback(hwnd, hwnds): def callback(hwnd, hwnds):
@ -25,8 +48,15 @@ def getHwndByPid(pid):
hwnds.append(hwnd) hwnds.append(hwnd)
hwnds = [] hwnds = []
win32gui.EnumWindows(callback, hwnds) # win32gui.EnumWindows(callback, hwnds)
return hwnds[0] while True:
win32gui.EnumWindows(callback, hwnds)
if hwnds:
return hwnds[0]
# 如果没有找到窗口,则等待一段时间再尝试
time.sleep(0.1)
# return hwnds
# return hwnds[0]
class CommonHelper: class CommonHelper:
def __init__(self): def __init__(self):
@ -43,6 +73,7 @@ class MainWindow(QWidget):
super().__init__() super().__init__()
self.setObjectName("MainWindow") self.setObjectName("MainWindow")
self.devicesManange = DevicesManange() self.devicesManange = DevicesManange()
self.batteryManange = BatteryManange()
self.process = None self.process = None
self.initUI() self.initUI()
@ -50,6 +81,10 @@ class MainWindow(QWidget):
self.protocolTimer = QTimer() self.protocolTimer = QTimer()
self.protocolTimer.timeout.connect(self.readValues) self.protocolTimer.timeout.connect(self.readValues)
self.batteryTimer = QTimer()
self.batteryTimer.timeout.connect(self.refreshProgressBar)
self.batteryTimer.start(3000)
# self.toolbarWidget = QWidget() # self.toolbarWidget = QWidget()
# self.addToolBar(Qt.LeftToolBarArea, self.toolbar) # self.addToolBar(Qt.LeftToolBarArea, self.toolbar)
@ -71,9 +106,18 @@ class MainWindow(QWidget):
self.switchBtn.setCheckable(True) self.switchBtn.setCheckable(True)
self.switchBtn.clicked.connect(self.switchWidget) self.switchBtn.clicked.connect(self.switchWidget)
self.batteryProBar = CustomProgressBar()
self.batteryProBar.setRange(0, 100)
self.batteryStateLabel = QLabel()
self.batteryStateLabel.setObjectName('batteryLabel')
toolbarLayout.addWidget(self.startProtocolBtn, 1) toolbarLayout.addWidget(self.startProtocolBtn, 1)
toolbarLayout.addWidget(self.switchBtn, 1) toolbarLayout.addWidget(self.switchBtn, 1)
toolbarLayout.addWidget(QWidget(), 20) toolbarLayout.addWidget(QWidget(), 20)
# toolbarLayout.addWidget(QLabel('电量:'), 1)
toolbarLayout.addWidget(self.batteryProBar, 1)
toolbarLayout.addWidget(self.batteryStateLabel, 1)
toolbarLayout.setSpacing(20) toolbarLayout.setSpacing(20)
toolbarLayout.setContentsMargins(0, 0, 0, 0) toolbarLayout.setContentsMargins(0, 0, 0, 0)
@ -114,6 +158,8 @@ class MainWindow(QWidget):
self.setWindowIcon(QIcon('Static/zhjt.ico')) self.setWindowIcon(QIcon('Static/zhjt.ico'))
self.setWindowTitle("PROFIBUS") self.setWindowTitle("PROFIBUS")
self.refreshProgressBar()
# self.resize(800, 600) # self.resize(800, 600)
# self.showMaximized() # self.showMaximized()
@ -162,6 +208,19 @@ class MainWindow(QWidget):
# except Exception as e: # except Exception as e:
# print(e) # print(e)
def refreshProgressBar(self):
# self.temp = temp / 10 if temp > -3276.8 else float('nan') # 假设温度值需要除以10得到实际℃数
# self.current = current # mA
# self.volt = volt # mV
# self.remaintime = remaintime # 分钟
# self.capacity = capacity # mAH
# self.cappercent = cappercent # 百分比%
# self.state = state # 充放电状态
result = self.batteryManange.readBatteryInfo()
self.batteryProBar.setValue(result.cappercent)
self.batteryStateLabel.setText(result.chargingStatus)
def switchWidget(self): def switchWidget(self):
if self.switchBtn.isChecked(): if self.switchBtn.isChecked():
@ -211,6 +270,7 @@ class MainWindow(QWidget):
def closeEvent(self, event): def closeEvent(self, event):
if self.process: if self.process:
self.process.kill() self.process.kill()
self.batteryManange.close()
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication(sys.argv) app = QApplication(sys.argv)

@ -0,0 +1,69 @@
import serial
import struct
# 定义电池状态常量
BATSTATE_CHARGE_COMPLETE = 0x01
BATSTATE_CHARGE_NOW = 0x02
BATSTATE_CHARGE_LEARN = 0x10
BATSTATE_DISCHAR_NOW = 0x04
# 定义TagResult结构体对应的Python类
class TagResult:
def __init__(self, data):
fmt = '<hhiHHBB4x' # 小端序little-endian对应INT16、INT16、INT16、UINT16、UINT16、UINT8、UINT8和4个字节的预留空间
temp, current, volt, remaintime, capacity, cappercent, state, *_ = struct.unpack(fmt, data)
self.temp = temp / 10 if temp > -3276.8 else float('nan') # 假设温度值需要除以10得到实际℃数
self.current = current # mA
self.volt = volt # mV
self.remaintime = remaintime # 分钟
self.capacity = capacity # mAH
self.cappercent = cappercent # 百分比%
self.state = state # 充放电状态
@property
def chargingStatus(self):
statusString = ""
if self.state & BATSTATE_DISCHAR_NOW:
statusString = f"剩余使用时间约:{self.remaintime}分钟"
if self.state & BATSTATE_CHARGE_LEARN:
statusString += ",现在插入充电器可执行充电校准"
elif (self.state & BATSTATE_CHARGE_NOW) or (self.state & BATSTATE_CHARGE_COMPLETE):
if self.state & BATSTATE_CHARGE_NOW:
statusString = "充电中"
else:
statusString = "充电完成"
if self.state & BATSTATE_CHARGE_LEARN:
statusString += ",正在执行充电校准"
return statusString
class BatteryManange():
def __init__(self):
self.port = 'COM2' # 替换为实际的串口号
self.baudRate = 9600
try:
self.ser = serial.Serial(self.port, self.baudRate)
except:
pass
def readBatteryInfo(self):
command = b'\xAA\xFF\x55\xFF\x01\x80\xAA\xEE\x55\xEE'
self.ser.write(command)
response = self.ser.read(26)
# 检查包头和包尾是否正确
if not (response.startswith(b'\xAA\xFF\x55\xFF') and response.endswith(b'\xAA\xEE\x55\xEE')):
raise ValueError("无效的包头或包尾")
contentData = response[4:22]
# 创建TagResult对象并解析电池信息
result = TagResult(contentData)
# print(result.chargingStatus)
return result
def close(self):
self.ser.close()
Loading…
Cancel
Save