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 button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show(DemoUtils.IpAddressInputWrong);
                return;
            }

            toyopuc_net.IpAddress = textBox1.Text;

            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show(DemoUtils.PortInputWrong);
                return;
            }

            toyopuc_net.Port = port;

            toyopuc_net.ConnectClose( );

            try
            {
                OperateResult connect = toyopuc_net.ConnectServer( );
                if (connect.IsSuccess)
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess);
                    button2.Enabled = true;
                    button1.Enabled = false;
                    panel2.Enabled  = true;
                    userControlCurve1.ReadWriteNet = toyopuc_net;
                }
                else
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#3
0
 protected override bool Connect()
 {
     try
     {
         // PLC.ConnectTimeOut = 1000;
         OperateResult opres = PLC.ConnectServer();
         if (opres.IsSuccess)
         {
             LOG.Info($"Connect to ToyopucDataSource [{SourceName}] success.");
         }
         else
         {
             LOG.Warn($"Connect to ToyopucDataSource [{SourceName}] failed.");
         }
         return(opres.IsSuccess);
     }
     catch (Exception ex)
     {
         LOG.Error($"Connect to [{SourceName}] failed. Message [{ex.Message}]");
         return(false);
     }
 }