InvalidModbusRequestException.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. namespace NModbus
  3. {
  4. #if NET46
  5. using System.Runtime.Serialization;
  6. #endif
  7. /// <summary>
  8. /// An exception that provides the exception code that will be sent in response to an invalid Modbus request.
  9. /// </summary>
  10. #if NET46
  11. [Serializable]
  12. #endif
  13. public class InvalidModbusRequestException : Exception
  14. {
  15. private readonly byte _exceptionCode;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="InvalidModbusRequestException" /> class with a specified Modbus exception code.
  18. /// </summary>
  19. /// <param name="exceptionCode">The Modbus exception code to provide to the slave.</param>
  20. public InvalidModbusRequestException(byte exceptionCode)
  21. : this(GetMessage(exceptionCode), exceptionCode)
  22. {
  23. }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="InvalidModbusRequestException" /> class with a specified error message and Modbus exception code.
  26. /// </summary>
  27. /// <param name="message">The error message that explains the reason for the exception.</param>
  28. /// <param name="exceptionCode">The Modbus exception code to provide to the slave.</param>
  29. public InvalidModbusRequestException(string message, byte exceptionCode)
  30. : this(message, exceptionCode, null)
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="InvalidModbusRequestException" /> class with a specified Modbus exception code and a reference to the inner exception that is the cause of this exception.
  35. /// </summary>
  36. /// <param name="exceptionCode">The Modbus exception code to provide to the slave.</param>
  37. /// <param name="innerException">The exception that is the cause of the current exception. If the <paramref name="innerException" /> parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
  38. public InvalidModbusRequestException(byte exceptionCode, Exception innerException)
  39. : this(GetMessage(exceptionCode), exceptionCode, innerException)
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="InvalidModbusRequestException" /> class with a specified Modbus exception code and a reference to the inner exception that is the cause of this exception.
  44. /// </summary>
  45. /// <param name="message">The error message that explains the reason for the exception.</param>
  46. /// <param name="exceptionCode">The Modbus exception code to provide to the slave.</param>
  47. /// <param name="innerException">The exception that is the cause of the current exception. If the <paramref name="innerException" /> parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
  48. public InvalidModbusRequestException(string message, byte exceptionCode, Exception innerException)
  49. : base(message, innerException)
  50. {
  51. _exceptionCode = exceptionCode;
  52. }
  53. #if NET46
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="InvalidModbusRequestException" /> class with serialized data.
  56. /// </summary>
  57. /// <param name="info">The object that holds the serialized object data.</param>
  58. /// <param name="context">The contextual information about the source or destination.</param>
  59. protected InvalidModbusRequestException(SerializationInfo info, StreamingContext context)
  60. : base(info, context)
  61. {
  62. _exceptionCode = info.GetByte(nameof(ExceptionCode));
  63. }
  64. #endif
  65. /// <summary>
  66. /// Gets the Modbus exception code to provide to the slave.
  67. /// </summary>
  68. public byte ExceptionCode => _exceptionCode;
  69. #if NET46
  70. /// <summary>Sets the <see cref="SerializationInfo" /> object with the Modbus exception code and additional exception information.</summary>
  71. /// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
  72. /// <param name="context">The <see cref="StreamingContext" /> that contains contextual information about the source or destination.</param>
  73. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  74. {
  75. base.GetObjectData(info, context);
  76. info.AddValue("ExceptionCode", this._exceptionCode, typeof(byte));
  77. }
  78. #endif
  79. private static string GetMessage(byte exceptionCode)
  80. {
  81. return $"Modbus exception code {exceptionCode}.";
  82. }
  83. }
  84. }