TsapPair.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. namespace S7.Net.Protocol
  3. {
  4. /// <summary>
  5. /// Implements a pair of TSAP addresses used to connect to a PLC.
  6. /// </summary>
  7. public class TsapPair
  8. {
  9. /// <summary>
  10. /// The local <see cref="Tsap" />.
  11. /// </summary>
  12. public Tsap Local { get; set; }
  13. /// <summary>
  14. /// The remote <see cref="Tsap" />
  15. /// </summary>
  16. public Tsap Remote { get; set; }
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="TsapPair" /> class using the specified local and
  19. /// remote TSAP.
  20. /// </summary>
  21. /// <param name="local">The local TSAP.</param>
  22. /// <param name="remote">The remote TSAP.</param>
  23. public TsapPair(Tsap local, Tsap remote)
  24. {
  25. Local = local;
  26. Remote = remote;
  27. }
  28. /// <summary>
  29. /// Builds a <see cref="TsapPair" /> that can be used to connect to a PLC using the default connection
  30. /// addresses.
  31. /// </summary>
  32. /// <remarks>
  33. /// The remote TSAP is constructed using <code>new Tsap(0x03, (byte) ((rack &lt;&lt; 5) | slot))</code>.
  34. /// </remarks>
  35. /// <param name="cpuType">The CPU type of the PLC.</param>
  36. /// <param name="rack">The rack of the PLC's network card.</param>
  37. /// <param name="slot">The slot of the PLC's network card.</param>
  38. /// <returns>A TSAP pair that matches the given parameters.</returns>
  39. /// <exception cref="ArgumentOutOfRangeException">The <paramref name="cpuType"/> is invalid.
  40. ///
  41. /// -or-
  42. ///
  43. /// The <paramref name="rack"/> parameter is less than 0.
  44. ///
  45. /// -or-
  46. ///
  47. /// The <paramref name="rack"/> parameter is greater than 15.
  48. ///
  49. /// -or-
  50. ///
  51. /// The <paramref name="slot"/> parameter is less than 0.
  52. ///
  53. /// -or-
  54. ///
  55. /// The <paramref name="slot"/> parameter is greater than 15.</exception>
  56. public static TsapPair GetDefaultTsapPair(CpuType cpuType, int rack, int slot)
  57. {
  58. if (rack < 0) throw InvalidRackOrSlot(rack, nameof(rack), "minimum", 0);
  59. if (rack > 0x0F) throw InvalidRackOrSlot(rack, nameof(rack), "maximum", 0x0F);
  60. if (slot < 0) throw InvalidRackOrSlot(slot, nameof(slot), "minimum", 0);
  61. if (slot > 0x0F) throw InvalidRackOrSlot(slot, nameof(slot), "maximum", 0x0F);
  62. switch (cpuType)
  63. {
  64. case CpuType.S7200:
  65. return new TsapPair(new Tsap(0x10, 0x00), new Tsap(0x10, 0x01));
  66. case CpuType.Logo0BA8:
  67. // The actual values are probably on a per-project basis
  68. return new TsapPair(new Tsap(0x01, 0x00), new Tsap(0x01, 0x02));
  69. case CpuType.S7200Smart:
  70. case CpuType.S71200:
  71. case CpuType.S71500:
  72. case CpuType.S7300:
  73. case CpuType.S7400:
  74. // Testing with S7 1500 shows only the remote TSAP needs to match. This might differ for other
  75. // PLC types.
  76. return new TsapPair(new Tsap(0x01, 0x00), new Tsap(0x03, (byte) ((rack << 5) | slot)));
  77. default:
  78. throw new ArgumentOutOfRangeException(nameof(cpuType), "Invalid CPU Type specified");
  79. }
  80. }
  81. private static ArgumentOutOfRangeException InvalidRackOrSlot(int value, string name, string extrema,
  82. int extremaValue)
  83. {
  84. return new ArgumentOutOfRangeException(name,
  85. $"Invalid {name} value specified (decimal: {value}, hexadecimal: {value:X}), {extrema} value " +
  86. $"is {extremaValue} (decimal) or {extremaValue:X} (hexadecimal).");
  87. }
  88. }
  89. }