You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 line
1.3KB

  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. }