示例#1
0
        public Item <ushort>[] ReadUShorts(DeviceAddress deviceAddress, ushort length)
        {
            var datas  = readRegister(deviceAddress, length);
            var bdatas = UnsafeNetConvert.BytesToUShorts(datas, 0, length, deviceAddress.ByteOrder);

            return(NetConvert.ToItems(bdatas, 0, length));
        }
示例#2
0
        /// <summary>
        /// Write Multi Reister Rely:
        /// Function:0x10
        /// </summary>
        /// <param name="address">40001~49999</param>
        /// <param name="data">length less than 128</param>
        /// <returns></returns>
        private byte[] writeMultiReisterRely(int address, byte[] data)
        {
            byte[] relyBuffer = new byte[6];
            var    count      = data.Length / 2;
            var    value      = UnsafeNetConvert.BytesToUShorts(data, 0, count, ByteOrder.BigEndian);

            for (int i = 0; i < count; i++)
            {
                var addressString = string.Format("{0:D5}", address + i + 1 + 40000);             //地址偏移+1
                if (_mapping.SetValue(addressString, new ushort[] { value[i] }) == -1)
                {
                    return(errorRely(FunctionCode.WriteMulRegister, ErrorCode.LllegalDataAddress));
                }
            }
            relyBuffer[0] = (byte)_salveId;
            relyBuffer[1] = (byte)FunctionCode.WriteMulRegister;
            var addrByte  = UnsafeNetConvert.UShortToBytes((ushort)address, ByteOrder.BigEndian);
            var countByte = UnsafeNetConvert.UShortToBytes((ushort)count, ByteOrder.BigEndian);

            Array.Copy(addrByte, 0, relyBuffer, 2, 2);
            Array.Copy(countByte, 0, relyBuffer, 4, 2);
            return(relyBuffer);
        }
示例#3
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 <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);
            }
        }