Files

35 lines
1.3 KiB
C#
Raw Permalink Normal View History

2026-03-25 21:34:41 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using MembranePoreTester.Communication;
2026-03-24 20:40:26 +08:00
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace MembranePoreTester.ViewModels
{
2026-03-25 21:34:41 +08:00
public class ViewModelBase : ObservableObject
2026-03-24 20:40:26 +08:00
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
protected async Task WriteFloatAsync(ushort address, float value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
ushort high = (ushort)((bytes[0] << 8) | bytes[1]);
ushort low = (ushort)((bytes[2] << 8) | bytes[3]);
await App.PlcService.WriteSingleRegisterAsync(address, high);
await App.PlcService.WriteSingleRegisterAsync((ushort)(address + 1), low);
}
}
}