Beispiel #1
0
        public Item <short> ReadShort(DeviceAddress deviceAddress)
        {
            var datas = readRegister(deviceAddress, 1);

            return(datas == null ? Item <short> .CreateDefault() :
                   new Item <short>()
            {
                Vaule = UnsafeNetConvert.BytesToShort(datas, 0, deviceAddress.ByteOrder), UpdateTime = DateTime.Now, Quality = QUALITIES.QUALITY_GOOD
            });
        }
Beispiel #2
0
        /// <summary>
        /// 16位无符号整形点绑定16位整形数据
        /// </summary>
        /// <param name="source">16位整形源数据</param>
        /// <param name="sourceindex">源数据点索引</param>
        /// <param name="target">目标数据,点长度为1的虚拟点</param>
        /// <param name="bindingWay">绑定方式,默认为单方向</param>
        public static bool UshortBinding(IPoint <short> source, int sourceindex, IPoint <ushort> target, BindingWay bindingWay = BindingWay.OneWay)
        {
            if (sourceindex >= source.Length)
            {
                return(false);
            }
            switch (bindingWay)
            {
            case BindingWay.OneWay:
                source.PropertyChanged += (s, p) =>
                {
                    if (int.Parse(p.PropertyName) == sourceindex)
                    {
                        byte[] temp1 = UnsafeNetConvert.ShortToBytes(source[(byte)sourceindex], ByteOrder.None);
                        ushort temp2 = UnsafeNetConvert.BytesToUShort(temp1, 0, ByteOrder.None);
                        target.ValueUpdate(temp2, 0);
                    }
                };
                return(true);

            case BindingWay.TwoWay:
                source.PropertyChanged += (s, p) =>
                {
                    if (int.Parse(p.PropertyName) == sourceindex)
                    {
                        byte[] temp1 = UnsafeNetConvert.ShortToBytes(source[(byte)sourceindex], ByteOrder.None);
                        ushort temp2 = UnsafeNetConvert.BytesToUShort(temp1, 0, ByteOrder.None);
                        target.ValueUpdate(temp2, 0);
                    }
                };
                target.PropertyChanged += (s, p) =>
                {
                    var temp1 = UnsafeNetConvert.UShortToBytes(target[0], ByteOrder.None);
                    var temp2 = UnsafeNetConvert.BytesToShort(temp1, 0, ByteOrder.None);
                    source.ValueUpdate(temp2, sourceindex);
                };
                return(true);

            case BindingWay.ReverseWay:
                target.PropertyChanged += (s, p) =>
                {
                    var temp1 = UnsafeNetConvert.UShortToBytes(target[0], ByteOrder.None);
                    var temp2 = UnsafeNetConvert.BytesToShort(temp1, 0, ByteOrder.None);
                    source.ValueUpdate(temp2, sourceindex);
                };
                return(true);

            default:
                return(false);
            }
        }
Beispiel #3
0
        private byte[] bufferRely(byte[] buffer)
        {
            byte[] getBuffer = null;
            try
            {
                if (buffer[0] == (byte)_salveId)
                {
                    switch ((FunctionCode)buffer[1])
                    {
                    case FunctionCode.ReadCoil:
                        if (buffer.Length < 6)
                        {
                            getBuffer = errorRely(FunctionCode.ReadCoil, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var count        = UnsafeNetConvert.BytesToShort(buffer, 4, ByteOrder.BigEndian);
                            getBuffer = readCoilRely(startAddress, count);
                        }
                        break;

                    case FunctionCode.ReadInputStatus:
                        if (buffer.Length < 6)
                        {
                            getBuffer = errorRely(FunctionCode.ReadInputStatus, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var count        = UnsafeNetConvert.BytesToShort(buffer, 4, ByteOrder.BigEndian);
                            getBuffer = readInStatusRely(startAddress, count);
                        }
                        break;

                    case FunctionCode.ReadInputRegister:
                        if (buffer.Length < 6)
                        {
                            getBuffer = errorRely(FunctionCode.ReadInputRegister, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var count        = UnsafeNetConvert.BytesToShort(buffer, 4, ByteOrder.BigEndian);
                            getBuffer = readInputRegisterRely(startAddress, count);
                        }
                        break;

                    case FunctionCode.ReadHoldRegister:
                        if (buffer.Length < 6)
                        {
                            getBuffer = errorRely(FunctionCode.ReadHoldRegister, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var count        = UnsafeNetConvert.BytesToShort(buffer, 4, ByteOrder.BigEndian);
                            getBuffer = readHoldRegisterRely(startAddress, count);
                        }
                        break;

                    case FunctionCode.ForceSingleCoil:
                        if (buffer.Length < 6)
                        {
                            getBuffer = errorRely(FunctionCode.ForceSingleCoil, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var data         = new byte[] { buffer[4], buffer[5] };
                            getBuffer = writeSingleCoilRely(startAddress, data);
                        }
                        break;

                    case FunctionCode.ForceMulCoil:
                        if (buffer.Length < 7 || buffer.Length < 7 + buffer[6])
                        {
                            getBuffer = errorRely(FunctionCode.ForceMulCoil, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var count        = UnsafeNetConvert.BytesToShort(buffer, 4, ByteOrder.BigEndian);
                            var data         = new byte[buffer[6]];
                            Array.Copy(buffer, 7, data, 0, buffer[6]);
                            getBuffer = writeMulColilRely(startAddress, data, count);
                        }
                        break;

                    case FunctionCode.WriteSingleRegister:
                        if (buffer.Length < 6)
                        {
                            getBuffer = errorRely(FunctionCode.WriteSingleRegister, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var data         = new byte[] { buffer[4], buffer[5] };
                            getBuffer = writeSingleReisterRely(startAddress, data);
                        }
                        break;

                    case FunctionCode.WriteMulRegister:
                        if (buffer.Length < 7 || buffer.Length < 7 + buffer[6])
                        {
                            getBuffer = errorRely(FunctionCode.WriteMulRegister, ErrorCode.LllegalData);
                        }
                        else
                        {
                            var startAddress = UnsafeNetConvert.BytesToShort(buffer, 2, ByteOrder.BigEndian);
                            var data         = new byte[buffer[6]];
                            Array.Copy(buffer, 7, data, 0, buffer[6]);
                            getBuffer = writeMultiReisterRely(startAddress, data);
                        }
                        break;

                    default:
                        getBuffer = errorRely((FunctionCode)buffer[1], ErrorCode.LllegalFuctionCode);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorLog(string.Format("Modbus buffer Rely Error:{0}", ex.ToString()));
            }
            return(getBuffer);
        }