Clock.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Net;
  3. using System.Threading.Tasks;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using S7.Net.Protocol;
  6. namespace S7.Net.UnitTest.CommunicationTests;
  7. [TestClass]
  8. public class Clock
  9. {
  10. [TestMethod, Timeout(1000)]
  11. public async Task Read_Clock_Value()
  12. {
  13. var cs = new CommunicationSequence
  14. {
  15. ConnectionOpenTemplates.ConnectionRequestConfirm,
  16. ConnectionOpenTemplates.CommunicationSetup,
  17. {
  18. """
  19. // TPKT
  20. 03 00 00 1d
  21. // COTP
  22. 02 f0 80
  23. // S7 read clock
  24. // UserData header
  25. 32 07 00 00 PDU1 PDU2
  26. // Parameter length
  27. 00 08
  28. // Data length
  29. 00 04
  30. // Parameter
  31. // Head
  32. 00 01 12
  33. // Length
  34. 04
  35. // Method (Request/Response): Req
  36. 11
  37. // Type request (4...) Function group timers (...7)
  38. 47
  39. // Subfunction: read clock
  40. 01
  41. // Sequence number
  42. 00
  43. // Data
  44. // Return code
  45. 0a
  46. // Transport size
  47. 00
  48. // Payload length
  49. 00 00
  50. """,
  51. """
  52. // TPKT
  53. 03 00 00 2b
  54. // COTP
  55. 02 f0 80
  56. // S7 read clock response
  57. // UserData header
  58. 32 07 00 00 PDU1 PDU2
  59. // Parameter length
  60. 00 0c
  61. // Data length
  62. 00 0e
  63. // Parameter
  64. // Head
  65. 00 01 12
  66. // Length
  67. 08
  68. // Method (Request/Response): Res
  69. 12
  70. // Type response (8...) Function group timers (...7)
  71. 87
  72. // Subfunction: read clock
  73. 01
  74. // Sequence number
  75. 01
  76. // Data unit reference
  77. 00
  78. // Last data unit? Yes
  79. 00
  80. // Error code
  81. 00 00
  82. // Data
  83. // Error code
  84. ff
  85. // Transport size: OCTET STRING
  86. 09
  87. // Length
  88. 00 0a
  89. // Timestamp
  90. // Reserved
  91. 00
  92. // Year 1
  93. 19
  94. // Year 2
  95. 14
  96. // Month
  97. 08
  98. // Day
  99. 20
  100. // Hour
  101. 11
  102. // Minute
  103. 59
  104. // Seconds
  105. 43
  106. // Milliseconds: 912..., Day of week: ...4
  107. 91 24
  108. """
  109. }
  110. };
  111. static async Task Client(int port)
  112. {
  113. var conn = new Plc(IPAddress.Loopback.ToString(), port, new TsapPair(new Tsap(1, 2), new Tsap(3, 4)));
  114. await conn.OpenAsync();
  115. var time = await conn.ReadClockAsync();
  116. Assert.AreEqual(new DateTime(2014, 8, 20, 11, 59, 43, 912), time);
  117. conn.Close();
  118. }
  119. await Task.WhenAll(cs.Serve(out var port), Client(port));
  120. }
  121. [TestMethod, Timeout(1000)]
  122. public async Task Write_Clock_Value()
  123. {
  124. var cs = new CommunicationSequence
  125. {
  126. ConnectionOpenTemplates.ConnectionRequestConfirm,
  127. ConnectionOpenTemplates.CommunicationSetup,
  128. {
  129. """
  130. // TPKT
  131. 03 00 00 27
  132. // COTP
  133. 02 f0 80
  134. // S7 read clock
  135. // UserData header
  136. 32 07 00 00 PDU1 PDU2
  137. // Parameter length
  138. 00 08
  139. // Data length
  140. 00 0e
  141. // Parameter
  142. // Head
  143. 00 01 12
  144. // Length
  145. 04
  146. // Method (Request/Response): Req
  147. 11
  148. // Type request (4...) Function group timers (...7)
  149. 47
  150. // Subfunction: write clock
  151. 02
  152. // Sequence number
  153. 00
  154. // Data
  155. // Return code
  156. ff
  157. // Transport size
  158. 09
  159. // Payload length
  160. 00 0a
  161. // Payload
  162. // Timestamp
  163. // Reserved
  164. 00
  165. // Year 1
  166. 19
  167. // Year 2
  168. 14
  169. // Month
  170. 08
  171. // Day
  172. 20
  173. // Hour
  174. 11
  175. // Minute
  176. 59
  177. // Seconds
  178. 43
  179. // Milliseconds: 912..., Day of week: ...4
  180. 91 24
  181. """,
  182. """
  183. // TPKT
  184. 03 00 00 21
  185. // COTP
  186. 02 f0 80
  187. // S7 read clock response
  188. // UserData header
  189. 32 07 00 00 PDU1 PDU2
  190. // Parameter length
  191. 00 0c
  192. // Data length
  193. 00 04
  194. // Parameter
  195. // Head
  196. 00 01 12
  197. // Length
  198. 08
  199. // Method (Request/Response): Res
  200. 12
  201. // Type response (8...) Function group timers (...7)
  202. 87
  203. // Subfunction: write clock
  204. 02
  205. // Sequence number
  206. 01
  207. // Data unit reference
  208. 00
  209. // Last data unit? Yes
  210. 00
  211. // Error code
  212. 00 00
  213. // Data
  214. // Error code
  215. 0a
  216. // Transport size: NONE
  217. 00
  218. // Length
  219. 00 00
  220. """
  221. }
  222. };
  223. static async Task Client(int port)
  224. {
  225. var conn = new Plc(IPAddress.Loopback.ToString(), port, new TsapPair(new Tsap(1, 2), new Tsap(3, 4)));
  226. await conn.OpenAsync();
  227. await conn.WriteClockAsync(new DateTime(2014, 08, 20, 11, 59, 43, 912));
  228. conn.Close();
  229. }
  230. await Task.WhenAll(cs.Serve(out var port), Client(port));
  231. }
  232. }