Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ProtocolParser.cs 1.3KB

pirms 9 mēnešiem
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace NearCardAttendance.Service.TcpServer.Protocol
  7. {
  8. public class ProtocolParser
  9. {
  10. public int MessageLength { get; private set; } = default!;
  11. public string FuncNo { get; private set; } = default!;
  12. public string SeqNo { get; private set; } = default!;
  13. public string Data { get; private set; } = default!;
  14. //private string _protocolString { get; set; } = default!;
  15. public ProtocolParser(string protocolString)
  16. {
  17. ParseProtocolString(protocolString);
  18. //_protocolString = protocolString;
  19. }
  20. private void ParseProtocolString(string protocolString)
  21. {
  22. try
  23. {
  24. _ = int.TryParse(protocolString.AsSpan(0, 4), out int messageLength);
  25. MessageLength = messageLength;
  26. FuncNo = protocolString.Substring(4,2);
  27. SeqNo = protocolString.Substring(6,4);
  28. Data = protocolString.Substring(10).TrimEnd();
  29. }
  30. catch (Exception e)
  31. {
  32. Console.WriteLine($"Error: {e.Message}");
  33. // throw new ArgumentException("Invalid protocol string format.");
  34. }
  35. }
  36. }
  37. }