public Item <int> ReadInt(DeviceAddress deviceAddress) { var datas = readyBytes(deviceAddress, 4); return(datas == null ? Item <int> .CreateDefault() : new Item <int>() { Vaule = UnsafeNetConvert.BytesToInt(datas, 0, deviceAddress.ByteOrder), UpdateTime = DateTime.Now, Quality = QUALITIES.QUALITY_GOOD }); }
/// <summary> /// 16位无符号整形点绑定32整形数据 /// </summary> /// <param name="source">32位整形源数据</param> /// <param name="sourceindex">源数据点索引</param> /// <param name="targets">目标数据,数组长度为2,点长度为1的虚拟点</param> /// <param name="bindingWay">绑定方式,默认为单方向</param> public static bool UshortBinding(IPoint <int> source, int sourceindex, IPoint <ushort>[] targets, ByteOrder byterOrder = ByteOrder.None, BindingWay bindingWay = BindingWay.OneWay) { if (sourceindex >= source.Length) { return(false); } if (targets.Length != 2) { return(false); } switch (bindingWay) { case BindingWay.OneWay: source.PropertyChanged += (s, p) => { if (int.Parse(p.PropertyName) == sourceindex) { byte[] temp1 = UnsafeNetConvert.IntToBytes(source[(byte)sourceindex], byterOrder); ushort[] temp2 = (byterOrder == ByteOrder.BigEndian)? UnsafeNetConvert.BytesToUShorts(temp1, 0, 2, byterOrder):UnsafeNetConvert.BytesToUShorts(temp1, 0, 2, ByteOrder.None); for (int i = 0; i < targets.Length; i++) { targets[i].ValueUpdate(temp2[i], 0); } } }; return(true); case BindingWay.TwoWay: source.PropertyChanged += (s, p) => { if (int.Parse(p.PropertyName) == sourceindex) { byte[] temp1 = UnsafeNetConvert.IntToBytes(source[(byte)sourceindex], byterOrder); ushort[] temp2 = (byterOrder == ByteOrder.BigEndian) ? UnsafeNetConvert.BytesToUShorts(temp1, 0, 2, byterOrder) : UnsafeNetConvert.BytesToUShorts(temp1, 0, 2, ByteOrder.None); for (int i = 0; i < targets.Length; i++) { targets[i].ValueUpdate(temp2[i], 0); } } }; foreach (var target in targets) { target.PropertyChanged += (s, p) => { var temp1 = (byterOrder == ByteOrder.BigEndian) ? UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, byterOrder): UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, ByteOrder.None); var temp2 = UnsafeNetConvert.BytesToInt(temp1, 0, byterOrder); source.ValueUpdate(temp2, sourceindex); }; } return(true); case BindingWay.ReverseWay: foreach (var target in targets) { target.PropertyChanged += (s, p) => { var temp1 = (byterOrder == ByteOrder.BigEndian) ? UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, byterOrder) : UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, ByteOrder.None); var temp2 = UnsafeNetConvert.BytesToInt(temp1, 0, byterOrder); source.ValueUpdate(temp2, sourceindex); }; } return(true); default: return(false); } }