Esempio n. 1
0
        public Item <uint> ReadUInt(DeviceAddress deviceAddress)
        {
            var datas = readyBytes(deviceAddress, 4);

            return(datas == null ? Item <uint> .CreateDefault() :
                   new Item <uint>()
            {
                Vaule = UnsafeNetConvert.BytesToUInt(datas, 0, deviceAddress.ByteOrder), UpdateTime = DateTime.Now, Quality = QUALITIES.QUALITY_GOOD
            });
        }
Esempio n. 2
0
        /// <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 <uint> source, int sourceindex, IPoint <ushort>[] targets, ByteOrder byteOrder = 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.UIntToBytes(source[(byte)sourceindex], byteOrder);
                        ushort[] temp2 = (byteOrder == ByteOrder.BigEndian)?UnsafeNetConvert.BytesToUShorts(temp1, 0, 2, byteOrder): 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.UIntToBytes(source[(byte)sourceindex], byteOrder);
                        ushort[] temp2 = (byteOrder == ByteOrder.BigEndian) ? UnsafeNetConvert.BytesToUShorts(temp1, 0, 2, byteOrder)
                            : 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 = (byteOrder == ByteOrder.BigEndian) ? UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, byteOrder)
                            : UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, ByteOrder.None);
                        var temp2 = UnsafeNetConvert.BytesToUInt(temp1, 0, byteOrder);
                        source.ValueUpdate(temp2, sourceindex);
                    };
                }
                return(true);

            case BindingWay.ReverseWay:
                foreach (var target in targets)
                {
                    target.PropertyChanged += (s, p) =>
                    {
                        var temp1 = (byteOrder == ByteOrder.BigEndian) ? UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, byteOrder)
                            : UnsafeNetConvert.UShortsToBytes(new ushort[] { targets[0][0], targets[1][0] }, ByteOrder.None);
                        var temp2 = UnsafeNetConvert.BytesToUInt(temp1, 0, byteOrder);
                        source.ValueUpdate(temp2, sourceindex);
                    };
                }
                return(true);

            default:
                return(false);
            }
        }