HeaderPacket.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Core.Communication.Transport;
  2. using PLC.Siemens.Protocol.Common;
  3. namespace PLC.Siemens.Protocol.Header
  4. {
  5. public class HeaderPacket
  6. {
  7. public byte P { get; set; } // Telegram ID, always 32
  8. public byte PduType { get; set; } // Header type 1 or 7
  9. public ushort AbEx { get; set; } // AB currently unknown, maybe it can be used for long numbers.
  10. public ushort Sequence { get; set; }// Message ID. This can be used to make sure a received answer
  11. public ushort ParLen { get; set; } // Length of parameters which follow this header
  12. public ushort DataLen { get; set; } // Length of data which follow the parameters
  13. public ushort PacketLength { get{return 0x0A;} }
  14. public virtual void Build(ByteBuffer buffer)
  15. {
  16. P = buffer.PopByte();
  17. PduType = buffer.PopByte();
  18. AbEx = buffer.PopUshort();
  19. Sequence = buffer.PopUshort();
  20. ParLen = buffer.PopUshort(); //ReqFunNegotiateParams自身数据长度 不包含head的
  21. DataLen = buffer.PopUshort();
  22. }
  23. public void Build(PduType pduType, ushort parLen, ushort dataLen)
  24. {
  25. P = 0x32;
  26. PduType = (byte)pduType;
  27. AbEx = 0x0000;
  28. Sequence = GetNextWord();
  29. ParLen = parLen;
  30. DataLen = dataLen;
  31. }
  32. public void GetByteBuffer(ByteBuffer buffer)
  33. {
  34. buffer.Push(P);
  35. buffer.Push(PduType);
  36. buffer.Push(AbEx);
  37. buffer.Push(Sequence);
  38. buffer.Push(ParLen);
  39. buffer.Push(DataLen);
  40. }
  41. private static ushort _cntword = 1;
  42. protected static ushort GetNextWord()
  43. {
  44. if (_cntword == 0xFFFF)
  45. _cntword = 1;
  46. return _cntword++;
  47. }
  48. }
  49. }