更新
This commit is contained in:
@@ -62,12 +62,23 @@ namespace COFTester.Models
|
|||||||
if (File.Exists(filePath))
|
if (File.Exists(filePath))
|
||||||
{
|
{
|
||||||
string json = File.ReadAllText(filePath);
|
string json = File.ReadAllText(filePath);
|
||||||
return JsonSerializer.Deserialize<AppConfig>(json) ?? new AppConfig();
|
var config = JsonSerializer.Deserialize<AppConfig>(json);
|
||||||
}
|
if (config != null)
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
{
|
||||||
// 加載失敗,返回默認配置
|
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 (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[AppConfig] 加載配置失敗: {ex.Message}");
|
||||||
}
|
}
|
||||||
return new AppConfig();
|
return new AppConfig();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ namespace COFTester.Resources
|
|||||||
["ResetSystem"] = "重置系统",
|
["ResetSystem"] = "重置系统",
|
||||||
["Connect"] = "连接",
|
["Connect"] = "连接",
|
||||||
["Disconnect"] = "断开",
|
["Disconnect"] = "断开",
|
||||||
|
["Reconnect"] = "重新连接",
|
||||||
|
["ToggleConnection"] = "连接/断开",
|
||||||
|
|
||||||
// 左侧面板 - 测试参数
|
// 左侧面板 - 测试参数
|
||||||
["TestParameters"] = "测试参数",
|
["TestParameters"] = "测试参数",
|
||||||
@@ -130,6 +132,8 @@ namespace COFTester.Resources
|
|||||||
["ResetSystem"] = "Reset System",
|
["ResetSystem"] = "Reset System",
|
||||||
["Connect"] = "Connect",
|
["Connect"] = "Connect",
|
||||||
["Disconnect"] = "Disconnect",
|
["Disconnect"] = "Disconnect",
|
||||||
|
["Reconnect"] = "Reconnect",
|
||||||
|
["ToggleConnection"] = "Connect/Disconnect",
|
||||||
|
|
||||||
// Left Panel - Test Parameters
|
// Left Panel - Test Parameters
|
||||||
["TestParameters"] = "Test Parameters",
|
["TestParameters"] = "Test Parameters",
|
||||||
@@ -266,6 +270,8 @@ namespace COFTester.Resources
|
|||||||
public string Connecting => GetString("Connecting");
|
public string Connecting => GetString("Connecting");
|
||||||
public string Connect => GetString("Connect");
|
public string Connect => GetString("Connect");
|
||||||
public string Disconnect => GetString("Disconnect");
|
public string Disconnect => GetString("Disconnect");
|
||||||
|
public string Reconnect => GetString("Reconnect");
|
||||||
|
public string ToggleConnection => GetString("ToggleConnection");
|
||||||
public string CurrentTest => GetString("CurrentTest");
|
public string CurrentTest => GetString("CurrentTest");
|
||||||
public string SystemReady => GetString("SystemReady");
|
public string SystemReady => GetString("SystemReady");
|
||||||
public string Testing => GetString("Testing");
|
public string Testing => GetString("Testing");
|
||||||
@@ -360,6 +366,8 @@ namespace COFTester.Resources
|
|||||||
OnPropertyChanged(nameof(Connecting));
|
OnPropertyChanged(nameof(Connecting));
|
||||||
OnPropertyChanged(nameof(Connect));
|
OnPropertyChanged(nameof(Connect));
|
||||||
OnPropertyChanged(nameof(Disconnect));
|
OnPropertyChanged(nameof(Disconnect));
|
||||||
|
OnPropertyChanged(nameof(Reconnect));
|
||||||
|
OnPropertyChanged(nameof(ToggleConnection));
|
||||||
OnPropertyChanged(nameof(CurrentTest));
|
OnPropertyChanged(nameof(CurrentTest));
|
||||||
OnPropertyChanged(nameof(SystemReady));
|
OnPropertyChanged(nameof(SystemReady));
|
||||||
OnPropertyChanged(nameof(Testing));
|
OnPropertyChanged(nameof(Testing));
|
||||||
|
|||||||
@@ -864,15 +864,21 @@ namespace COFTester.Services
|
|||||||
{
|
{
|
||||||
public static IDataAcquisitionService CreateService(AppConfig config)
|
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),
|
"MODBUSTCP" => new ModbusTcpService(config.ModbusTcp),
|
||||||
"MODBUSRTU" => new ModbusRtuService(config.ModbusRtu),
|
"MODBUSRTU" => new ModbusRtuService(config.ModbusRtu),
|
||||||
"MODBUSASCII" => new ModbusAsciiService(config.ModbusRtu),
|
"MODBUSASCII" => new ModbusAsciiService(config.ModbusRtu),
|
||||||
"SERIALPORT" => new SerialPortService(config.SerialPort),
|
"SERIALPORT" => new SerialPortService(config.SerialPort),
|
||||||
"SIMULATED" => new SimulatedDataAcquisitionService(),
|
"SIMULATED" => new SimulatedDataAcquisitionService(),
|
||||||
_ => throw new ArgumentException($"不支持的通信模式: {config.CommunicationMode}")
|
_ => new SimulatedDataAcquisitionService() // 默認使用模擬模式
|
||||||
};
|
};
|
||||||
|
|
||||||
|
System.Diagnostics.Debug.WriteLine($"[ModbusServiceFactory] 已創建服務: {service.GetType().Name}");
|
||||||
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string[] GetAvailableSerialPorts()
|
public static string[] GetAvailableSerialPorts()
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ namespace COFTester.ViewModels
|
|||||||
// 初始化命令
|
// 初始化命令
|
||||||
ConnectCommand = new AsyncRelayCommand(ConnectAsync, () => !IsConnected && !_isConnecting);
|
ConnectCommand = new AsyncRelayCommand(ConnectAsync, () => !IsConnected && !_isConnecting);
|
||||||
DisconnectCommand = new RelayCommand(Disconnect, () => IsConnected && !_isTesting);
|
DisconnectCommand = new RelayCommand(Disconnect, () => IsConnected && !_isTesting);
|
||||||
|
ToggleConnectionCommand = new AsyncRelayCommand(ToggleConnectionAsync, () => !_isConnecting && !_isTesting);
|
||||||
StartCommand = new RelayCommand(StartTest, () => !_isTesting && IsConnected);
|
StartCommand = new RelayCommand(StartTest, () => !_isTesting && IsConnected);
|
||||||
StopCommand = new RelayCommand(StopTest, () => _isTesting);
|
StopCommand = new RelayCommand(StopTest, () => _isTesting);
|
||||||
ResetCommand = new RelayCommand(Reset, () => !_isTesting && IsConnected);
|
ResetCommand = new RelayCommand(Reset, () => !_isTesting && IsConnected);
|
||||||
@@ -84,10 +85,28 @@ namespace COFTester.ViewModels
|
|||||||
_clockTimer.Tick += (s, e) => CurrentDateTime = DateTime.Now;
|
_clockTimer.Tick += (s, e) => CurrentDateTime = DateTime.Now;
|
||||||
_clockTimer.Start();
|
_clockTimer.Start();
|
||||||
|
|
||||||
// 自動連接(如果是模擬模式則直接連接)
|
// 根據配置自動連接
|
||||||
if (_config.CommunicationMode?.ToUpper() == "SIMULATED")
|
AutoConnectOnStartup();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 啟動時根據配置自動連接
|
||||||
|
/// </summary>
|
||||||
|
private async void AutoConnectOnStartup()
|
||||||
{
|
{
|
||||||
StatusMessage = "模擬模式 - 已就緒";
|
var mode = _config.CommunicationMode?.ToUpper();
|
||||||
|
|
||||||
|
if (mode == "SIMULATED")
|
||||||
|
{
|
||||||
|
// 模擬模式直接連接
|
||||||
|
StatusMessage = "模擬模式 - 正在連接...";
|
||||||
|
await ConnectAsync();
|
||||||
|
}
|
||||||
|
else if (mode == "MODBUSTCP" || mode == "MODBUSRTU" || mode == "MODBUSASCII")
|
||||||
|
{
|
||||||
|
// Modbus 模式自動嘗試連接
|
||||||
|
StatusMessage = $"正在自動連接 {ConnectionInfo}...";
|
||||||
|
await ConnectAsync();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -263,6 +282,11 @@ namespace COFTester.ViewModels
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 連接按鈕文字(根據連接狀態切換)
|
||||||
|
/// </summary>
|
||||||
|
public string ConnectionButtonText => IsConnected ? Lang.Disconnect : Lang.Connect;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 所有测试记录集合
|
/// 所有测试记录集合
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -363,6 +387,7 @@ namespace COFTester.ViewModels
|
|||||||
#region Commands
|
#region Commands
|
||||||
public ICommand ConnectCommand { get; }
|
public ICommand ConnectCommand { get; }
|
||||||
public ICommand DisconnectCommand { get; }
|
public ICommand DisconnectCommand { get; }
|
||||||
|
public ICommand ToggleConnectionCommand { get; }
|
||||||
public ICommand StartCommand { get; }
|
public ICommand StartCommand { get; }
|
||||||
public ICommand StopCommand { get; }
|
public ICommand StopCommand { get; }
|
||||||
public ICommand ResetCommand { get; }
|
public ICommand ResetCommand { get; }
|
||||||
@@ -465,6 +490,7 @@ namespace COFTester.ViewModels
|
|||||||
{
|
{
|
||||||
StatusMessage = $"已連接 - {ConnectionInfo}";
|
StatusMessage = $"已連接 - {ConnectionInfo}";
|
||||||
OnPropertyChanged(nameof(IsConnected));
|
OnPropertyChanged(nameof(IsConnected));
|
||||||
|
OnPropertyChanged(nameof(ConnectionButtonText));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -484,6 +510,7 @@ namespace COFTester.ViewModels
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
IsConnecting = false;
|
IsConnecting = false;
|
||||||
|
OnPropertyChanged(nameof(ConnectionButtonText));
|
||||||
CommandManager.InvalidateRequerySuggested();
|
CommandManager.InvalidateRequerySuggested();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -503,6 +530,7 @@ namespace COFTester.ViewModels
|
|||||||
_daqService.Disconnect();
|
_daqService.Disconnect();
|
||||||
StatusMessage = "已斷開連接";
|
StatusMessage = "已斷開連接";
|
||||||
OnPropertyChanged(nameof(IsConnected));
|
OnPropertyChanged(nameof(IsConnected));
|
||||||
|
OnPropertyChanged(nameof(ConnectionButtonText));
|
||||||
CommandManager.InvalidateRequerySuggested();
|
CommandManager.InvalidateRequerySuggested();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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()
|
private void StartTest()
|
||||||
{
|
{
|
||||||
if (!IsConnected)
|
if (!IsConnected)
|
||||||
|
|||||||
@@ -145,31 +145,27 @@
|
|||||||
<StackPanel>
|
<StackPanel>
|
||||||
<TextBlock Text="{Binding Lang.TestControl}" FontWeight="Bold" Margin="0,0,0,10" Foreground="{StaticResource GrayBrush}"/>
|
<TextBlock Text="{Binding Lang.TestControl}" FontWeight="Bold" Margin="0,0,0,10" Foreground="{StaticResource GrayBrush}"/>
|
||||||
|
|
||||||
<!-- 連接控制 -->
|
<!-- 連接控制 - 單一切換按鈕 -->
|
||||||
<Grid Margin="0,0,0,10">
|
<Button Command="{Binding ToggleConnectionCommand}" Height="36" Foreground="White" FontSize="12" Margin="0,0,0,10">
|
||||||
<Grid.ColumnDefinitions>
|
<Button.Content>
|
||||||
<ColumnDefinition Width="*"/>
|
<TextBlock Text="{Binding ConnectionButtonText}"/>
|
||||||
<ColumnDefinition Width="*"/>
|
</Button.Content>
|
||||||
</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>
|
<Button.Style>
|
||||||
<Style TargetType="Button">
|
<Style TargetType="Button">
|
||||||
<Setter Property="Background" Value="#27AE60"/>
|
<Setter Property="Background" Value="#27AE60"/>
|
||||||
<Setter Property="BorderThickness" Value="0"/>
|
<Setter Property="BorderThickness" Value="0"/>
|
||||||
<Setter Property="Cursor" Value="Hand"/>
|
<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>
|
</Style>
|
||||||
</Button.Style>
|
</Button.Style>
|
||||||
</Button>
|
</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>
|
|
||||||
|
|
||||||
<!-- 連接狀態顯示 -->
|
<!-- 連接狀態顯示 -->
|
||||||
<Border Background="#F8F9FA" CornerRadius="4" Padding="8" Margin="0,0,0,10">
|
<Border Background="#F8F9FA" CornerRadius="4" Padding="8" Margin="0,0,0,10">
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"CommunicationMode": "Simulated",
|
"CommunicationMode": "ModbusRTU",
|
||||||
"ModbusTcp": {
|
"ModbusTcp": {
|
||||||
"IpAddress": "192.168.1.100",
|
"IpAddress": "192.168.1.100",
|
||||||
"Port": 502,
|
"Port": 502,
|
||||||
|
|||||||
Reference in New Issue
Block a user