Пример #1
0
        public override bool WriteTagToRealDevice(Tag tag, object value)
        {
            lock (this)
            {
                try
                {
                    OperateResult opres = new OperateResult();
                    if (value is bool)
                    {
                        opres = PLC.Write(tag.Address, (bool)value);
                    }
                    else if (tag.TagType == "string")
                    {
                        if (tag.Address.Contains("#"))
                        {
                            string[] adds    = tag.Address.Split('#');
                            string   address = adds[0];
                            ushort   len     = Convert.ToUInt16(adds[1]);

                            string val = value.ToString();

                            if (val.Length > len)
                            {
                                val = val.Substring(0, len);
                            }

                            StringBuilder sb = new StringBuilder(val);
                            while (sb.Length < len)
                            {
                                sb.Append("\0");
                            }
                            opres = PLC.Write(address, sb.ToString());
                        }
                        else
                        {
                            opres = PLC.Write(tag.Address, value.ToString());
                        }
                    }
                    else
                    {
                        opres = PLC.Write(tag.Address, ConvertUtils.GetBytes(tag, value));
                    }

                    LOG.Info($"DataSource[{SourceName}] write tag. Tag[{tag.TagName}] Address[{tag.Address}] Value[{value.ToString()}] IsSuccess[{opres.IsSuccess}]");
                    return(true);
                }
                catch (Exception ex)
                {
                    LOG.Error($"DataSource[{SourceName}] write tag error. Tag[{tag.TagName}] Address[{tag.Address}] Value[{value.ToString()}] Message[{ex.Message}]");
                    return(false);
                }
            }
        }
        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]);
            }
        }
 private void button24_Click(object sender, EventArgs e)
 {
     // bool写入
     DemoUtils.WriteResultRender(() => toyopuc_net.Write(textBox8.Text, new bool[] { bool.Parse(textBox7.Text) }), textBox8.Text);
 }