234 lines
8.3 KiB
C#
234 lines
8.3 KiB
C#
|
|
// using UnityEngine;
|
||
|
|
// using UnityEditor;
|
||
|
|
// using System;
|
||
|
|
|
||
|
|
// /// <summary>
|
||
|
|
// /// DCS串口通信配置和测试窗口
|
||
|
|
// /// </summary>
|
||
|
|
// public class DCSSerialConfigWindow : EditorWindow
|
||
|
|
// {
|
||
|
|
// private string portName = "COM1";
|
||
|
|
// private int baudRate = 115200;
|
||
|
|
// private bool autoConnect = true;
|
||
|
|
|
||
|
|
// private ISerialCommunicationService serialService;
|
||
|
|
// private ISystemSettingsService settingsService;
|
||
|
|
|
||
|
|
// private Vector2 scrollPosition;
|
||
|
|
// private string logText = "";
|
||
|
|
|
||
|
|
// [MenuItem("DCS/串口通信配置")]
|
||
|
|
// public static void ShowWindow()
|
||
|
|
// {
|
||
|
|
// GetWindow<DCSSerialConfigWindow>("DCS串口通信配置");
|
||
|
|
// }
|
||
|
|
|
||
|
|
// void OnEnable()
|
||
|
|
// {
|
||
|
|
// // 从系统设置加载配置
|
||
|
|
// if (Application.isPlaying)
|
||
|
|
// {
|
||
|
|
// settingsService = ServiceLocator.Get<ISystemSettingsService>();
|
||
|
|
// if (settingsService != null)
|
||
|
|
// {
|
||
|
|
// portName = settingsService.SerialPortName;
|
||
|
|
// baudRate = settingsService.SerialBaudRate;
|
||
|
|
// autoConnect = settingsService.EnableSerialCommunication;
|
||
|
|
// }
|
||
|
|
|
||
|
|
// serialService = ServiceLocator.Get<ISerialCommunicationService>();
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// void OnGUI()
|
||
|
|
// {
|
||
|
|
// GUILayout.Label("DCS串口通信配置", EditorStyles.boldLabel);
|
||
|
|
|
||
|
|
// EditorGUILayout.Space();
|
||
|
|
|
||
|
|
// // 串口配置区域
|
||
|
|
// GUILayout.Label("串口设置", EditorStyles.boldLabel);
|
||
|
|
// portName = EditorGUILayout.TextField("端口名称:", portName);
|
||
|
|
// baudRate = EditorGUILayout.IntField("波特率:", baudRate);
|
||
|
|
// autoConnect = EditorGUILayout.Toggle("自动连接:", autoConnect);
|
||
|
|
|
||
|
|
// EditorGUILayout.Space();
|
||
|
|
|
||
|
|
// // 保存配置按钮
|
||
|
|
// if (GUILayout.Button("保存配置到系统设置"))
|
||
|
|
// {
|
||
|
|
// SaveSettings();
|
||
|
|
// }
|
||
|
|
|
||
|
|
// EditorGUILayout.Space();
|
||
|
|
|
||
|
|
// // 运行时控制区域
|
||
|
|
// if (Application.isPlaying)
|
||
|
|
// {
|
||
|
|
// GUILayout.Label("运行时控制", EditorStyles.boldLabel);
|
||
|
|
|
||
|
|
// if (serialService != null)
|
||
|
|
// {
|
||
|
|
// EditorGUILayout.LabelField("连接状态:", serialService.IsConnected ? "已连接" : "未连接");
|
||
|
|
// EditorGUILayout.LabelField("握手状态:", serialService.IsHandshakeCompleted ? "已完成" : "未完成");
|
||
|
|
|
||
|
|
// EditorGUILayout.BeginHorizontal();
|
||
|
|
// if (GUILayout.Button("连接"))
|
||
|
|
// {
|
||
|
|
// TestConnection();
|
||
|
|
// }
|
||
|
|
// if (GUILayout.Button("断开"))
|
||
|
|
// {
|
||
|
|
// DisconnectSerial();
|
||
|
|
// }
|
||
|
|
// EditorGUILayout.EndHorizontal();
|
||
|
|
|
||
|
|
// EditorGUILayout.BeginHorizontal();
|
||
|
|
// if (GUILayout.Button("发送握手"))
|
||
|
|
// {
|
||
|
|
// SendHandshake();
|
||
|
|
// }
|
||
|
|
// if (GUILayout.Button("时间同步"))
|
||
|
|
// {
|
||
|
|
// SendTimeSync();
|
||
|
|
// }
|
||
|
|
// EditorGUILayout.EndHorizontal();
|
||
|
|
|
||
|
|
// EditorGUILayout.Space();
|
||
|
|
|
||
|
|
// // 测试报警按钮
|
||
|
|
// GUILayout.Label("测试报警", EditorStyles.boldLabel);
|
||
|
|
// EditorGUILayout.BeginHorizontal();
|
||
|
|
// if (GUILayout.Button("电池低电量"))
|
||
|
|
// {
|
||
|
|
// SendTestAlarm(0x21, 1);
|
||
|
|
// }
|
||
|
|
// if (GUILayout.Button("BFI异常"))
|
||
|
|
// {
|
||
|
|
// SendTestAlarm(0x32, 2);
|
||
|
|
// }
|
||
|
|
// EditorGUILayout.EndHorizontal();
|
||
|
|
|
||
|
|
// EditorGUILayout.BeginHorizontal();
|
||
|
|
// if (GUILayout.Button("激光器异常"))
|
||
|
|
// {
|
||
|
|
// SendTestAlarm(0x33, 2);
|
||
|
|
// }
|
||
|
|
// if (GUILayout.Button("通信异常"))
|
||
|
|
// {
|
||
|
|
// SendTestAlarm(0x34, 2);
|
||
|
|
// }
|
||
|
|
// EditorGUILayout.EndHorizontal();
|
||
|
|
// }
|
||
|
|
// else
|
||
|
|
// {
|
||
|
|
// EditorGUILayout.HelpBox("串口通信服务未初始化", MessageType.Warning);
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
// else
|
||
|
|
// {
|
||
|
|
// EditorGUILayout.HelpBox("请在运行时使用此功能", MessageType.Info);
|
||
|
|
// }
|
||
|
|
|
||
|
|
// EditorGUILayout.Space();
|
||
|
|
|
||
|
|
// // 协议信息区域
|
||
|
|
// GUILayout.Label("DCS协议信息", EditorStyles.boldLabel);
|
||
|
|
// EditorGUILayout.HelpBox(
|
||
|
|
// "DCS项目串口通信协议v1.0.0\\n" +
|
||
|
|
// "• 波特率: 115200 bps\\n" +
|
||
|
|
// "• 数据位: 8, 停止位: 1, 校验: None\\n" +
|
||
|
|
// "• 帧格式: AA 55 [CMD] [LEN] [DATA] [CHK] 0D 0A\\n" +
|
||
|
|
// "• 支持握手、心跳、数据上报、报警控制等功能",
|
||
|
|
// MessageType.Info);
|
||
|
|
|
||
|
|
// // 报警代码参考
|
||
|
|
// EditorGUILayout.Space();
|
||
|
|
// GUILayout.Label("报警代码参考", EditorStyles.boldLabel);
|
||
|
|
// scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(150));
|
||
|
|
|
||
|
|
// EditorGUILayout.LabelField("0x11", "电池电量低(<40%) - 低优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x21", "电池电量低(<20%) - 中优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x31", "电池电量空(<5%) - 高优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x32", "BFI数值异常 - 高优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x22", "激光器温度异常 - 中优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x33", "激光器工作异常 - 高优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x34", "通信异常 - 高优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x35", "电池故障 - 高优先级");
|
||
|
|
// EditorGUILayout.LabelField("0x12", "交流电未连接 - 低优先级");
|
||
|
|
|
||
|
|
// EditorGUILayout.EndScrollView();
|
||
|
|
// }
|
||
|
|
|
||
|
|
// private void SaveSettings()
|
||
|
|
// {
|
||
|
|
// if (Application.isPlaying && settingsService != null)
|
||
|
|
// {
|
||
|
|
// settingsService.SerialPortName = portName;
|
||
|
|
// settingsService.SerialBaudRate = baudRate;
|
||
|
|
// settingsService.EnableSerialCommunication = autoConnect;
|
||
|
|
|
||
|
|
// Debug.Log("串口设置已保存");
|
||
|
|
// }
|
||
|
|
// else
|
||
|
|
// {
|
||
|
|
// // 在编辑器模式下保存到EditorPrefs
|
||
|
|
// EditorPrefs.SetString("DCS_SerialPort", portName);
|
||
|
|
// EditorPrefs.SetInt("DCS_BaudRate", baudRate);
|
||
|
|
// EditorPrefs.SetBool("DCS_AutoConnect", autoConnect);
|
||
|
|
|
||
|
|
// Debug.Log("串口设置已保存到编辑器偏好设置");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// private void TestConnection()
|
||
|
|
// {
|
||
|
|
// if (serialService != null)
|
||
|
|
// {
|
||
|
|
// if (serialService.Connect(portName, baudRate))
|
||
|
|
// {
|
||
|
|
// Debug.Log($"正在连接串口: {portName} @ {baudRate}");
|
||
|
|
// }
|
||
|
|
// else
|
||
|
|
// {
|
||
|
|
// Debug.LogError("串口连接失败");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// private void DisconnectSerial()
|
||
|
|
// {
|
||
|
|
// if (serialService != null)
|
||
|
|
// {
|
||
|
|
// serialService.Disconnect();
|
||
|
|
// Debug.Log("串口已断开");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// private void SendHandshake()
|
||
|
|
// {
|
||
|
|
// if (serialService != null && serialService.IsConnected)
|
||
|
|
// {
|
||
|
|
// serialService.SendHandshakeRequest();
|
||
|
|
// Debug.Log("已发送握手请求");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// private void SendTimeSync()
|
||
|
|
// {
|
||
|
|
// if (serialService != null && serialService.IsConnected)
|
||
|
|
// {
|
||
|
|
// serialService.SendTimeSync(DateTime.Now);
|
||
|
|
// Debug.Log("已发送时间同步");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// private void SendTestAlarm(byte alarmCode, byte priority)
|
||
|
|
// {
|
||
|
|
// if (serialService != null && serialService.IsConnected)
|
||
|
|
// {
|
||
|
|
// serialService.SendAlarmCommand(alarmCode, priority, 1);
|
||
|
|
// Debug.Log($"已发送测试报警: 0x{alarmCode:X2}");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
// }
|