[TestMethod]        // Handling Request on Server
        public void WriteSingleRegisterPDU_LifeCycle()
        {
            var RandomGen = new Random();

            int Address = RandomGen.Next(MinAddress, MaxAddress);
            var Value = RandomGen.Next(0xFFFF);

            var Server = new BaseModbusServer();


            Server.AddHoldingRegister(Address);

            var ClientPDU = new WriteSingleRegisterPDU(Address, Value);
            var Request = ClientPDU.BuildRequest();

            var ServerPDU = new WriteSingleRegisterPDU();
            ServerPDU.CheckRequest(Request);

            Assert.AreEqual(Address, ServerPDU.StartingAddress, "Whong Address recognition in CheckRequest");

            ServerPDU.Handle(Server);
            Assert.AreEqual(Value, ServerPDU.Value, "Whong Address recognition in CheckRequest");

            ClientPDU.CheckResponse(ServerPDU.BuildResponse());
            Assert.AreEqual(Value, ClientPDU.Value, "Whong Address recognition in CheckRequest");
        }
        [TestMethod]        // Oficial Example 
        public void WriteSingleRegisterPDU_DocSampleReq()
        {
            var PDU = new WriteSingleRegisterPDU(1, 3);

            var ExpectedReq = new byte[] 
            {          
                0x06,           // Function Code
                0x00,   0x01,   // Starting Address
                0x00,   0x03,   // Value
            };

            var OkResponse = new byte[]
            {
                0x06,           // Function Code
                0x00,   0x01,   // Starting Address
                0x00,   0x03,   // Value
            };

            var ExResponse = new byte[]
            {
                0x86,
                0x01,
            };

            CollectionAssert.AreEqual(ExpectedReq, PDU.BuildRequest(), "Wrong Request");
            Assert.AreEqual(Status.OK, PDU.CheckResponse(OkResponse), "OK status not recognited");
            Assert.AreEqual(Status.Exception, PDU.CheckResponse(ExResponse), "Exception status not recognited");


        }
        public void WriteSingleRegisterPDU_ArgumentRange()
        {
            IProtocolDataUnit TestingPDU;

            try
            {
                TestingPDU = new WriteSingleRegisterPDU(MinAddress - 1, 5);
                Assert.Fail("Min Address Error");
            }
            catch (ArgumentOutOfRangeException) { }

            try
            {
                TestingPDU = new WriteSingleRegisterPDU(MaxAddress + 1, 5);
                Assert.Fail("Max Address Error");
            }
            catch (ArgumentOutOfRangeException) { }
        }
Exemple #4
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);
        }
Exemple #5
0
        public void WriteSingleRegister(IModbusDevice Device, int StartAddr, int Value)
        {
            if (!(Device is RTU_Device))
                throw new ArgumentException("Wrong Type", "Device");

            var PDU = new WriteSingleRegisterPDU(StartAddr, Value);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU);
        }
Exemple #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);
        }