using Prism.Mvvm;
using System.Text.Json.Serialization;
namespace PLCSharp.VVMs.Connects.ModbusTcp
{
///
/// Modbus 服务端可访问地址条目
///
public class DataItem : BindableBase
{
public enum ValueInterpretationEnum
{
UInt16,
/// 两个连续寄存器组成 32 位有符号整数 (大端)
Int32,
/// 两个连续寄存器组成 32 位 IEEE 754 浮点数 (大端)
Float,
/// 每个寄存器存 2 个字符 (高字节在前)
String,
}
private ushort _Address;
///
/// Address
///
public ushort Address
{
get => _Address;
set => SetProperty(ref _Address, value);
}
private ushort _Value;
///
/// 值
///
public ushort Value
{
get => _Value;
set
{
SetProperty(ref _Value, value);
RefreshDisplay();
}
}
private ushort _HighValue;
///
/// High值
///
public ushort HighValue
{
get => _HighValue;
set
{
SetProperty(ref _HighValue, value);
}
}
private ValueInterpretationEnum _ValueInterpretation = ValueInterpretationEnum.UInt16;
///
/// 值Interpretation
///
public ValueInterpretationEnum ValueInterpretation
{
get => _ValueInterpretation;
set
{
SetProperty(ref _ValueInterpretation, value);
RefreshDisplay();
}
}
// ───────── 显示值(只读,由 RefreshDisplay 更新) ─────────
private string _DisplayValue = "0";
///
/// Display值
///
[JsonIgnore]
public string DisplayValue
{
get => _DisplayValue;
private set => SetProperty(ref _DisplayValue, value);
}
// ───────── 本地设定值(服务端:输入即解析更新当前值) ─────────
private string _InputValue;
///
/// Input值
///
public string InputValue
{
get => _InputValue;
set
{
SetProperty(ref _InputValue, value);
ParseInputValueAndRefresh();
}
}
private byte _UnitId = 1;
///
/// UnitId
///
public byte UnitId
{
get { return _UnitId; }
set { SetProperty(ref _UnitId, value); }
}
// ───────── 远程发送值(客户端:输入不更新当前值,发送时解析) ─────────
private string _SendValue = "";
///
/// 发送值
///
public string SendValue { get => _SendValue; set => SetProperty(ref _SendValue, value); }
private string _Description = "";
///
/// Description
///
public string Description
{
get => _Description;
set => SetProperty(ref _Description, value);
}
private void RefreshDisplay()
{
DisplayValue = ValueInterpretation switch
{
ValueInterpretationEnum.UInt16 => Value.ToString(),
ValueInterpretationEnum.Int32 => CombineToInt32(Value, HighValue).ToString(),
ValueInterpretationEnum.Float => CombineToFloat(Value, HighValue).ToString("R"),
ValueInterpretationEnum.String => DecodeString(Value, HighValue),
_ => Value.ToString(),
};
}
/// 解析 InputValue 到 _Value / _HighValue(发送前调用)
public void ParseInputValue()
{
if (string.IsNullOrEmpty(_InputValue)) return;
switch (ValueInterpretation)
{
case ValueInterpretationEnum.UInt16:
if (ushort.TryParse(_InputValue, out ushort uv))
_Value = uv;
break;
case ValueInterpretationEnum.Int32:
if (int.TryParse(_InputValue, out int iv))
{
uint u = (uint)iv;
_Value = (ushort)(u & 0xFFFF);
_HighValue = (ushort)((u >> 16) & 0xFFFF);
}
break;
case ValueInterpretationEnum.Float:
if (float.TryParse(_InputValue, out float fv))
{
uint raw = BitConverter.ToUInt32(BitConverter.GetBytes(fv));
_HighValue = (ushort)((raw >> 16) & 0xFFFF);
_Value = (ushort)(raw & 0xFFFF);
}
break;
case ValueInterpretationEnum.String:
{
string s = _InputValue ?? "";
char c1 = s.Length > 0 ? s[0] : '\0';
char c2 = s.Length > 1 ? s[1] : '\0';
char c3 = s.Length > 2 ? s[2] : '\0';
char c4 = s.Length > 3 ? s[3] : '\0';
_Value = (ushort)((c1 << 8) | c2);
_HighValue = (ushort)((c3 << 8) | c4);
}
break;
}
}
/// 解析 InputValue 并刷新 DisplayValue(服务端输入时调用)
public void ParseInputValueAndRefresh()
{
ParseInputValue();
RefreshDisplay();
}
/// 解析 SendValue 到 _Value / _HighValue(发送前调用,不刷新 DisplayValue)
public void ParseSendValue()
{
if (string.IsNullOrEmpty(_SendValue)) return;
string oldInput = _InputValue;
_InputValue = _SendValue;
ParseInputValue();
_InputValue = oldInput;
}
private static int CombineToInt32(ushort lo, ushort hi) => (hi << 16) | lo;
private static float CombineToFloat(ushort lo, ushort hi)
{
uint raw = (uint)((hi << 16) | lo);
return BitConverter.ToSingle(BitConverter.GetBytes(raw));
}
private static string DecodeString(ushort lo, ushort hi)
{
char c1 = (char)((lo >> 8) & 0xFF);
char c2 = (char)(lo & 0xFF);
char c3 = (char)((hi >> 8) & 0xFF);
char c4 = (char)(hi & 0xFF);
return $"{c1}{c2}{c3}{c4}";
}
/// 从服务端读取更新时直接设置原始值(仅刷新显示,不改 InputValue,不触发写入)
public void SetRawValues(ushort lo, ushort hi)
{
_Value = lo;
_HighValue = hi;
RefreshDisplay();
}
}
}