优化项目代码
This commit is contained in:
@@ -27,15 +27,23 @@ namespace 头罩视野
|
||||
InitializeComponent();
|
||||
ModbusHelper.Connect("192.168.1.10", 502); // 连接一次,全局生效
|
||||
}
|
||||
//private void GoHome(object s, RoutedEventArgs e) => MainFrame.Content = new Views.ChangeLanguage();
|
||||
private void GoTest(object s, RoutedEventArgs e) => MainFrame.Content = new Views.PageTest();
|
||||
|
||||
// 程序退出
|
||||
protected override void OnClosed(EventArgs e)
|
||||
{
|
||||
base.OnClosed(e);
|
||||
|
||||
//ModbusHelper.Dispose();
|
||||
}
|
||||
|
||||
//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 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()); 页面相互跳转
|
||||
|
||||
private void ShowError(string msg) => MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
//private void ShowError(string msg) => MessageBox.Show(msg, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
using System.Net.Sockets;
|
||||
using 头罩视野.Services.Data;
|
||||
|
||||
using Modbus.Device;
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using Microsoft.Win32;
|
||||
using 头罩视野.Services.Data;
|
||||
|
||||
public static class ModbusHelper
|
||||
{
|
||||
// 全局唯一连接,所有页面共用
|
||||
public static TcpClient TcpClient => ModbusResourceManager.Instance.TcpClient;
|
||||
|
||||
|
||||
// 统一连接方法(全项目只调用一次)
|
||||
public static bool Connect(string ip, int port = 502)
|
||||
{
|
||||
|
||||
@@ -10,8 +10,8 @@ namespace 头罩视野
|
||||
public class TestRecord
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Time { get; set; }
|
||||
public string Date { get; set; }
|
||||
public string Time { get; set; } = string.Empty;
|
||||
public string Date { get; set; } = string.Empty;
|
||||
public double LeftEyeArea { get; set; }
|
||||
public double RightEyeArea { get; set; }
|
||||
public double BinocularArea { get; set; }
|
||||
|
||||
@@ -23,7 +23,6 @@ namespace 头罩视野.Views
|
||||
public partial class PageTest : Page
|
||||
{
|
||||
|
||||
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
|
||||
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
|
||||
DispatcherTimer _timer;
|
||||
DataChange c = new DataChange();
|
||||
@@ -32,7 +31,7 @@ namespace 头罩视野.Views
|
||||
//// 定时采集用
|
||||
private DispatcherTimer testTimer;
|
||||
// 保存上一条数据(用于去重)
|
||||
private TestDataStore.TestRecord _lastRecord;
|
||||
private TestDataStore.TestRecord? _lastRecord;
|
||||
|
||||
public PageTest()
|
||||
{
|
||||
@@ -53,10 +52,6 @@ namespace 头罩视野.Views
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取客户端
|
||||
var client = ModbusHelper.TcpClient;
|
||||
|
||||
|
||||
}
|
||||
|
||||
//复位btn
|
||||
@@ -75,11 +70,11 @@ namespace 头罩视野.Views
|
||||
ma.BtnClickFunction(Function.ButtonType.切换型, 1);
|
||||
}
|
||||
//反转
|
||||
private bool _isPressing = false;
|
||||
//private bool _isPressing = false;
|
||||
private async void Button_Click_ResDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
((Button)sender).CaptureMouse();
|
||||
_isPressing = true;
|
||||
//_isPressing = true;
|
||||
await Task.Run(() =>
|
||||
{
|
||||
//重新占位写入
|
||||
@@ -97,11 +92,11 @@ namespace 头罩视野.Views
|
||||
|
||||
}
|
||||
//正转
|
||||
private bool _isPressing1 = false;
|
||||
|
||||
private async void Button_Click_ForDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
((Button)sender).CaptureMouse();
|
||||
_isPressing1 = true;
|
||||
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
@@ -116,7 +111,6 @@ namespace 头罩视野.Views
|
||||
{
|
||||
((Button)sender).ReleaseMouseCapture();
|
||||
|
||||
_isPressing1 = false;
|
||||
System.Diagnostics.Debug.WriteLine("正传end");
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,14 @@ namespace 头罩视野.Views
|
||||
DynamicHeader();
|
||||
// 2. 调用(名字和上面的变量一致)
|
||||
// 2. 启动定时器,定时读取数据(每100ms读一次)
|
||||
StartPlcReadTimer(100);
|
||||
StartPlcReadTimer(100);
|
||||
|
||||
//// 判断连接
|
||||
if (!ModbusHelper.IsConnected)
|
||||
{
|
||||
MessageBox.Show("未连接");
|
||||
return;
|
||||
}
|
||||
}
|
||||
//动态生成表头
|
||||
void DynamicHeader()
|
||||
@@ -75,7 +82,7 @@ namespace 头罩视野.Views
|
||||
_plcReadTimer.Start();
|
||||
}
|
||||
|
||||
private void ReadPlcData(object sender, ElapsedEventArgs e)
|
||||
private void ReadPlcData(object? sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (_modbusMaster == null || !ModbusHelper.TcpClient.Connected)
|
||||
return;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
xmlns:local="clr-namespace:头罩视野.Views"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="768" d:DesignWidth="1024"
|
||||
Background="#F5F7FA" Loaded="Page_Loaded"
|
||||
Background="#F5F7FA" Loaded="Page_Loaded"
|
||||
Title="RecordPage">
|
||||
|
||||
<Page.Resources>
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace 头罩视野.Views
|
||||
public partial class RecordPage : Page
|
||||
{
|
||||
|
||||
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
|
||||
|
||||
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
|
||||
|
||||
public RecordPage()
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace 头罩视野.Views
|
||||
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
|
||||
DispatcherTimer _timer;
|
||||
DataChange c = new DataChange();
|
||||
Function ma;
|
||||
Function ma ;
|
||||
public VisiData()
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -40,8 +40,6 @@ namespace 头罩视野.Views
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取客户端
|
||||
var client = ModbusHelper.TcpClient;
|
||||
}
|
||||
//复位btn
|
||||
private void Button_Click_Reset(object sender, RoutedEventArgs e)
|
||||
@@ -61,11 +59,11 @@ namespace 头罩视野.Views
|
||||
|
||||
|
||||
//反转
|
||||
private bool _isPressing = false;
|
||||
|
||||
private async void Button_Click_ResDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
((Button)sender).CaptureMouse();
|
||||
_isPressing = true;
|
||||
|
||||
await Task.Run(() => {
|
||||
//重新占位写入
|
||||
_modbusMaster.WriteSingleCoilAsync(1, 10, true);
|
||||
@@ -77,16 +75,15 @@ namespace 头罩视野.Views
|
||||
private void Button_Click_ResUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
((Button)sender).ReleaseMouseCapture();
|
||||
_isPressing = false;
|
||||
System.Diagnostics.Debug.WriteLine("end1111");
|
||||
|
||||
}
|
||||
//正转
|
||||
private bool _isPressing1 = false;
|
||||
//private bool _isPressing1 = false;
|
||||
private async void Button_Click_ForDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
((Button)sender).CaptureMouse();
|
||||
_isPressing1 = true;
|
||||
//_isPressing1 = true;
|
||||
|
||||
await Task.Run(() => {
|
||||
//重新占位写入
|
||||
@@ -99,8 +96,7 @@ namespace 头罩视野.Views
|
||||
private void Button_Click_ForUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
((Button)sender).ReleaseMouseCapture();
|
||||
|
||||
_isPressing1 = false;
|
||||
|
||||
System.Diagnostics.Debug.WriteLine("正传end");
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,4 @@
|
||||
#pragma checksum "..\..\..\..\Views\RecordPage.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "7C25257D5B49F85310AA23991215A080D30141C5"
|
||||
#pragma checksum "..\..\..\..\Views\RecordPage.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "2C38967E29481C17659DB2B3BEE79230294D83EF"
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 此代码由工具生成。
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user