0702调试

This commit is contained in:
zjk
2025-07-02 16:20:36 +08:00
parent 16d4c25c77
commit ba8cd0383a
13 changed files with 1761 additions and 156 deletions
+215 -5
View File
@@ -23,13 +23,40 @@ class PowerManagerApp:
# 解决字体模糊问题
style = ttk.Style()
style.configure('.', font=('Microsoft YaHei', 10))
self.simulation_HighVolBusFbMsg = msg_disHighVolBusFbMsg()
self.simulation_HighAVolBusFbMsg = msg_disHighAVolBusFbMsg()
self.simulation_LowMainBusFbMsg = msg_disLowMainBusFbMsg()
self.simulation_LowBusFbMsg = msg_disLowBusFbMsg()
self.simulation_HVBCmdMsg = msg_disHVBCmdMsg()
self.simulation_HVBACmdMsg = msg_disHVBACmdMsg()
self.simulation_LVMBCmdMsg = msg_disLVMBCmdMsg()
self.simulation_LVBCmdMsg = msg_disLVBCmdMsg()
# 菜单栏复选框状态变量
self.menu_checkbox_vars = {
'Forward_Feedback': tk.BooleanVar(value=False),
'Transparent_Command': tk.BooleanVar(value=False),
'Simulation_Mode': tk.BooleanVar(value=False)
}
self.Forward_Feedback = self.menu_checkbox_vars['Forward_Feedback']
self.Transparent_Command = self.menu_checkbox_vars['Transparent_Command']
self.Simulation_Mode = self.menu_checkbox_vars['Simulation_Mode']
# 添加模拟模式回调
self.Simulation_Mode.trace_add('write', self.on_simulation_mode_changed)
self.simulation_thread = None
self.simulation_running = False
self.root.title("电源管理系统")
self.root.tk.call('tk', 'scaling', 1.5) # 适配高DPI显示
# 移除固定窗口大小设置,实现自适应
self.root.minsize(800, 600) # 设置最小窗口大小
# UDP通信配置
# 上位机端口
self.udp_control_port = 5001
self.udp_control_host = "192.168.0.223"
# 下位机端口
# self.udp_ip = "127.0.0.1"
self.udp_ip = "192.168.0.140"
self.udp_port = 7000
@@ -211,6 +238,19 @@ class PowerManagerApp:
def setup_ui(self):
"""初始化用户界面"""
# 创建菜单栏
menubar = tk.Menu(self.root)
# 添加选项菜单
options_menu = tk.Menu(menubar, tearoff=0)
options_menu.add_checkbutton(label="转发反馈", variable=self.menu_checkbox_vars['Forward_Feedback'])
options_menu.add_checkbutton(label="透传指令", variable=self.menu_checkbox_vars['Transparent_Command'])
options_menu.add_checkbutton(label="模拟模式", variable=self.menu_checkbox_vars['Simulation_Mode'])
menubar.add_cascade(label="选项", menu=options_menu)
# 配置菜单栏
self.root.config(menu=menubar)
# 主框架
main_frame = ttk.Frame(self.root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
@@ -448,7 +488,36 @@ class PowerManagerApp:
elif breaker_name == "冷却系统启动":
cmd.coolingSystem = state
# self.send_udp_command(cmd.pack())
if(self.Simulation_Mode):
if breaker_name == "燃料电池断路器":
self.simulation_HVBCmdMsg.fuelCellCircuitBreaker = state
elif breaker_name == "动力锂电池断路器":
self.simulation_HVBCmdMsg.powerLithiumBatteryCircuitBreaker = state
elif breaker_name == "推进电机断路器":
self.simulation_HVBCmdMsg.propulsionMotorCircuitBreaker = state
elif breaker_name == "锂电池组仪表断路器":
self.simulation_HVBCmdMsg.lithiumBatteryGroupInstrumentCircuitBreaker = state
elif breaker_name == "DC/DC模块断路器":
self.simulation_HVBCmdMsg.dcDc5ModuleCircuitBreaker = state
elif breaker_name == "艏高压配电盒断路器":
self.simulation_HVBCmdMsg.bowHighVoltageDistributionBoxCircuitBreaker = state
elif breaker_name == "艉FT设备45断路器":
self.simulation_HVBCmdMsg.sternFTDevice45CircuitBreaker = state
elif breaker_name == "艉舵机开关1断路器":
self.simulation_HVBCmdMsg.sternRudderSwitch1CircuitBreaker = state
elif breaker_name == "艉舵机开关2断路器":
self.simulation_HVBCmdMsg.sternRudderSwitch2CircuitBreaker = state
elif breaker_name == "FB备用断路器":
self.simulation_HVBCmdMsg.fbReservedCircuitBreaker = state
elif breaker_name == "TYZ备用断路器":
self.simulation_HVBCmdMsg.tyzReservedCircuitBreaker = state
elif breaker_name == "备用断路器":
self.simulation_HVBCmdMsg.reservedCircuitBreaker = state
elif breaker_name == "DC/DC模块启动":
self.simulation_HVBCmdMsg.dcdc5Module = state
elif breaker_name == "冷却系统启动":
self.simulation_HVBCmdMsg.coolingSystem = state
self.send_udp_command(cmd.pack(), self.disSysHostC1, self.disSysPortC1)
self.db.insertHighVolBusCmd(cmd)
if state==0x55:
@@ -473,10 +542,26 @@ class PowerManagerApp:
cmd.bowRudderControlBoxCircuitBreaker = state
elif breaker_name == "开盖启动气缸断路器":
cmd.openWaterCoverStartCylinderCircuitBreaker = state
if state==0x55:
buntu=True
else:
buntu=False
if(self.Simulation_Mode):
if breaker_name == "艏FT设备123断路器":
self.simulation_HVBACmdMsg.bowFTDevice123CircuitBreaker = state
elif breaker_name == "执行机构断路器":
self.simulation_HVBACmdMsg.actuatorCircuitBreaker = state
elif breaker_name == "桅舵机控制箱断路器":
self.simulation_HVBACmdMsg.mastSteeringGearControlBoxCircuitBreaker = state
elif breaker_name == "XCZ断路器":
self.simulation_HVBACmdMsg.xczCircuitBreaker = state
elif breaker_name == "艏舵控制箱断路器":
self.simulation_HVBACmdMsg.bowRudderControlBoxCircuitBreaker = state
elif breaker_name == "开盖启动气缸断路器":
self.simulation_HVBACmdMsg.openWaterCoverStartCylinderCircuitBreaker = state
self.log_message(f"发送动力汇流排A配电指令: {breaker_name} {'合' if buntu else '分'}")
# self.send_udp_command(cmd.pack())
self.send_udp_command(cmd.pack(), self.disSysHostC1, self.disSysPortC1)
@@ -501,6 +586,20 @@ class PowerManagerApp:
elif breaker_name == "应急锂电池组2断路器":
cmd.emergencyLithiumBatteryGroup2CircuitBreaker = state
if(self.Simulation_Mode):
if breaker_name == "锂电池组仪表断路器":
self.simulation_LVMBCmdMsg.lithiumBatteryGroupInstrumentCircuitBreaker = state
elif breaker_name == "艏低压配电盒断路器":
self.simulation_LVMBCmdMsg.bowLowVoltageDistributionBoxCircuitBreaker = state
elif breaker_name == "单元4仪表48V断路器":
self.simulation_LVMBCmdMsg.unit4InstrumentDC48VCircuitBreaker = state
elif breaker_name == "DC/C1直流配电板断路器":
self.simulation_LVMBCmdMsg.dcC1DCDistributionPanelCircuitBreaker = state
elif breaker_name == "备用断路器1":
self.simulation_LVMBCmdMsg.reservedCircuitBreaker1 = state
elif breaker_name == "备用断路器2":
self.simulation_LVMBCmdMsg.reservedCircuitBreaker2 = state
# self.send_udp_command(cmd.pack())
print(breaker_name,state)
print(cmd)
@@ -533,6 +632,24 @@ class PowerManagerApp:
elif breaker_name == "应急锂电池组1断路器":
cmd.emergencyLithiumBatteryGroup1CircuitBreaker = state
if(self.Simulation_Mode):
if breaker_name == "单元1断路器":
self.simulation_LVBCmdMsg.unit1CircuitBreaker = state
elif breaker_name == "单元2断路器":
self.simulation_LVBCmdMsg.unit2CircuitBreaker = state
elif breaker_name == "单元3断路器":
self.simulation_LVBCmdMsg.unit3CircuitBreaker = state
elif breaker_name == "单元5断路器":
self.simulation_LVBCmdMsg.unit5CircuitBreaker = state
elif breaker_name == "艏PZ设备断路器":
self.simulation_LVBCmdMsg.bowPZDeviceCircuitBreaker = state
elif breaker_name == "备用断路器1":
self.simulation_LVBCmdMsg.reservedCircuitBreaker1 = state
elif breaker_name == "备用断路器2":
self.simulation_LVBCmdMsg.reservedCircuitBreaker2 = state
elif breaker_name == "应急锂电池组1断路器":
self.simulation_LVBCmdMsg.emergencyLithiumBatteryGroup1CircuitBreaker = state
# self.send_udp_command(cmd.pack())
self.send_udp_command(cmd.pack(), self.disSysHostC2, self.disSysPortC2)
self.db.insertLowBusCmd(cmd)
@@ -548,11 +665,29 @@ class PowerManagerApp:
# print('接收中')
try:
data, addr = self.sock.recvfrom(1024)
# 转发消息到上位机
if(self.Forward_Feedback.get()):
self.send_udp_command(data, self.udp_control_host, self.udp_control_port)
self.process_udp_message(data)
# print('接收成功')
if(self.Transparent_Command):
msg_id = int.from_bytes(data[2:4], byteorder='little')
if msg_id == 0x0001:
self.send_udp_command(data, self.disSysHostC1, self.disSysPortC1)
self.simulation_HVBCmdMsg.unpack(data)
elif msg_id == 0x0002:
self.send_udp_command(data, self.disSysHostC1, self.disSysPortC1)
self.simulation_HVBACmdMsg.unpack(data)
elif msg_id == 0x0003:
self.send_udp_command(data, self.disSysHostC2, self.disSysPortC2)
self.simulation_LVMBCmdMsg.unpack(data)
elif msg_id == 0x0004:
self.send_udp_command(data, self.disSysHostC2, self.disSysPortC2)
self.simulation_LVBCmdMsg.unpack(data)
except Exception as e:
self.log_message(f"接收错误: {str(e)}")
def setup_high_vol_ui(self, parent):
"""设置高压母线状态界面"""
# 初始化故障位列表
@@ -1391,9 +1526,84 @@ class PowerManagerApp:
self.log_text.config(state=tk.DISABLED)
self.log_text.see(tk.END)
def on_simulation_mode_changed(self, *args):
"""模拟模式复选框回调"""
if self.Simulation_Mode.get():
self.start_simulation()
else:
self.stop_simulation()
def start_simulation(self):
"""启动模拟线程"""
if not self.simulation_running:
self.simulation_running = True
self.simulation_thread = threading.Thread(target=self.simulation_loop)
self.simulation_thread.daemon = True
self.simulation_thread.start()
self.log_message("模拟模式已启动")
def stop_simulation(self):
"""停止模拟线程"""
if self.simulation_running:
self.simulation_running = False
if self.simulation_thread:
self.simulation_thread.join()
self.log_message("模拟模式已停止")
def simulation_loop(self):
"""模拟线程主循环"""
while self.simulation_running:
# 发送模拟数据
self.simulation_HighVolBusFbMsg.fuelCellCircuitBreaker = self.simulation_HVBCmdMsg.fuelCellCircuitBreaker
self.simulation_HighVolBusFbMsg.powerLithiumBatteryCircuitBreaker = self.simulation_HVBCmdMsg.powerLithiumBatteryCircuitBreaker
self.simulation_HighVolBusFbMsg.propulsionMotorCircuitBreaker = self.simulation_HVBCmdMsg.propulsionMotorCircuitBreaker
self.simulation_HighVolBusFbMsg.lithiumBatteryGroupInstrumentCircuitBreaker = self.simulation_HVBCmdMsg.lithiumBatteryGroupInstrumentCircuitBreaker
self.simulation_HighVolBusFbMsg.dcDc5ModuleCircuitBreaker = self.simulation_HVBCmdMsg.dcDc5ModuleCircuitBreaker
self.simulation_HighVolBusFbMsg.bowHighVoltageDistributionBoxCircuitBreaker = self.simulation_HVBCmdMsg.bowHighVoltageDistributionBoxCircuitBreaker
self.simulation_HighVolBusFbMsg.sternFTDevice45CircuitBreaker = self.simulation_HVBCmdMsg.sternFTDevice45CircuitBreaker
self.simulation_HighVolBusFbMsg.sternRudderSwitch1CircuitBreaker = self.simulation_HVBCmdMsg.sternRudderSwitch1CircuitBreaker
self.simulation_HighVolBusFbMsg.sternRudderSwitch2CircuitBreaker = self.simulation_HVBCmdMsg.sternRudderSwitch2CircuitBreaker
self.simulation_HighVolBusFbMsg.fbReservedCircuitBreaker = self.simulation_HVBCmdMsg.fbReservedCircuitBreaker
self.simulation_HighVolBusFbMsg.tyzReservedCircuitBreaker = self.simulation_HVBCmdMsg.tyzReservedCircuitBreaker
self.simulation_HighVolBusFbMsg.reservedCircuitBreaker = self.simulation_HVBCmdMsg.reservedCircuitBreaker
self.simulation_HighAVolBusFbMsg.actuatorCircuitBreaker = self.simulation_HVBACmdMsg.actuatorCircuitBreaker
self.simulation_HighAVolBusFbMsg.bowFTDevice123CircuitBreaker = self.simulation_HVBACmdMsg.bowFTDevice123CircuitBreaker
self.simulation_HighAVolBusFbMsg.mastSteeringGearControlBoxCircuitBreaker = self.simulation_HVBACmdMsg.mastSteeringGearControlBoxCircuitBreaker
self.simulation_HighAVolBusFbMsg.xczCircuitBreaker = self.simulation_HVBACmdMsg.xczCircuitBreaker
self.simulation_HighAVolBusFbMsg.bowRudderControlBoxCircuitBreaker = self.simulation_HVBACmdMsg.bowRudderControlBoxCircuitBreaker
self.simulation_HighAVolBusFbMsg.openWaterCoverStartCylinderCircuitBreaker = self.simulation_HVBACmdMsg.openWaterCoverStartCylinderCircuitBreaker
self.simulation_LowMainBusFbMsg.lithiumBatteryGroupInstrumentCircuitBreaker = self.simulation_LVMBCmdMsg.lithiumBatteryGroupInstrumentCircuitBreaker
self.simulation_LowMainBusFbMsg.bowLowVoltageDistributionBoxCircuitBreaker = self.simulation_LVMBCmdMsg.bowLowVoltageDistributionBoxCircuitBreaker
self.simulation_LowMainBusFbMsg.unit4InstrumentDC48VCircuitBreaker = self.simulation_LVMBCmdMsg.unit4InstrumentDC48VCircuitBreaker
self.simulation_LowMainBusFbMsg.dcC1DCDistributionPanelCircuitBreaker = self.simulation_LVMBCmdMsg.dcC1DCDistributionPanelCircuitBreaker
self.simulation_LowMainBusFbMsg.reservedCircuitBreaker1 = self.simulation_LVMBCmdMsg.reservedCircuitBreaker1
self.simulation_LowMainBusFbMsg.reservedCircuitBreaker2 = self.simulation_LVMBCmdMsg.reservedCircuitBreaker2
self.simulation_LowMainBusFbMsg.emergencyLithiumBatteryGroup2CircuitBreaker = self.simulation_LVMBCmdMsg.emergencyLithiumBatteryGroup2CircuitBreaker
self.simulation_LowBusFbMsg.unit1CircuitBreaker = self.simulation_LVBCmdMsg.unit1CircuitBreaker
self.simulation_LowBusFbMsg.unit2CircuitBreaker = self.simulation_LVBCmdMsg.unit2CircuitBreaker
self.simulation_LowBusFbMsg.unit3CircuitBreaker = self.simulation_LVBCmdMsg.unit3CircuitBreaker
self.simulation_LowBusFbMsg.unit5CircuitBreaker = self.simulation_LVBCmdMsg.unit5CircuitBreaker
self.simulation_LowBusFbMsg.bowPZDeviceCircuitBreaker = self.simulation_LVBCmdMsg.bowPZDeviceCircuitBreaker
self.simulation_LowBusFbMsg.reservedCircuitBreaker1 = self.simulation_LVBCmdMsg.reservedCircuitBreaker1
self.simulation_LowBusFbMsg.reservedCircuitBreaker2 = self.simulation_LVBCmdMsg.reservedCircuitBreaker2
self.simulation_LowBusFbMsg.emergencyLithiumBatteryGroup1CircuitBreaker = self.simulation_LVBCmdMsg.emergencyLithiumBatteryGroup1CircuitBreaker
self.send_udp_command(self.simulation_HighVolBusFbMsg.pack(), self.udp_ip, self.udp_port)
time.sleep(0.01)
self.send_udp_command(self.simulation_HighAVolBusFbMsg.pack(), self.udp_ip, self.udp_port)
time.sleep(0.01)
self.send_udp_command(self.simulation_LowBusFbMsg.pack(), self.udp_ip, self.udp_port)
time.sleep(0.01)
self.send_udp_command(self.simulation_LowMainBusFbMsg.pack(), self.udp_ip, self.udp_port)
time.sleep(1) # 1秒间隔
def on_closing(self):
"""窗口关闭事件处理"""
self.running = False
self.stop_simulation()
self.sock.close()
self.root.destroy()
if __name__ == "__main__":
+55
View File
@@ -0,0 +1,55 @@
import tkinter as tk
from PowerManagerApp import PowerManagerApp
import tkinter as tk
from tkinter import ttk
import socket
import threading
import time
from db import DB
import os
from udpMsg import (
msg_disHighVolBusFbMsg,
msg_disHighAVolBusFbMsg,
msg_disLowMainBusFbMsg,
msg_disLowBusFbMsg,
msg_disHVBCmdMsg,
msg_disHVBACmdMsg,
msg_disLVMBCmdMsg,
msg_disLVBCmdMsg
)
class CcuApp:
def __init__(self, root):
self.root = root
self.root.title("CCU")
self.root.tk.call('tk', 'scaling', 1.5) # 适配高DPI显示
# 移除固定窗口大小设置,实现自适应
self.root.minsize(800, 600) # 设置最小窗口大小
self.root.geometry("800x600")
# 上位机端口
self.udp_control_port = 6000
self.udp_control_host = "127.0.0.0"
self.msg_disHighVolBusFbMsg = msg_disHighVolBusFbMsg()
self.msg_disHighAVolBusFbMsg = msg_disHighAVolBusFbMsg()
self.msg_disLowMainBusFbMsg = msg_disLowMainBusFbMsg()
self.msg_disLowBusFbMsg = msg_disLowBusFbMsg()
self.msg_disHVBCmdMsg = msg_disHVBCmdMsg()
self.msg_disHVBACmdMsg = msg_disHVBACmdMsg()
self.msg_disLVMBCmdMsg = msg_disLVMBCmdMsg()
self.msg_disLVBCmdMsg = msg_disLVBCmdMsg()
if __name__ == "__main__":
# 创建Tkinter根窗口
root = tk.Tk()
# 创建CCUApp实例
app = CcuApp(root)
# 启动主循环
root.mainloop()
+37
View File
@@ -0,0 +1,37 @@
def bit_viewer(input_data):
try:
# 定义 ANSI 转义序列颜色代码
RED = '\033[91m'
GREEN = '\033[92m'
RESET = '\033[0m'
# 尝试将输入转换为十进制整数
if input_data.startswith('0x'):
num = int(input_data, 16)
else:
num = int(input_data)
# 将整数转换为二进制字符串,去除前缀 '0b',并补全到 32 位
binary_str = format(num, '032b')
result = []
for i in range(32):
bit = binary_str[i]
# 根据位的值选择不同颜色
if bit == '1':
colored_bit = RED + bit + RESET
else:
colored_bit = GREEN + bit + RESET
result.append(f"Bit {31 - i}: {colored_bit}")
return "\n".join(result)
except ValueError:
return "输入无效,请输入有效的十进制或十六进制数。"
if __name__ == "__main__":
while True:
user_input = input("请输入十进制或十六进制数据(十六进制以 0x 开头),输入 'q' 退出:")
if user_input.lower() == 'q':
break
result = bit_viewer(user_input)
print(result)