HeaderPacket.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using PlcSiemens.O;
  2. using PlcSiemens.Protocol.Common;
  3. namespace PlcSiemens.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
  14. { get { return 0x0A; } }
  15. public virtual void Build(ByteBuffer buffer)
  16. {
  17. P = buffer.PopByte();
  18. PduType = buffer.PopByte();
  19. AbEx = buffer.PopUshort();
  20. Sequence = buffer.PopUshort();
  21. ParLen = buffer.PopUshort(); //ReqFunNegotiateParams自身数据长度 不包含head的
  22. DataLen = buffer.PopUshort();
  23. }
  24. public void Build(PduType pduType, ushort parLen, ushort dataLen)
  25. {
  26. P = 0x32;
  27. PduType = (byte)pduType;
  28. AbEx = 0x0000;
  29. Sequence = GetNextWord();
  30. ParLen = parLen;
  31. DataLen = dataLen;
  32. }
  33. public void GetByteBuffer(ByteBuffer buffer)
  34. {
  35. buffer.Push(P);
  36. buffer.Push(PduType);
  37. buffer.Push(AbEx);
  38. buffer.Push(Sequence);
  39. buffer.Push(ParLen);
  40. buffer.Push(DataLen);
  41. }
  42. private static ushort _cntword = 1;
  43. protected static ushort GetNextWord()
  44. {
  45. if (_cntword == 0xFFFF)
  46. _cntword = 1;
  47. return _cntword++;
  48. }
  49. }
  50. }