Esempio n. 1
0
        /// <summary>
        /// Read Hold Register Rely
        /// Fucntion:0x03
        /// </summary>
        /// <param name="address">40001~49999</param>
        /// <param name="count">less than 128</param>
        /// <returns></returns>
        private byte[] readHoldRegisterRely(int address, int count)
        {
            byte[] dataBuffer = new byte[count * 2];
            byte[] relyBuffer = new byte[count * 2 + 3]; //addtion SlaveId,Function,Count
            IPointMapping <ushort> _ushortMapping = _mapping as IPointMapping <ushort>;


            var ushortData = new ushort[count];

            for (int i = 0; i < count; i++)
            {
                var addressString = string.Format("{0:D5}", address + i + 1 + 40000);//地址偏移+1
                if (_mapping.Find(addressString))
                {
                    ushortData[i] = _ushortMapping.GetValue(addressString)[0];
                }
                else
                {
                    return(errorRely(FunctionCode.ReadHoldRegister, ErrorCode.LllegalDataAddress));
                };
            }
            dataBuffer = UnsafeNetConvert.UShortsToBytes(ushortData, ByteOrder.BigEndian);

            relyBuffer[0] = (byte)_salveId;
            relyBuffer[1] = (byte)FunctionCode.ReadHoldRegister;
            relyBuffer[2] = (byte)dataBuffer.Length;
            Array.Copy(dataBuffer, 0, relyBuffer, 3, dataBuffer.Length);
            return(relyBuffer);
        }
Esempio n. 2
0
 public int WriteUShorts(DeviceAddress deviceAddress, ushort[] datas)
 {
     lock (_async)
     {
         _handler = _handler++ >= 65535 ? 0 : _handler;
         var source = createWriteHeader(deviceAddress, _handler, UnsafeNetConvert.UShortsToBytes(datas, ByteOrder.BigEndian));
         return(writedata(deviceAddress, source));
     }
 }
Esempio n. 3
0
        public int WriteUShorts(DeviceAddress deviceAddress, ushort[] datas)
        {
            ushort startAddress;

            if (Function.EnableWriteRegister(deviceAddress.Address, out startAddress))
            {
                byte[] valueBytes = UnsafeNetConvert.BytesPerversion(UnsafeNetConvert.UShortsToBytes(datas, deviceAddress.ByteOrder));
                byte[] sendBytes  = getWriteMulRegisterHeader((byte)deviceAddress.SalveId, startAddress, valueBytes);
                return(writeBytes(sendBytes));
            }
            else
            {
                return(-1);
            }
        }
Esempio n. 4
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);
            }
        }