Implementing ASCII protocol; removing min. unit ID for serial line - see note on README

This commit is contained in:
2024-03-28 18:08:44 +01:00
parent dee0d67453
commit 1bcac96d52
7 changed files with 1949 additions and 192 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -40,19 +40,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForUnitIdOnSerializeReadCoils()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadCoils(0x00, 19, 19);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(0)]
[DataRow(2001)]
@@ -152,19 +139,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForUnitIdOnSerializeReadDiscreteInputs()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadDiscreteInputs(0x00, 19, 19);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(0)]
[DataRow(2001)]
@@ -264,19 +238,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForUnitIdOnSerializeReadHoldingRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadHoldingRegisters(0x00, 19, 19);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(0)]
[DataRow(126)]
@@ -371,19 +332,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeForUnitIdOnSerializeReadInputRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadInputRegisters(0x00, 19, 19);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(0)]
[DataRow(126)]
@@ -484,19 +432,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeExceptionForUnitIdOnSerializeReadDeviceIdentification()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeReadDeviceIdentification(0x00, ModbusDeviceIdentificationCategory.Basic, ModbusDeviceIdentificationObject.ProductCode);
// Assert - ArgumentOutOfRangeException
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowOutOfRangeExceptionForCategoryOnSerializeReadDeviceIdentification()
@@ -592,19 +527,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowArgumentOutOfRangeForUnitIdOnSerializeWriteSingleCoil()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteSingleCoil(0x00, new Coil());
// Assert - ArgumentOutOfRangeException
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteSingleCoil()
@@ -669,19 +591,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowArgumentOutOfRangeForUnitIdOnSerializeWriteSingleHoldingRegister()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteSingleHoldingRegister(0x00, new HoldingRegister());
// Assert - ArgumentOutOfRangeException
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteSingleHoldingRegister()
@@ -759,19 +668,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowArgumentOutOfRangeForUnitIdOnSerializeWriteMultipleCoils()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleCoils(0x00, new List<Coil>());
// Assert - ArgumentOutOfRangeException
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteMultipleCoils()
@@ -903,19 +799,6 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// CRC check will be ignored
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void ShouldThrowArgumentOutOfRangeForUnitIdOnSerializeWriteMultipleHoldingRegisters()
{
// Arrange
var protocol = new RtuProtocol();
// Act
protocol.SerializeWriteMultipleHoldingRegisters(0x00, new List<HoldingRegister>());
// Assert - ArgumentOutOfRangeException
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNullOnSerializeWriteMultipleHoldingRegisters()
@@ -1187,26 +1070,34 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
Assert.IsTrue(complete);
}
[TestMethod]
public void ShouldValidateReadResponse()
[DataTestMethod]
[DataRow(0x01)]
[DataRow(0x02)]
[DataRow(0x03)]
[DataRow(0x04)]
public void ShouldValidateReadResponse(int fn)
{
// Arrange
byte[] request = [UNIT_ID, 0x01, 0x00, 0x01, 0x00, 0x02]; // CRC missing, OK
byte[] response = [UNIT_ID, 0x01, 0x01, 0x00, 0x00, 0x00];
AddCrc(response);
byte[] request = [UNIT_ID, (byte)fn, 0x00, 0x01, 0x00, 0x02]; // CRC missing, OK
byte[] response = [UNIT_ID, (byte)fn, 0x01, 0x00, 0x00, 0x00];
SetCrc(response);
var protocol = new RtuProtocol();
// Act
protocol.ValidateResponse(request, response);
}
[TestMethod]
public void ShouldValidateWriteResponse()
[DataTestMethod]
[DataRow(0x05)]
[DataRow(0x06)]
[DataRow(0x0F)]
[DataRow(0x10)]
public void ShouldValidateWriteResponse(int fn)
{
// Arrange
byte[] request = [UNIT_ID, 0x05, 0x00, 0x01, 0xFF, 0x00]; // CRC missing, OK
byte[] response = [UNIT_ID, 0x05, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00];
AddCrc(response);
byte[] request = [UNIT_ID, (byte)fn, 0x00, 0x01, 0xFF, 0x00]; // CRC missing, OK
byte[] response = [UNIT_ID, (byte)fn, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00];
SetCrc(response);
var protocol = new RtuProtocol();
// Act
@@ -1220,7 +1111,7 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// Arrange
byte[] request = [UNIT_ID, 0x01, 0x00, 0x01, 0x00, 0x02]; // CRC missing, OK
byte[] response = [UNIT_ID + 1, 0x01, 0x01, 0x00, 0x00, 0x00];
AddCrc(response);
SetCrc(response);
var protocol = new RtuProtocol();
// Act
@@ -1249,7 +1140,7 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// Arrange
byte[] request = [UNIT_ID, 0x01, 0x00, 0x01, 0x00, 0x02]; // CRC missing, OK
byte[] response = [UNIT_ID, 0x02, 0x01, 0x00, 0x00, 0x00];
AddCrc(response);
SetCrc(response);
var protocol = new RtuProtocol();
// Act
@@ -1263,7 +1154,7 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// Arrange
byte[] request = [UNIT_ID, 0x01, 0x00, 0x01, 0x00, 0x02]; // CRC missing, OK
byte[] response = [UNIT_ID, 0x81, 0x01, 0x00, 0x00];
AddCrc(response);
SetCrc(response);
var protocol = new RtuProtocol();
// Act
@@ -1281,7 +1172,7 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// Arrange
byte[] request = [UNIT_ID, (byte)fn, 0x00, 0x01, 0x00, 0x02]; // CRC missing, OK
byte[] response = [UNIT_ID, (byte)fn, 0xFF, 0x00, 0x00, 0x00, 0x00];
AddCrc(response);
SetCrc(response);
var protocol = new RtuProtocol();
// Act
@@ -1299,7 +1190,7 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
// Arrange
byte[] request = [UNIT_ID, (byte)fn, 0x00, 0x01, 0x00, 0x02]; // CRC missing, OK
byte[] response = [UNIT_ID, (byte)fn, 0x00, 0x13, 0x00, 0x02, 0x00, 0x00, 0x00];
AddCrc(response);
SetCrc(response);
var protocol = new RtuProtocol();
// Act
@@ -1380,7 +1271,7 @@ namespace AMWD.Protocols.Modbus.Tests.Common.Protocols
Assert.AreEqual("RTU", result);
}
private static void AddCrc(byte[] bytes)
private static void SetCrc(byte[] bytes)
{
byte[] crc = RtuProtocol.CRC16(bytes, 0, bytes.Length - 2);
bytes[^2] = crc[0];