Example #1
0
        public bool UpdateBlock()
        {
            //lock (this)
            //{
            OperateResult <byte[]> read = null;

            try
            {
                if (null != ReadAreaMaxLen && ReadAreaMaxLen.Count > 0)
                {
                    foreach (var item in ReadAreaMaxLen)
                    {
                        //Stopwatch sw = new Stopwatch();

                        //sw.Start();

                        read = allenBradleyNet.Read(item.Key, (ushort)item.Value);
                        if (null == read)
                        {
                            LOG.Error("读取失败:返回的报文体为空!");
                            return(false);
                        }

                        //sunjian   2019/11/14   防止xml配置有误,read.Content为空,导致UpdateBlock返回值为false,造成ABLogixDataSource UpdateAllValue更新所有数据失败,machine所有其他值都拿不到。
                        if (read.Content is null)
                        {
                            LOG.Error($"{allenBradleyNet} {item.Key} 数据读取失败,返回报文体内容为空!");
                            continue;
                        }

                        if (ReceiveByteMaxLen[item.Key] != read.Content.Length)
                        {
                            LOG.Error("读取失败:返回的报文体byte数组长度异常!");
                            return(false);
                        }

                        if (read.IsSuccess)
                        {
                            _dbAreaBuf[item.Key] = read.Content;
                        }
                        else
                        {
                            LOG.Error("读取失败:" + read.ToMessageShowString());
                            return(false);
                        }

                        //sw.Break();
                        //LOG.Info("读取Tag点时间是:"+sw.ElapsedMilliseconds);
                    }
                    return(true);
                }

                LOG.Error("读取失败:读取集合为空!");
                return(false);
            }
            catch (Exception ex)
            {
                LOG.Error("读取失败:" + ex.Message + ex.StackTrace);
                return(false);
            }
            // }
        }
 public override object ReadTag(Tag tag)
 {
     if (tag.AccessType == TagAccessType.Read || tag.AccessType == TagAccessType.ReadWrite)
     {
         try
         {
             if (tag.TagType == "bool")
             {
                 var res = PLC.ReadBool(tag.Address);
                 if (res.IsSuccess)
                 {
                     tag.TagValue = res.Content;
                     tag.Quality  = Quality.Good;
                 }
                 else
                 {
                     tag.TagValue = null;
                     tag.Quality  = Quality.Bad;
                 }
             }
             else if (tag.TagType == "string")
             {
                 //if (tag.Address.Contains("#"))
                 //{
                 // string address = tag.Address.Split('#')[0];
                 // ushort len = Convert.ToUInt16(tag.Address.Split('#')[1]);
                 OperateResult <string> res = PLC.ReadString(tag.Address);
                 if (res.IsSuccess)
                 {
                     string strval = res.Content;//.Length >= len ? res.Content.Substring(0, len) : res.Content;
                     tag.TagValue = strval.Replace("\0", "");
                     tag.Quality  = Quality.Good;
                 }
                 else
                 {
                     tag.TagValue = null;
                     tag.Quality  = Quality.Bad;
                 }
                 //}
                 //else
                 //{
                 //    throw new Message("Invalid tag address");
                 //}
             }
             else
             {
                 //  2019年11月11日 11:41:12  夏  读取int由传入长度改为常量1
                 // ushort len = utils.GetLength(tag);
                 OperateResult <byte[]> res = PLC.Read(tag.Address, 1);
                 ConvertUtils.DecodeTagValue(tag, res);
             }
             return(tag.TagValue);
         }
         catch (Exception ex)
         {
             LOG.Error($"Datasource[{SourceName}] read error. Tag[{tag.TagName}] Address[{tag.Address}] Message[{ex.Message}]");
             tag.TagValue = null;
             tag.Quality  = Quality.Bad;
             return(tag.TagValue);
         }
     }
     else
     {
         return(null);
     }
 }