示例#1
0
        public void TcpExceptionOverSocketTest()
        {
            var model   = new ModbusModel();
            var scanner = new ModbusTCPScanner();

            using (var server = new TcpServerModel(model, scanner))
                using (var master = ModbusMaster.TCP(IPAddress.Loopback.ToString(), server.Port))
                {
                    var ex = Assert.Throws <ModbusException>(() => H.SharedExceptionTest(master));
                    Assert.AreEqual("Modbus exception 2", ex.Message);
                    Assert.AreEqual(2, ex.Code);
                }
        }
示例#2
0
        public void TcpRetryAfterTimeoutTest()
        {
            var model   = new ModbusModel();
            var scanner = new ModbusTCPScanner();

            using (var server = new TcpServerModel(model, scanner))
                using (var master = ModbusMaster.TCP(IPAddress.Loopback.ToString(), server.Port, 50))
                {
                    server.delay = 100;
                    Assert.Throws(typeof(Exception), () => master.WriteCoil(0, 0, false));
                    //client side delay needed to eat all server side delay otherwise
                    //the added socket stream discard on write will still throw tid mismatch
                    System.Threading.Thread.Sleep(100);
                    server.delay = 0;
                    master.WriteCoil(0, 0, false);
                }
        }
示例#3
0
        public void RackSweepTest()
        {
            //http://www.opto22.com/documents/1678_Modbus_TCP_Protocol_Guide.pdf
            //required otg included in SharpOpto22
            //m0.0 = 24V
            //m0.1 = 0V
            //m0.2 = m1.2
            //m0.3 = m1.3
            //m2.1 = m3.1
            //m2.2 = m3.2
            using (var master = ModbusMaster.TCP("10.77.0.2", 502))
            {
                master.WriteCoils(1, 4, Bools(4, false));  //clear module
                Thread.Sleep(50);
                master.WriteCoils(1, 64, Bools(4, true));  //clear on latches
                master.WriteCoils(1, 128, Bools(4, true)); //clear off latches

                Assert.AreEqual(true, master.ReadCoil(1, 0));
                Assert.AreEqual(false, master.ReadCoil(1, 1));
                Assert.AreEqual(false, master.ReadCoil(1, 2));
                Assert.AreEqual(false, master.ReadCoil(1, 3));

                master.WriteCoil(1, 6, false);
                master.WriteCoil(1, 7, false);
                Thread.Sleep(50);
                Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 64 + 2, 2));
                Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 128 + 2, 2));
                Assert.AreEqual(new bool[] { false, false }, master.ReadCoils(1, 6, 2));
                Assert.AreEqual(new bool[] { true, false, false, false }, master.ReadCoils(1, 0, 4));

                master.WriteCoil(1, 6, true);
                master.WriteCoil(1, 7, false);
                Thread.Sleep(50);
                Assert.AreEqual(true, master.ReadCoil(1, 0));
                Assert.AreEqual(false, master.ReadCoil(1, 1));
                Assert.AreEqual(true, master.ReadCoil(1, 2));
                Assert.AreEqual(false, master.ReadCoil(1, 3));
                Assert.AreEqual(new bool[] { true, false }, master.ReadInputs(1, 64 + 2, 2));
                Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 128 + 2, 2));
                Assert.AreEqual(new bool[] { true, false }, master.ReadCoils(1, 6, 2));
                Assert.AreEqual(new bool[] { true, false, true, false }, master.ReadCoils(1, 0, 4));

                master.WriteCoil(1, 6, false);
                master.WriteCoil(1, 7, true);
                Thread.Sleep(50);
                Assert.AreEqual(true, master.ReadCoil(1, 0));
                Assert.AreEqual(false, master.ReadCoil(1, 1));
                Assert.AreEqual(false, master.ReadCoil(1, 2));
                Assert.AreEqual(true, master.ReadCoil(1, 3));
                Assert.AreEqual(new bool[] { true, true }, master.ReadInputs(1, 64 + 2, 2));
                Assert.AreEqual(new bool[] { true, false }, master.ReadInputs(1, 128 + 2, 2));
                Assert.AreEqual(new bool[] { false, true }, master.ReadCoils(1, 6, 2));
                Assert.AreEqual(new bool[] { true, false, false, true }, master.ReadCoils(1, 0, 4));

                master.WriteCoils(1, 64, Bools(4, true));  //clear on latches
                master.WriteCoils(1, 128, Bools(4, true)); //clear off latches
                Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 64 + 2, 2));
                Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 128 + 2, 2));


                //analog
                SetAnalog(master, 12, 0);
                SetAnalog(master, 13, 0);
                Assert.AreEqual(0, GetAnalog(master, 12));
                Assert.AreEqual(0, GetAnalog(master, 13));
                Thread.Sleep(50);
                Assert.That(0f, Is.EqualTo(GetAnalog2(master, 8)).Within(0.1));
                Assert.That(0f, Is.EqualTo(GetAnalog2(master, 9)).Within(0.1));

                SetAnalog(master, 12, 5);
                SetAnalog(master, 13, 10);
                Assert.AreEqual(5, GetAnalog(master, 12));
                Assert.AreEqual(10, GetAnalog(master, 13));
                Thread.Sleep(50);
                Assert.That(5f, Is.EqualTo(GetAnalog2(master, 8)).Within(0.1));
                Assert.That(10f, Is.EqualTo(GetAnalog2(master, 9)).Within(0.1));

                SetAnalog2(master, 12, -5);
                SetAnalog2(master, 13, -10);
                Assert.AreEqual(-5, GetAnalog(master, 12));
                Assert.AreEqual(-10, GetAnalog(master, 13));
                Thread.Sleep(50);
                Assert.That(-5f, Is.EqualTo(GetAnalog2(master, 8)).Within(0.1));
                Assert.That(-10f, Is.EqualTo(GetAnalog2(master, 9)).Within(0.1));
            }
        }