项目逻辑添加

This commit is contained in:
2026-05-15 11:13:06 +08:00
parent 0b68413bf9
commit a050c307a8
5 changed files with 25 additions and 25 deletions

View File

@@ -6,6 +6,7 @@ namespace TabletTester2025.Services
{ {
Task ConnectAsync(); Task ConnectAsync();
Task<float> ReadFloatAsync(ushort startAddress); Task<float> ReadFloatAsync(ushort startAddress);
Task<int> ReadIntAsync(ushort startAddress);
Task WriteCoilAsync(ushort coilAddress, bool value); Task WriteCoilAsync(ushort coilAddress, bool value);
Task<bool> ReadCoilAsync(ushort coilAddress); Task<bool> ReadCoilAsync(ushort coilAddress);
Task WriteRegisterAsync(ushort registerAddress, ushort value); Task WriteRegisterAsync(ushort registerAddress, ushort value);

View File

@@ -46,13 +46,20 @@ namespace TabletTester2025.Services
} }
throw new Exception($"无法连接到 PLC ({_config.IpAddress}:{_config.Port})"); throw new Exception($"无法连接到 PLC ({_config.IpAddress}:{_config.Port})");
} }
//读取寄存器返回浮点型
public async Task<float> ReadFloatAsync(ushort startAddress) public async Task<float> ReadFloatAsync(ushort startAddress)
{ {
await EnsureConnectedAsync(); await EnsureConnectedAsync();
var registers = await ReadHoldingRegistersAsync(startAddress, 2); var registers = await ReadHoldingRegistersAsync(startAddress, 2);
return UshortToFloat(registers[1], registers[0]); return UshortToFloat(registers[1], registers[0]);
} }
//读取返回整型
public async Task<int> ReadIntAsync(ushort startAddress)
{
await EnsureConnectedAsync();
var registers = await ReadHoldingRegistersAsync(startAddress, 1);
return registers[0];
}
public async Task WriteCoilAsync(ushort coilAddress, bool value) public async Task WriteCoilAsync(ushort coilAddress, bool value)
{ {

View File

@@ -27,6 +27,13 @@ namespace TabletTester2025.Services
return Task.FromResult(value); return Task.FromResult(value);
} }
// 👇 新增 ReadIntAsync 的模拟实现
public Task<int> ReadIntAsync(ushort startAddress)
{
// 模拟整数返回比如返回0-1000之间的随机数
return Task.FromResult(_rand.Next(0, 1000));
}
public Task WriteCoilAsync(ushort coilAddress, bool value) => Task.CompletedTask; public Task WriteCoilAsync(ushort coilAddress, bool value) => Task.CompletedTask;
public Task<bool> ReadCoilAsync(ushort coilAddress) public Task<bool> ReadCoilAsync(ushort coilAddress)

View File

@@ -313,7 +313,7 @@
<TextBlock Text="片剂四用仪 (硬度·脆碎度·崩解·溶出) 符合《中国药典》" <TextBlock Text="片剂四用仪 (硬度·脆碎度·崩解·溶出) 符合《中国药典》"
FontSize="22" FontWeight="Bold" Foreground="White" VerticalAlignment="Center"/> FontSize="22" FontWeight="Bold" Foreground="White" VerticalAlignment="Center"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
<Button Command="{Binding OpenSettingsCommand}" Content="⚙ 参数设置" Style="{StaticResource ActionButton}"/> <!--<Button Command="{Binding OpenSettingsCommand}" Content="⚙ 参数设置" Style="{StaticResource ActionButton}"/>-->
<Button Command="{Binding OpenHistoryCommand}" Content="📜 历史记录" Style="{StaticResource ActionButton}"/> <Button Command="{Binding OpenHistoryCommand}" Content="📜 历史记录" Style="{StaticResource ActionButton}"/>
<Button Command="{Binding OpenCalibrationCommand}" Content="🔧 校准" Style="{StaticResource ActionButton}"/> <Button Command="{Binding OpenCalibrationCommand}" Content="🔧 校准" Style="{StaticResource ActionButton}"/>
<Button Command="{Binding ExportAllCommand}" Content="📁 导出全部" Style="{StaticResource ActionButton}"/> <Button Command="{Binding ExportAllCommand}" Content="📁 导出全部" Style="{StaticResource ActionButton}"/>

View File

@@ -47,32 +47,17 @@ namespace 片剂四用仪.Views
if (m.Type == PlcParamType.Int) if (m.Type == PlcParamType.Int)
{ {
ushort[] res = await _plc.ReadHoldingRegistersAsync((ushort)m.Address, 1); int value = await _plc.ReadIntAsync((ushort)m.Address);
if (_plc.IsConnected)
{ Dispatcher.Invoke(() => tb.Text = value.ToString());
Dispatcher.Invoke(() => tb.Text = res[0].ToString());
}
else
{
Dispatcher.Invoke(() => tb.Text = "0"); // 读取失败时显示占位符
}
} }
else else
{ {
ushort[] res = await _plc.ReadHoldingRegistersAsync((ushort)m.Address, 2); float value = await _plc.ReadFloatAsync((ushort)m.Address);
// 同样增加有效性判断 Dispatcher.Invoke(() => tb.Text = value.ToString("F2"));
if (_plc.IsConnected)
{
float value = BitConverter.ToSingle(BitConverter.GetBytes((uint)(res[0] << 16 | res[1])), 0);
Dispatcher.Invoke(() => tb.Text = value.ToString("F2"));
}
else
{
Dispatcher.Invoke(() => tb.Text = "0"); // 读取失败时显示占位符
}
} }
} }
// 正常循环的Delay同样处理取消异常 // 正常循环的Delay同样处理取消异常