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 button_read_short_Click(object sender, EventArgs e) { // 读取short变量 DemoUtils.ReadResultRender(toyopuc_net.ReadInt16(textBox3.Text), textBox3.Text, textBox4); }