SlaveException.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using NModbus.Message;
  4. namespace NModbus
  5. {
  6. #if NET46
  7. using System.Runtime.Serialization;
  8. using System.Security.Permissions;
  9. #endif
  10. /// <summary>
  11. /// Represents slave errors that occur during communication.
  12. /// </summary>
  13. #if NET46
  14. [Serializable]
  15. #endif
  16. public class SlaveException : Exception
  17. {
  18. private const string SlaveAddressPropertyName = "SlaveAdress";
  19. private const string FunctionCodePropertyName = "FunctionCode";
  20. private const string SlaveExceptionCodePropertyName = "SlaveExceptionCode";
  21. [AllowNull]
  22. private readonly SlaveExceptionResponse _slaveExceptionResponse;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="SlaveException" /> class.
  25. /// </summary>
  26. public SlaveException()
  27. {
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="SlaveException" /> class.
  31. /// </summary>
  32. /// <param name="message">The message.</param>
  33. public SlaveException(string message)
  34. : base(message)
  35. {
  36. }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="SlaveException" /> class.
  39. /// </summary>
  40. /// <param name="message">The message.</param>
  41. /// <param name="innerException">The inner exception.</param>
  42. public SlaveException(string message, Exception innerException)
  43. : base(message, innerException)
  44. {
  45. }
  46. internal SlaveException(SlaveExceptionResponse slaveExceptionResponse)
  47. {
  48. _slaveExceptionResponse = slaveExceptionResponse;
  49. }
  50. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Used by test code.")]
  51. internal SlaveException(string message, SlaveExceptionResponse slaveExceptionResponse)
  52. : base(message)
  53. {
  54. _slaveExceptionResponse = slaveExceptionResponse;
  55. }
  56. #if NET46
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="SlaveException" /> class.
  59. /// </summary>
  60. /// <param name="info">
  61. /// The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized
  62. /// object data about the exception being thrown.
  63. /// </param>
  64. /// <param name="context">
  65. /// The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual
  66. /// information about the source or destination.
  67. /// </param>
  68. /// <exception cref="T:System.Runtime.Serialization.SerializationException">
  69. /// The class name is null or
  70. /// <see cref="P:System.Exception.HResult"></see> is zero (0).
  71. /// </exception>
  72. /// <exception cref="T:System.ArgumentNullException">The info parameter is null. </exception>
  73. protected SlaveException(SerializationInfo info, StreamingContext context)
  74. : base(info, context)
  75. {
  76. if (info != null)
  77. {
  78. _slaveExceptionResponse = new SlaveExceptionResponse(
  79. info.GetByte(SlaveAddressPropertyName),
  80. info.GetByte(FunctionCodePropertyName),
  81. info.GetByte(SlaveExceptionCodePropertyName));
  82. }
  83. }
  84. #endif
  85. /// <summary>
  86. /// Gets a message that describes the current exception.
  87. /// </summary>
  88. /// <value>
  89. /// The error message that explains the reason for the exception, or an empty string.
  90. /// </value>
  91. public override string Message
  92. {
  93. get
  94. {
  95. string responseString;
  96. responseString = _slaveExceptionResponse != null ? string.Concat(Environment.NewLine, _slaveExceptionResponse) : string.Empty;
  97. return string.Concat(base.Message, responseString);
  98. }
  99. }
  100. /// <summary>
  101. /// Gets the response function code that caused the exception to occur, or 0.
  102. /// </summary>
  103. /// <value>The function code.</value>
  104. public byte FunctionCode => _slaveExceptionResponse != null ? _slaveExceptionResponse.FunctionCode : (byte)0;
  105. /// <summary>
  106. /// Gets the slave exception code, or 0.
  107. /// </summary>
  108. /// <value>The slave exception code.</value>
  109. public byte SlaveExceptionCode => _slaveExceptionResponse != null ? _slaveExceptionResponse.SlaveExceptionCode : (byte)0;
  110. /// <summary>
  111. /// Gets the slave address, or 0.
  112. /// </summary>
  113. /// <value>The slave address.</value>
  114. public byte SlaveAddress => _slaveExceptionResponse != null ? _slaveExceptionResponse.SlaveAddress : (byte)0;
  115. #if NET46
  116. /// <summary>
  117. /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"></see>
  118. /// with information about the exception.
  119. /// </summary>
  120. /// <param name="info">
  121. /// The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized
  122. /// object data about the exception being thrown.
  123. /// </param>
  124. /// <param name="context">
  125. /// The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual
  126. /// information about the source or destination.
  127. /// </param>
  128. /// <exception cref="T:System.ArgumentNullException">The info parameter is a null reference (Nothing in Visual Basic). </exception>
  129. /// <PermissionSet>
  130. /// <IPermission
  131. /// class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
  132. /// version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*" />
  133. /// <IPermission
  134. /// class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
  135. /// version="1" Flags="SerializationFormatter" />
  136. /// </PermissionSet>
  137. [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  138. [SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Argument info is validated, rule does not understand AND condition.")]
  139. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  140. {
  141. base.GetObjectData(info, context);
  142. if (info != null && _slaveExceptionResponse != null)
  143. {
  144. info.AddValue(SlaveAddressPropertyName, _slaveExceptionResponse.SlaveAddress);
  145. info.AddValue(FunctionCodePropertyName, _slaveExceptionResponse.FunctionCode);
  146. info.AddValue(SlaveExceptionCodePropertyName, _slaveExceptionResponse.SlaveExceptionCode);
  147. }
  148. }
  149. #endif
  150. }
  151. }