DateTimeData.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using PlcSiemens.O;
  2. using PlcSiemens.Protocol.Common;
  3. namespace PlcSiemens.Protocol.DateTime
  4. {
  5. public class DateTimeData
  6. {
  7. public byte Ret { get; set; }//0xFF
  8. public byte Size { get; set; }
  9. public ushort Length { get; set; }
  10. public byte Rsvd { get; set; }
  11. public byte HiYear { get; set; }
  12. public byte[] Time { get; set; }//³¤¶È8
  13. public ushort PacketLength
  14. { get { return 0x0E; } }
  15. public DateTimeData()
  16. {
  17. Time = new byte[8];
  18. }
  19. public void Build(ByteBuffer buffer)
  20. {
  21. Ret = buffer.PopByte();
  22. Size = buffer.PopByte();
  23. Length = buffer.PopUshort();
  24. Rsvd = buffer.PopByte();
  25. HiYear = buffer.PopByte();
  26. Time = buffer.PopBytes(8);
  27. }
  28. public void Build(System.DateTime dt)
  29. {
  30. Ret = 0xFF;
  31. Size = (byte)TsType.TsResOctet;
  32. Length = 0x000A;
  33. Rsvd = 0x00;
  34. HiYear = 0x19; // *must* be 19 tough it's not the Hi part of the year...
  35. Time[0] = WordToBcd((ushort)(dt.Year - 2000));
  36. Time[1] = WordToBcd((ushort)(dt.Month));
  37. Time[2] = WordToBcd((ushort)(dt.Day));
  38. Time[3] = WordToBcd((ushort)(dt.Hour));
  39. Time[4] = WordToBcd((ushort)(dt.Minute));
  40. Time[5] = WordToBcd((ushort)(dt.Second));
  41. Time[6] = 0x00;
  42. Time[7] = WordToBcd((ushort)(dt.DayOfWeek));
  43. }
  44. public void GetByteBuffer(ByteBuffer buffer)
  45. {
  46. buffer.Push(Ret);
  47. buffer.Push(Size);
  48. buffer.Push(Length);
  49. buffer.Push(Rsvd);
  50. buffer.Push(HiYear);
  51. buffer.Push(Time);
  52. }
  53. private static byte WordToBcd(ushort value)
  54. {
  55. return (byte)(((value / 10) << 4) | (value % 10));
  56. }
  57. }
  58. }