public void ToyopucNetUnitTest()
        {
            ToyopucNet plc = new ToyopucNet("127.0.0.1", 8899);

            if (!plc.ConnectServer().IsSuccess)
            {
                Console.WriteLine("无法连接PLC,将跳过单元测试。等待网络正常时,再进行测试");
                return;
            }

            // 开始单元测试,从bool类型开始测试
            string address = "M200";

            bool[] boolTmp = new bool[] { true, true, false, true, false, true, false };
            Assert.IsTrue(plc.Write(address, true).IsSuccess);
            Assert.IsTrue(plc.ReadBool(address).Content == true);
            Assert.IsTrue(plc.Write(address, boolTmp).IsSuccess);
            bool[] readBool = plc.ReadBool(address, (ushort)boolTmp.Length).Content;
            for (int i = 0; i < boolTmp.Length; i++)
            {
                Assert.IsTrue(readBool[i] == boolTmp[i]);
            }

            address = "D300";
            // short类型
            Assert.IsTrue(plc.Write(address, (short)12345).IsSuccess);
            Assert.IsTrue(plc.ReadInt16(address).Content == 12345);
            short[] shortTmp = new short[] { 123, 423, -124, 5313, 2361 };
            Assert.IsTrue(plc.Write(address, shortTmp).IsSuccess);
            short[] readShort = plc.ReadInt16(address, (ushort)shortTmp.Length).Content;
            for (int i = 0; i < readShort.Length; i++)
            {
                Assert.IsTrue(readShort[i] == shortTmp[i]);
            }

            // ushort类型
            Assert.IsTrue(plc.Write(address, (ushort)51234).IsSuccess);
            Assert.IsTrue(plc.ReadUInt16(address).Content == 51234);
            ushort[] ushortTmp = new ushort[] { 5, 231, 12354, 5313, 12352 };
            Assert.IsTrue(plc.Write(address, ushortTmp).IsSuccess);
            ushort[] readUShort = plc.ReadUInt16(address, (ushort)ushortTmp.Length).Content;
            for (int i = 0; i < ushortTmp.Length; i++)
            {
                Assert.IsTrue(readUShort[i] == ushortTmp[i]);
            }
        }
Exemplo n.º 2
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_bool_Click(object sender, EventArgs e)
 {
     // 读取bool变量
     DemoUtils.ReadResultRender(toyopuc_net.ReadBool(textBox3.Text), textBox3.Text, textBox4);
 }