Compare commits

...

2 Commits

Author SHA1 Message Date
54d4eb01e2 页面逻辑添加 2026-05-14 18:15:50 +08:00
c884a30482 页面样式调整 2026-05-14 18:15:50 +08:00
5 changed files with 148 additions and 17 deletions

View File

@@ -6,9 +6,9 @@ namespace TabletTester2025.Services
{
Task ConnectAsync();
Task<float> ReadFloatAsync(ushort startAddress);
Task WriteCoilAsync(ushort coilAddress, bool value);
Task WriteCoilAsync(ushort coilAddress, bool value);// true false
Task<bool> ReadCoilAsync(ushort coilAddress);
Task WriteRegisterAsync(ushort registerAddress, ushort value);
Task WriteRegisterAsync(ushort registerAddress, ushort value);//写寄存器地址
Task<ushort[]> ReadHoldingRegistersAsync(ushort startAddress, ushort count);
bool IsConnected { get; }
}

View File

@@ -65,7 +65,13 @@ namespace TabletTester2025.ViewModels
OpenCalibrationCommand = new AsyncRelayCommand(() => { MessageBox.Show("校准功能待实现"); return Task.CompletedTask; });
// 跳转到 PlcDataPage 页面
ShowDataCommand = new AsyncRelayCommand(() => { new ShowData().ShowDialog(); return Task.CompletedTask; });
ShowDataCommand = new AsyncRelayCommand(async () =>
{
// 用你项目里已有的PLC实例假设叫 _plcClient
var window = new ShowData(_plc);
window.ShowDialog();
});
}
private async Task ConnectToPlc()

View File

@@ -8,5 +8,7 @@ namespace TabletTester2025
{
InitializeComponent();
}
}
}

View File

@@ -5,12 +5,13 @@
WindowStartupLocation="CenterScreen"
Background="#F0F2F5">
<Window.Resources>
<!-- 卡片样式 -->
<Style x:Key="CardStyle" TargetType="Border">
<Setter Property="Background" Value="White"/>
<Setter Property="CornerRadius" Value="10"/>
<Setter Property="Margin" Value="15"/>
<Setter Property="Margin" Value="15,15,80,15"/>
<Setter Property="Padding" Value="15"/>
<Setter Property="Effect">
<Setter.Value>

View File

@@ -1,25 +1,147 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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.Shapes;
using TabletTester2025.Services;
namespace .Views
{
/// <summary>
/// ShowData.xaml 的交互逻辑
/// </summary>
public partial class ShowData : Window
{
public ShowData()
private readonly IPlcService _plc;
private CancellationTokenSource _cts;
public ShowData(IPlcService plc)
{
InitializeComponent();
_plc = plc;
Loaded += ShowData_Loaded;
Closing += ShowData_Closing;
}
private async void ShowData_Loaded(object sender, RoutedEventArgs e)
{
_cts = new CancellationTokenSource();
BindAllTextBoxWriteEvents();
await StartPlcReadLoop(_cts.Token);
}
private void ShowData_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
_cts?.Cancel();
}
// ====================== 读取 PLC ======================
private async Task StartPlcReadLoop(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
try
{
foreach (var m in _paramMappings)
{
var tb = FindName(m.TextBoxName) as TextBox;
if (tb == null) continue;
if (m.Type == PlcParamType.Int)
{
ushort[] res = await _plc.ReadHoldingRegistersAsync((ushort)m.Address, 1);
Dispatcher.Invoke(() => tb.Text = res[0].ToString());
}
else
{
ushort[] res = await _plc.ReadHoldingRegistersAsync((ushort)m.Address, 2);
float value = BitConverter.ToSingle(BitConverter.GetBytes((uint)(res[0] << 16 | res[1])), 0);
Dispatcher.Invoke(() => tb.Text = value.ToString("F2"));
}
}
}
catch { }
await Task.Delay(1000, token);
}
}
// ====================== 写入 PLC ======================
private void BindAllTextBoxWriteEvents()
{
foreach (var m in _paramMappings)
{
var tb = FindName(m.TextBoxName) as TextBox;
if (tb == null) continue;
tb.LostFocus += async (s, e) =>
{
try
{
if (m.Type == PlcParamType.Int)
{
if (int.TryParse(tb.Text, out int val))
await _plc.WriteRegisterAsync((ushort)m.Address, (ushort)val);
}
else
{
if (float.TryParse(tb.Text, out float val))
{
byte[] bytes = BitConverter.GetBytes(val);
ushort[] regs = new ushort[2];
regs[0] = (ushort)(BitConverter.ToUInt16(bytes, 2));
regs[1] = (ushort)(BitConverter.ToUInt16(bytes, 0));
await _plc.WriteRegisterAsync((ushort)m.Address, (ushort)val);
}
}
}
catch { }
};
}
}
// ====================== 地址映射表 ======================
private readonly PlcParamMapping[] _paramMappings = new[]
{
new PlcParamMapping("txt_HardnessSpeed", 300, PlcParamType.Float),
new PlcParamMapping("txt_HardnessDisplacement", 310, PlcParamType.Float),
new PlcParamMapping("txt_HardnessMotorLimit", 298, PlcParamType.Float),
new PlcParamMapping("txt_HardnessDamageThreshold", 400, PlcParamType.Float),
new PlcParamMapping("txt_MaxForceCollect", 72, PlcParamType.Float),
new PlcParamMapping("txt_BrittlenessTestTime", 410, PlcParamType.Int),
new PlcParamMapping("txt_PreBrittlenessMass", 412, PlcParamType.Int),
new PlcParamMapping("txt_PostBrittlenessMass", 414, PlcParamType.Int),
new PlcParamMapping("txt_WeightLossRate", 416, PlcParamType.Int),
new PlcParamMapping("txt_DisintegrationSpeed", 330, PlcParamType.Float),
new PlcParamMapping("txt_DisintegrationTime", 420, PlcParamType.Int),
new PlcParamMapping("txt_DissolutionTime", 430, PlcParamType.Int),
new PlcParamMapping("txt_DissolutionSamplingInterval", 432, PlcParamType.Int),
new PlcParamMapping("txt_ForceDisplay", 1314, PlcParamType.Float),
new PlcParamMapping("txt_ForceCoefficient", 1320, PlcParamType.Float),
new PlcParamMapping("txt_ForceProtection", 1322, PlcParamType.Float),
new PlcParamMapping("txt_TemperatureCoefficient", 1428, PlcParamType.Float),
new PlcParamMapping("txt_TemperatureDisplay", 1430, PlcParamType.Float),
};
}
internal class PlcParamMapping
{
public string TextBoxName { get; }
public int Address { get; }
public PlcParamType Type { get; }
public PlcParamMapping(string textBoxName, int address, PlcParamType type)
{
TextBoxName = textBoxName;
Address = address;
Type = type;
}
}
}
internal enum PlcParamType
{
Int,
Float
}
}