2026-04-18 18:14:12 +08:00
|
|
|
|
//using DateRecordView;
|
2026-04-21 10:35:43 +08:00
|
|
|
|
using Modbus.Device;
|
|
|
|
|
|
using System.Net.Sockets;
|
2026-04-18 18:14:12 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
using System.Windows.Navigation;
|
|
|
|
|
|
using System.Windows.Shapes;
|
2026-04-21 10:35:43 +08:00
|
|
|
|
using 头罩视野.Services;
|
|
|
|
|
|
using 头罩视野.Services.Data;
|
2026-04-18 18:14:12 +08:00
|
|
|
|
using 头罩视野.Views;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace 头罩视野
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
|
{
|
2026-04-21 10:35:43 +08:00
|
|
|
|
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
|
2026-04-18 18:14:12 +08:00
|
|
|
|
public MainWindow()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
2026-04-21 10:35:43 +08:00
|
|
|
|
InitializeModbusTcp();
|
2026-04-18 18:14:12 +08:00
|
|
|
|
//MainFrame.Content = new Views.ChangeLanguage();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//private void GoHome(object s, RoutedEventArgs e) => MainFrame.Content = new Views.ChangeLanguage();
|
|
|
|
|
|
private void GoTest(object s, RoutedEventArgs e) => MainFrame.Content = new Views.PageTest();
|
|
|
|
|
|
private void GoRecord(object s, RoutedEventArgs e) => MainFrame.Content = new Views.RecordDate();
|
|
|
|
|
|
private void GoView(object s, RoutedEventArgs e) => MainFrame.Content = new Views.RecordPage();
|
|
|
|
|
|
|
|
|
|
|
|
private void GoVisiPage(object s, RoutedEventArgs e) => MainFrame.Content = new Views.VisiData();
|
|
|
|
|
|
|
|
|
|
|
|
//NavigationService.Navigate(new Views.RecordDate()); 页面相互跳转
|
|
|
|
|
|
|
2026-04-21 10:35:43 +08:00
|
|
|
|
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 ShowError(string msg) => MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
2026-04-18 18:14:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|