This commit is contained in:
GukSang.Jin
2026-01-16 14:36:26 +08:00
parent 1419d5da14
commit 76e85a75a9
6 changed files with 98 additions and 34 deletions

View File

@@ -62,12 +62,23 @@ namespace COFTester.Models
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
return JsonSerializer.Deserialize<AppConfig>(json) ?? new AppConfig();
var config = JsonSerializer.Deserialize<AppConfig>(json);
if (config != null)
{
System.Diagnostics.Debug.WriteLine($"[AppConfig] 已加載配置: {filePath}");
System.Diagnostics.Debug.WriteLine($"[AppConfig] CommunicationMode = {config.CommunicationMode}");
return config;
}
}
else
{
System.Diagnostics.Debug.WriteLine($"[AppConfig] 配置文件不存在: {filePath}");
System.Diagnostics.Debug.WriteLine($"[AppConfig] 使用默認配置 (Simulated 模式)");
}
}
catch
catch (Exception ex)
{
// 加載失敗,返回默認配置
System.Diagnostics.Debug.WriteLine($"[AppConfig] 加載配置失敗: {ex.Message}");
}
return new AppConfig();
}

View File

@@ -34,6 +34,8 @@ namespace COFTester.Resources
["ResetSystem"] = "重置系统",
["Connect"] = "连接",
["Disconnect"] = "断开",
["Reconnect"] = "重新连接",
["ToggleConnection"] = "连接/断开",
// 左侧面板 - 测试参数
["TestParameters"] = "测试参数",
@@ -130,6 +132,8 @@ namespace COFTester.Resources
["ResetSystem"] = "Reset System",
["Connect"] = "Connect",
["Disconnect"] = "Disconnect",
["Reconnect"] = "Reconnect",
["ToggleConnection"] = "Connect/Disconnect",
// Left Panel - Test Parameters
["TestParameters"] = "Test Parameters",
@@ -266,6 +270,8 @@ namespace COFTester.Resources
public string Connecting => GetString("Connecting");
public string Connect => GetString("Connect");
public string Disconnect => GetString("Disconnect");
public string Reconnect => GetString("Reconnect");
public string ToggleConnection => GetString("ToggleConnection");
public string CurrentTest => GetString("CurrentTest");
public string SystemReady => GetString("SystemReady");
public string Testing => GetString("Testing");
@@ -360,6 +366,8 @@ namespace COFTester.Resources
OnPropertyChanged(nameof(Connecting));
OnPropertyChanged(nameof(Connect));
OnPropertyChanged(nameof(Disconnect));
OnPropertyChanged(nameof(Reconnect));
OnPropertyChanged(nameof(ToggleConnection));
OnPropertyChanged(nameof(CurrentTest));
OnPropertyChanged(nameof(SystemReady));
OnPropertyChanged(nameof(Testing));

View File

@@ -864,15 +864,21 @@ namespace COFTester.Services
{
public static IDataAcquisitionService CreateService(AppConfig config)
{
return config.CommunicationMode?.ToUpper() switch
var mode = config.CommunicationMode?.ToUpper();
System.Diagnostics.Debug.WriteLine($"[ModbusServiceFactory] 創建服務,模式: {config.CommunicationMode} -> {mode}");
IDataAcquisitionService service = mode switch
{
"MODBUSTCP" => new ModbusTcpService(config.ModbusTcp),
"MODBUSRTU" => new ModbusRtuService(config.ModbusRtu),
"MODBUSASCII" => new ModbusAsciiService(config.ModbusRtu),
"SERIALPORT" => new SerialPortService(config.SerialPort),
"SIMULATED" => new SimulatedDataAcquisitionService(),
_ => throw new ArgumentException($"不支持的通信模式: {config.CommunicationMode}")
_ => new SimulatedDataAcquisitionService() // 默認使用模擬模式
};
System.Diagnostics.Debug.WriteLine($"[ModbusServiceFactory] 已創建服務: {service.GetType().Name}");
return service;
}
public static string[] GetAvailableSerialPorts()

View File

@@ -57,6 +57,7 @@ namespace COFTester.ViewModels
// 初始化命令
ConnectCommand = new AsyncRelayCommand(ConnectAsync, () => !IsConnected && !_isConnecting);
DisconnectCommand = new RelayCommand(Disconnect, () => IsConnected && !_isTesting);
ToggleConnectionCommand = new AsyncRelayCommand(ToggleConnectionAsync, () => !_isConnecting && !_isTesting);
StartCommand = new RelayCommand(StartTest, () => !_isTesting && IsConnected);
StopCommand = new RelayCommand(StopTest, () => _isTesting);
ResetCommand = new RelayCommand(Reset, () => !_isTesting && IsConnected);
@@ -84,10 +85,28 @@ namespace COFTester.ViewModels
_clockTimer.Tick += (s, e) => CurrentDateTime = DateTime.Now;
_clockTimer.Start();
// 自動連接(如果是模擬模式則直接連接)
if (_config.CommunicationMode?.ToUpper() == "SIMULATED")
// 根據配置自動連接
AutoConnectOnStartup();
}
/// <summary>
/// 啟動時根據配置自動連接
/// </summary>
private async void AutoConnectOnStartup()
{
var mode = _config.CommunicationMode?.ToUpper();
if (mode == "SIMULATED")
{
StatusMessage = "模擬模式 - 已就緒";
// 模擬模式直接連接
StatusMessage = "模擬模式 - 正在連接...";
await ConnectAsync();
}
else if (mode == "MODBUSTCP" || mode == "MODBUSRTU" || mode == "MODBUSASCII")
{
// Modbus 模式自動嘗試連接
StatusMessage = $"正在自動連接 {ConnectionInfo}...";
await ConnectAsync();
}
else
{
@@ -263,6 +282,11 @@ namespace COFTester.ViewModels
}
}
/// <summary>
/// 連接按鈕文字(根據連接狀態切換)
/// </summary>
public string ConnectionButtonText => IsConnected ? Lang.Disconnect : Lang.Connect;
/// <summary>
/// 所有测试记录集合
/// </summary>
@@ -363,6 +387,7 @@ namespace COFTester.ViewModels
#region Commands
public ICommand ConnectCommand { get; }
public ICommand DisconnectCommand { get; }
public ICommand ToggleConnectionCommand { get; }
public ICommand StartCommand { get; }
public ICommand StopCommand { get; }
public ICommand ResetCommand { get; }
@@ -465,6 +490,7 @@ namespace COFTester.ViewModels
{
StatusMessage = $"已連接 - {ConnectionInfo}";
OnPropertyChanged(nameof(IsConnected));
OnPropertyChanged(nameof(ConnectionButtonText));
}
else
{
@@ -484,6 +510,7 @@ namespace COFTester.ViewModels
finally
{
IsConnecting = false;
OnPropertyChanged(nameof(ConnectionButtonText));
CommandManager.InvalidateRequerySuggested();
}
}
@@ -503,6 +530,7 @@ namespace COFTester.ViewModels
_daqService.Disconnect();
StatusMessage = "已斷開連接";
OnPropertyChanged(nameof(IsConnected));
OnPropertyChanged(nameof(ConnectionButtonText));
CommandManager.InvalidateRequerySuggested();
}
catch (Exception ex)
@@ -511,6 +539,21 @@ namespace COFTester.ViewModels
}
}
/// <summary>
/// 切換連接狀態(連接/斷開)
/// </summary>
private async Task ToggleConnectionAsync()
{
if (IsConnected)
{
Disconnect();
}
else
{
await ConnectAsync();
}
}
private void StartTest()
{
if (!IsConnected)

View File

@@ -145,31 +145,27 @@
<StackPanel>
<TextBlock Text="{Binding Lang.TestControl}" FontWeight="Bold" Margin="0,0,0,10" Foreground="{StaticResource GrayBrush}"/>
<!-- 連接控制 -->
<Grid Margin="0,0,0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="{Binding Lang.Connect}" Command="{Binding ConnectCommand}" Height="36" Foreground="White" FontSize="12" Margin="0,0,3,0">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="#27AE60"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Cursor" Value="Hand"/>
</Style>
</Button.Style>
</Button>
<Button Grid.Column="1" Content="{Binding Lang.Disconnect}" Command="{Binding DisconnectCommand}" Height="36" Foreground="White" FontSize="12" Margin="3,0,0,0">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="#95A5A6"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Cursor" Value="Hand"/>
</Style>
</Button.Style>
</Button>
</Grid>
<!-- 連接控制 - 單一切換按鈕 -->
<Button Command="{Binding ToggleConnectionCommand}" Height="36" Foreground="White" FontSize="12" Margin="0,0,0,10">
<Button.Content>
<TextBlock Text="{Binding ConnectionButtonText}"/>
</Button.Content>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="#27AE60"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Cursor" Value="Hand"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="True">
<Setter Property="Background" Value="#E74C3C"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsConnecting}" Value="True">
<Setter Property="Background" Value="#F39C12"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<!-- 連接狀態顯示 -->
<Border Background="#F8F9FA" CornerRadius="4" Padding="8" Margin="0,0,0,10">

View File

@@ -1,5 +1,5 @@
{
"CommunicationMode": "Simulated",
"CommunicationMode": "ModbusRTU",
"ModbusTcp": {
"IpAddress": "192.168.1.100",
"Port": 502,