Exemplo n.º 1
0
 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" || tag.Address.Contains("#"))
             {
                 string address             = tag.Address.Split('#')[0];
                 ushort len                 = Convert.ToUInt16(tag.Address.Split('#')[1]);
                 OperateResult <string> res = PLC.ReadString(tag.Address, len, Encoding.ASCII);
                 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
             {
                 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);
     }
 }
 private void button_read_string_Click(object sender, EventArgs e)
 {
     // 读取字符串
     DemoUtils.ReadResultRender(toyopuc_net.ReadString(textBox3.Text, ushort.Parse(textBox5.Text)), textBox3.Text, textBox4);
 }