using System;
namespace NModbus
{
#if NET46
using System.Runtime.Serialization;
#endif
///
/// An exception that provides the exception code that will be sent in response to an invalid Modbus request.
///
#if NET46
[Serializable]
#endif
public class InvalidModbusRequestException : Exception
{
private readonly byte _exceptionCode;
///
/// Initializes a new instance of the class with a specified Modbus exception code.
///
/// The Modbus exception code to provide to the slave.
public InvalidModbusRequestException(byte exceptionCode)
: this(GetMessage(exceptionCode), exceptionCode)
{
}
///
/// Initializes a new instance of the class with a specified error message and Modbus exception code.
///
/// The error message that explains the reason for the exception.
/// The Modbus exception code to provide to the slave.
public InvalidModbusRequestException(string message, byte exceptionCode)
: this(message, exceptionCode, null)
{
}
///
/// Initializes a new instance of the class with a specified Modbus exception code and a reference to the inner exception that is the cause of this exception.
///
/// The Modbus exception code to provide to the slave.
/// The exception that is the cause of the current exception. If the parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.
public InvalidModbusRequestException(byte exceptionCode, Exception innerException)
: this(GetMessage(exceptionCode), exceptionCode, innerException)
{
}
///
/// Initializes a new instance of the class with a specified Modbus exception code and a reference to the inner exception that is the cause of this exception.
///
/// The error message that explains the reason for the exception.
/// The Modbus exception code to provide to the slave.
/// The exception that is the cause of the current exception. If the parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.
public InvalidModbusRequestException(string message, byte exceptionCode, Exception innerException)
: base(message, innerException)
{
_exceptionCode = exceptionCode;
}
#if NET46
///
/// Initializes a new instance of the class with serialized data.
///
/// The object that holds the serialized object data.
/// The contextual information about the source or destination.
protected InvalidModbusRequestException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
_exceptionCode = info.GetByte(nameof(ExceptionCode));
}
#endif
///
/// Gets the Modbus exception code to provide to the slave.
///
public byte ExceptionCode => _exceptionCode;
#if NET46
/// Sets the object with the Modbus exception code and additional exception information.
/// The that holds the serialized object data about the exception being thrown.
/// The that contains contextual information about the source or destination.
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("ExceptionCode", this._exceptionCode, typeof(byte));
}
#endif
private static string GetMessage(byte exceptionCode)
{
return $"Modbus exception code {exceptionCode}.";
}
}
}