DateTimeData.cs 1.9 KB

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