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]);
            }
        }
예제 #2
0
        public override bool LoadFromConfig(XmlNode node)
        {
            var    xmlElement = (XmlElement)node;
            string _IPAddress = xmlElement.GetAttribute("IPAddress");

            PLC = new ToyopucNet
            {
                IpAddress = _IPAddress,
            };
            if (xmlElement.HasAttribute("Port"))
            {
                PLC.Port = int.TryParse(xmlElement.GetAttribute("Port"), out int port) ? port : 44818;
            }
            else
            {
                PLC.Port = 44818;
            }

            return(base.LoadFromConfig(xmlElement));
        }
 public FormToyopuc( )
 {
     InitializeComponent( );
     toyopuc_net = new ToyopucNet( );
 }