[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] // Handling Request on Server public void WriteMultipleCoilsPDU_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 Value = new bool[Quantity]; for (int i = 0; i < Quantity; i++) { Value[i] = (RandomGen.Next(2) != 0); Server.AddCoil(Address + i); } #endregion var ClientPDU = new WriteMultipleCoilsPDU(Address, Value); var Request = ClientPDU.BuildRequest(); var ServerPDU = new WriteMultipleCoilsPDU(); var ReqStatus = ServerPDU.CheckRequest(Request); Assert.AreEqual(Status.OK, ReqStatus, "Recognition Error"); Assert.AreEqual(Address, ServerPDU.StartingAddress, "Whong Address recognition in CheckRequest"); ServerPDU.Handle(Server); CollectionAssert.AreEqual(Value, ServerPDU.Value, "Whong Address recognition in CheckRequest"); ClientPDU.CheckResponse(ServerPDU.BuildResponse()); CollectionAssert.AreEqual(Value, ClientPDU.Value, "Whong Address recognition in CheckRequest"); }
[TestMethod] // Handling Request on Server public void WriteSingleCoilPDU_LifeCycle() { var RandomGen = new Random(); int Address = RandomGen.Next(MinAddress, MaxAddress); var Value = (RandomGen.Next(2) != 0); var Server = new BaseModbusServer(); Server.AddCoil(Address); var ClientPDU = new WriteSingleCoilPDU(Address, Value); var Request = ClientPDU.BuildRequest(); var ServerPDU = new WriteSingleCoilPDU(); var ReqStatus = ServerPDU.CheckRequest(Request); Assert.AreEqual(Status.OK, ReqStatus, "Recognition Error"); 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] // Handling Request on Server public void ReadInputRegistersPDU_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 int[Quantity]; for (int i = 0; i < Quantity; i++) { Values[i] = RandomGen.Next(0xFFFF); Server.AddInputRegister(Address + i, Values[i]); } #endregion var ClientPDU = new ReadInputRegistersPDU(Address, Quantity); var ServerPDU = new ReadInputRegistersPDU(); ServerPDU.CheckRequest(ClientPDU.BuildRequest()); 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"); }