public void  ReadCoilsPDU_ArgumentRange()
        {
            try
            {
                var TestingPDU = new ReadCoilsPDU(MinAddress - 1, NormQuantity);
                Assert.Fail("Min Address Error");
            }
            catch (ArgumentOutOfRangeException) { }

            try
            {
                var TestingPDU = new ReadCoilsPDU(MaxAddress + 1, NormQuantity);
                Assert.Fail("Max Address Error");
            }
            catch (ArgumentOutOfRangeException) { }

            try
            {
                var TestingPDU = new ReadCoilsPDU(NormAddress, MinQuantity - 1);
                Assert.Fail("Min Quantity Error");
            }
            catch (ArgumentOutOfRangeException) { }

            try
            {
                var TestingPDU = new ReadCoilsPDU(NormAddress, MaxQuantity + 1);
                Assert.Fail("Max Quantity Error");
            }
            catch (ArgumentOutOfRangeException) { }
        }
예제 #2
0
        public bool[] ReadCoils(IModbusDevice Device, int StartAddr, int Quantity)
        {
            if (!(Device is RTU_Device))
                throw new ArgumentException("Wrong Type", "Device");

            var PDU = new ReadCoilsPDU(StartAddr, Quantity);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU);

            return PDU.Value;
        }
        [TestMethod]        // Oficial Example 
        public void ReadCoilsPDU_DocSampleReq()
        {
            var PDU = new ReadCoilsPDU(19, 19);

            var ExpectedReq = new byte[] 
            {          
                0x01,       // Function Code
                0x00,       // Starting Address Hi
                0x13,       // Starting Address Lo
                0x00,       // Quantity of Inputs Hi
                0x13,       // Quantity of Inputs Lo
            };

            var ActualReq = PDU.BuildRequest();

            CollectionAssert.AreEqual(ExpectedReq, ActualReq, "Wrong Request");
        }
        [TestMethod]        // Handling Request on Server
        public void ReadCoilsPDU_LifeCycle()
        {
            var RandomGen = new Random();

            int Address = RandomGen.Next(MinAddress, MaxAddress);
            int Quantity = RandomGen.Next(MinQuantity, MaxQuantity);

            #region Build Server and Values
            var Server = new BaseModbusServer();
            var Values = new bool[Quantity];

            for (int i = 0; i < Quantity; i++)
            {
                Values[i] = (RandomGen.Next(2) != 0);
                Server.AddCoil(Address + i, Values[i]);
            }
            #endregion

            var ClientPDU = new ReadCoilsPDU(Address, Quantity);
            var Request = ClientPDU.BuildRequest();

            var ServerPDU = new ReadCoilsPDU();
            var ReqStatus = ServerPDU.CheckRequest(Request);

            Assert.AreEqual(Modbus.Common.Status.OK, ReqStatus, "Recognition Error");
            Assert.AreEqual(Address, ServerPDU.StartingAddress, "Whong Address recognition in CheckRequest");
            Assert.AreEqual(Quantity, ServerPDU.Quantity, "Whong Quantity recognition in CheckRequest");

            ServerPDU.Handle(Server);

            CollectionAssert.AreEqual(Values, ServerPDU.Value, "Whong Address recognition in CheckRequest");

            ClientPDU.CheckResponse(ServerPDU.BuildResponse());

            CollectionAssert.AreEqual(Values, ClientPDU.Value, "Whong Address recognition in CheckRequest");
        }
예제 #5
0
        public Status CheckRequest(byte[] Request)
        {
            if  (Request.Length < 13)    
                return Status.Error;

            Header = new MBAP(Request);

            if (Header.ProtocolID != 0)
                return Status.Error;



            switch (Request[1])
            {
                case 0x01: PDU = new ReadCoilsPDU(); break;
                case 0x02: PDU = new ReadDiscreteInputsPDU(); break;
                case 0x03: PDU = new ReadHoldingRegistersPDU(); break;
                case 0x04: PDU = new ReadInputRegistersPDU(); break;
                case 0x05: PDU = new WriteSingleCoilPDU(); break;
                case 0x06: PDU = new WriteSingleRegisterPDU(); break;
                case 0x0F: PDU = new WriteMultipleCoilsPDU(); break;
                case 0x10: PDU = new WriteMultipleRegistersPDU(); break;
                default: return Status.Error;
            }

            var PDURequest = new byte[Request.Length - 3];

            for (int i=0; i<PDURequest.Length; i++)
                PDURequest[i] = Request[i + 1];

            return PDU.CheckRequest(PDURequest);
        }
예제 #6
0
        public Status CheckRequest(byte[] Request)
        {
            if ( (Request.Length < 4) || (Request[0] != Device.Address) )    // 3 = Address + FuncCode + CRC_Lo + CRC_Hi
                return Status.Error;
           
            if (CRC.Get(Request) != 0)
                return Status.Error;

            switch (Request[1])
            {
                case 0x01: PDU = new ReadCoilsPDU(); break;
                case 0x02: PDU = new ReadDiscreteInputsPDU(); break;
                case 0x03: PDU = new ReadHoldingRegistersPDU(); break;
                case 0x04: PDU = new ReadInputRegistersPDU(); break;
                case 0x05: PDU = new WriteSingleCoilPDU(); break;
                case 0x06: PDU = new WriteSingleRegisterPDU(); break;
                case 0x0F: PDU = new WriteMultipleCoilsPDU(); break;
                case 0x10: PDU = new WriteMultipleRegistersPDU(); break;
                default: return Status.Error;
            }

            var PDURequest = new byte[Request.Length - 3];

            for (int i=0; i<PDURequest.Length; i++)
                PDURequest[i] = Request[i + 1];

            return PDU.CheckRequest(PDURequest);
        }