예제 #1
0
        /// <summary>
        /// 读取西门子PLC中该Tag的值
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public SiemensTag ReadTagValue(SiemensTag tag)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (tag is SiemensBoolOfTag)
            {
                byte[] buffer = new byte[1];
                int    resNo  =
                    specialRWConnection.ReadBlock(
                        DBType,
                        DBNumber,
                        tag.DB_Offset,
                        1,
                        ref buffer,
                        out string errText);
                SetTagValue(buffer, tag, tag.DB_Offset);
            }
            else if (tag is SiemensLengthTag)
            {
                if (tag is SiemensRealOfTag)
                {
                    SiemensRealOfTag ltag  = tag as SiemensRealOfTag;
                    float            value = 0;
                    int resNo =
                        specialRWConnection.ReadFloat(
                            DBType,
                            DBNumber,
                            ltag.DB_Offset,
                            ref value,
                            out string errText);
                    ltag.Value = value;
                }
                else
                {
                    SiemensLengthTag ltag   = tag as SiemensLengthTag;
                    byte[]           buffer = new byte[ltag.Length];
                    int resNo =
                        specialRWConnection.ReadBlock(
                            DBType,
                            DBNumber,
                            ltag.DB_Offset,
                            ltag.Length,
                            ref buffer,
                            out string errText);
                    SetTagValue(buffer, tag, tag.DB_Offset);
                }
            }

            sw.Stop();
            _log.Debug($"触发信号[{tag.Name}]的执行时长:[{sw.ElapsedMilliseconds}]毫秒");

            return(tag);
        }
예제 #2
0
        /// <summary>
        /// 设置Tag对象的值
        /// </summary>
        /// <param name="buffer">从PLC读取数据块中的内容</param>
        /// <param name="tag">Tag对象</param>
        /// <param name="beginOffset">数据块开始偏移量</param>
        private bool SetTagValue(byte[] buffer, CustomTag tag, int beginOffset)
        {
            int offset = tag.DB_Offset - beginOffset;

            if (tag is SiemensBoolOfTag)
            {
                SiemensBoolOfTag ltag = tag as SiemensBoolOfTag;

                bool newValue = Tools.GetBitValue(buffer[offset], ltag.Position);
                if (ltag.Value == newValue)
                {
                    return(false);
                }

                ltag.Value = newValue;
                _log.Trace($"[{ltag.Name}={ltag.Value}]");
                return(true);
            }
            else if (tag is SiemensByteOfTag)
            {
                SiemensByteOfTag ltag = tag as SiemensByteOfTag;

                byte newValue = buffer[offset];
                if (ltag.Value == newValue)
                {
                    return(false);
                }

                ltag.Value = newValue;
                _log.Trace($"[{ltag.Name}={ltag.Value}]");
                return(true);
            }
            else if (tag is SiemensWordOfTag)
            {
                SiemensWordOfTag ltag = tag as SiemensWordOfTag;

                ushort newValue = Tools.GetWordValue(buffer, offset);
                if (ltag.Value == newValue)
                {
                    return(false);
                }

                ltag.Value = newValue;
                _log.Trace($"[{ltag.Name}={ltag.Value}]");
                return(true);
            }
            else if (tag is SiemensIntOfTag)
            {
                SiemensIntOfTag ltag = tag as SiemensIntOfTag;

                short newValue = Tools.GetIntValue(buffer, offset);
                if (ltag.Value == newValue)
                {
                    return(false);
                }

                ltag.Value = newValue;
                _log.Trace($"[{ltag.Name}={ltag.Value}]");
                return(true);
            }
            else if (tag is SiemensDWordOfTag)
            {
                SiemensDWordOfTag ltag = tag as SiemensDWordOfTag;

                uint newValue = Tools.GetDWordValue(buffer, offset);
                if (ltag.Value == newValue)
                {
                    return(false);
                }

                ltag.Value = newValue;
                _log.Trace($"[{ltag.Name}={ltag.Value}]");
                return(true);
            }
            else if (tag is SiemensRealOfTag)
            {
                SiemensRealOfTag ltag = tag as SiemensRealOfTag;

                float newValue = Tools.GetRealValue(buffer, offset);
                if (ltag.Value == newValue)
                {
                    return(false);
                }

                ltag.Value = newValue;
                _log.Trace($"[{ltag.Name}={ltag.Value}]");
                return(true);
            }
            else if (tag is SiemensArrayCharOfTag)
            {
                SiemensArrayCharOfTag ltag = tag as SiemensArrayCharOfTag;

                string newValue = Tools.GetStringValue(buffer, offset, ltag.Length);
                if (ltag.Value == newValue)
                {
                    return(false);
                }

                ltag.Value = newValue;
                _log.Trace($"[{ltag.Name}={ltag.Value}]");
                return(true);
            }
            else
            {
                return(false);
            }
        }