ReadPlcStatus.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Net;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using S7.Net.Protocol;
  5. namespace S7.Net.UnitTest.CommunicationTests;
  6. [TestClass]
  7. public class ReadPlcStatus
  8. {
  9. [TestMethod]
  10. public async Task Read_Status_Run()
  11. {
  12. var cs = new CommunicationSequence {
  13. ConnectionOpenTemplates.ConnectionRequestConfirm,
  14. ConnectionOpenTemplates.CommunicationSetup,
  15. {
  16. """
  17. // TPKT
  18. 03 00 00 21
  19. // COTP
  20. 02 f0 80
  21. // S7 SZL read
  22. 32 07 00 00 PDU1 PDU2 00 08 00 08 00 01 12 04 11 44
  23. 01 00 ff 09 00 04 04 24 00 00
  24. """,
  25. """
  26. // TPKT
  27. 03 00 00 3d
  28. // COTP
  29. 02 f0 80
  30. // S7 SZL response
  31. 32 07 00 00 PDU1 PDU2 00 0c 00 20 00 01 12 08 12 84
  32. 01 02 00 00 00 00 ff 09 00 1c 04 24 00 00 00 14
  33. 00 01 51 44 ff 08 00 00 00 00 00 00 00 00 14 08
  34. 20 12 05 28 34 94
  35. """
  36. }
  37. };
  38. async Task Client(int port)
  39. {
  40. var conn = new Plc(IPAddress.Loopback.ToString(), port, new TsapPair(new Tsap(1, 2), new Tsap(3, 4)));
  41. await conn.OpenAsync();
  42. var status = await conn.ReadStatusAsync();
  43. Assert.AreEqual(0x08, status);
  44. conn.Close();
  45. }
  46. await Task.WhenAll(cs.Serve(out var port), Client(port));
  47. }
  48. }