项目代码优化
This commit is contained in:
@@ -26,7 +26,7 @@ namespace 头罩视野.Views
|
||||
|
||||
public partial class VisiData : Page
|
||||
{
|
||||
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
|
||||
|
||||
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
|
||||
DispatcherTimer _timer;
|
||||
DataChange c = new DataChange();
|
||||
@@ -34,7 +34,7 @@ namespace 头罩视野.Views
|
||||
public VisiData()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitializeModbusTcp();
|
||||
|
||||
_timer = InitDispatcherTimer();
|
||||
}
|
||||
//复位btn
|
||||
@@ -253,35 +253,7 @@ namespace 头罩视野.Views
|
||||
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 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)
|
||||
{
|
||||
_timer.Start();
|
||||
|
||||
Reference in New Issue
Block a user