项目代码优化

This commit is contained in:
2026-04-21 10:39:07 +08:00
parent 993f75f292
commit e9c7ebe0a7

View File

@@ -26,7 +26,7 @@ namespace 头罩视野.Views
public partial class VisiData : Page public partial class VisiData : Page
{ {
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster; private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
DispatcherTimer _timer; DispatcherTimer _timer;
DataChange c = new DataChange(); DataChange c = new DataChange();
@@ -34,7 +34,7 @@ namespace 头罩视野.Views
public VisiData() public VisiData()
{ {
InitializeComponent(); InitializeComponent();
InitializeModbusTcp();
_timer = InitDispatcherTimer(); _timer = InitDispatcherTimer();
} }
//复位btn //复位btn
@@ -253,35 +253,7 @@ namespace 头罩视野.Views
private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest(); private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest();
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate(); private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
private void GoView(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordPage(); private void GoView(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordPage();
private void InitializeModbusTcp()
{
try
{
string plcIp = "192.168.1.10";
bool initSuccess = ModbusResourceManager.Instance.Init(plcIp, 502);
if (!initSuccess)
{
MessageBox.Show("连接Modbus服务器失败", "错误");
//this.Close();
return;
}
// 检查连接状态
if (_tcpClient == null || !_tcpClient.Connected)
{
MessageBox.Show("Modbus连接异常", "错误");
return;
}
}
catch (Exception ex)
{
ShowError($"Modbus初始化失败: {ex.Message}");
}
}
private void Page_Loaded(object sender, RoutedEventArgs e) private void Page_Loaded(object sender, RoutedEventArgs e)
{ {
_timer.Start(); _timer.Start();