123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using System;
- using System.Net;
- using System.Threading.Tasks;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using S7.Net.Protocol;
- namespace S7.Net.UnitTest.CommunicationTests;
- [TestClass]
- public class Clock
- {
- [TestMethod, Timeout(1000)]
- public async Task Read_Clock_Value()
- {
- var cs = new CommunicationSequence
- {
- ConnectionOpenTemplates.ConnectionRequestConfirm,
- ConnectionOpenTemplates.CommunicationSetup,
- {
- """
- // TPKT
- 03 00 00 1d
- // COTP
- 02 f0 80
- // S7 read clock
- // UserData header
- 32 07 00 00 PDU1 PDU2
- // Parameter length
- 00 08
- // Data length
- 00 04
- // Parameter
- // Head
- 00 01 12
- // Length
- 04
- // Method (Request/Response): Req
- 11
- // Type request (4...) Function group timers (...7)
- 47
- // Subfunction: read clock
- 01
- // Sequence number
- 00
- // Data
- // Return code
- 0a
- // Transport size
- 00
- // Payload length
- 00 00
- """,
- """
- // TPKT
- 03 00 00 2b
- // COTP
- 02 f0 80
- // S7 read clock response
- // UserData header
- 32 07 00 00 PDU1 PDU2
- // Parameter length
- 00 0c
- // Data length
- 00 0e
- // Parameter
- // Head
- 00 01 12
- // Length
- 08
- // Method (Request/Response): Res
- 12
- // Type response (8...) Function group timers (...7)
- 87
- // Subfunction: read clock
- 01
- // Sequence number
- 01
- // Data unit reference
- 00
- // Last data unit? Yes
- 00
- // Error code
- 00 00
- // Data
- // Error code
- ff
- // Transport size: OCTET STRING
- 09
- // Length
- 00 0a
- // Timestamp
- // Reserved
- 00
- // Year 1
- 19
- // Year 2
- 14
- // Month
- 08
- // Day
- 20
- // Hour
- 11
- // Minute
- 59
- // Seconds
- 43
- // Milliseconds: 912..., Day of week: ...4
- 91 24
- """
- }
- };
- static async Task Client(int port)
- {
- var conn = new Plc(IPAddress.Loopback.ToString(), port, new TsapPair(new Tsap(1, 2), new Tsap(3, 4)));
- await conn.OpenAsync();
- var time = await conn.ReadClockAsync();
- Assert.AreEqual(new DateTime(2014, 8, 20, 11, 59, 43, 912), time);
- conn.Close();
- }
- await Task.WhenAll(cs.Serve(out var port), Client(port));
- }
- [TestMethod, Timeout(1000)]
- public async Task Write_Clock_Value()
- {
- var cs = new CommunicationSequence
- {
- ConnectionOpenTemplates.ConnectionRequestConfirm,
- ConnectionOpenTemplates.CommunicationSetup,
- {
- """
- // TPKT
- 03 00 00 27
- // COTP
- 02 f0 80
- // S7 read clock
- // UserData header
- 32 07 00 00 PDU1 PDU2
- // Parameter length
- 00 08
- // Data length
- 00 0e
- // Parameter
- // Head
- 00 01 12
- // Length
- 04
- // Method (Request/Response): Req
- 11
- // Type request (4...) Function group timers (...7)
- 47
- // Subfunction: write clock
- 02
- // Sequence number
- 00
- // Data
- // Return code
- ff
- // Transport size
- 09
- // Payload length
- 00 0a
- // Payload
- // Timestamp
- // Reserved
- 00
- // Year 1
- 19
- // Year 2
- 14
- // Month
- 08
- // Day
- 20
- // Hour
- 11
- // Minute
- 59
- // Seconds
- 43
- // Milliseconds: 912..., Day of week: ...4
- 91 24
- """,
- """
- // TPKT
- 03 00 00 21
- // COTP
- 02 f0 80
- // S7 read clock response
- // UserData header
- 32 07 00 00 PDU1 PDU2
- // Parameter length
- 00 0c
- // Data length
- 00 04
- // Parameter
- // Head
- 00 01 12
- // Length
- 08
- // Method (Request/Response): Res
- 12
- // Type response (8...) Function group timers (...7)
- 87
- // Subfunction: write clock
- 02
- // Sequence number
- 01
- // Data unit reference
- 00
- // Last data unit? Yes
- 00
- // Error code
- 00 00
- // Data
- // Error code
- 0a
- // Transport size: NONE
- 00
- // Length
- 00 00
- """
- }
- };
- static async Task Client(int port)
- {
- var conn = new Plc(IPAddress.Loopback.ToString(), port, new TsapPair(new Tsap(1, 2), new Tsap(3, 4)));
- await conn.OpenAsync();
- await conn.WriteClockAsync(new DateTime(2014, 08, 20, 11, 59, 43, 912));
- conn.Close();
- }
- await Task.WhenAll(cs.Serve(out var port), Client(port));
- }
- }
|