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