BxCmdSystemClockCorrect.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import struct
  2. import time
  3. from .BxCmd import baseBxCmd
  4. from .BxCmdCode import CMD_SYSTEM_CLOCK_CORRECT
  5. from .BxUtils import BIN2Uint64
  6. class CmdSystemClockCorrect(baseBxCmd):
  7. def __init__(self, t):
  8. super().__init__(CMD_SYSTEM_CLOCK_CORRECT.group, CMD_SYSTEM_CLOCK_CORRECT.code)
  9. self.sysTime = t
  10. # 转换为BCD码
  11. year = BIN2Uint64(Uint2BCD(t.tm_year, False), '<')
  12. month = BIN2Uint64(Uint2BCD(t.tm_mon, False), '<')
  13. day = BIN2Uint64(Uint2BCD(t.tm_mday, False), '<')
  14. hour = BIN2Uint64(Uint2BCD(t.tm_hour, False), '<')
  15. minute = BIN2Uint64(Uint2BCD(t.tm_min, False), '<')
  16. second = BIN2Uint64(Uint2BCD(t.tm_sec, False), '<')
  17. week = BIN2Uint64(Uint2BCD(t.tm_wday + 1, False), '<') # tm_wday是0-6,对应周一到周日,这里转换为1-7
  18. if week == 0:
  19. week = 7
  20. self.year = int(year)
  21. self.month = int(month)
  22. self.day = int(day)
  23. self.hour = int(hour)
  24. self.minute = int(minute)
  25. self.second = int(second)
  26. self.week = int(week)
  27. def Build(self):
  28. result = bytearray()
  29. # 写入命令组
  30. result.append(self.Group())
  31. # 写入命令
  32. result.append(self.Cmd())
  33. # 写入响应标志
  34. result.append(self.ReqResp())
  35. # 写入保留值
  36. result.append(0x00)
  37. result.append(0x00)
  38. # 写入年份(BCD码)
  39. y = bytearray([
  40. self.year & 0xff,
  41. (self.year >> 8) & 0xff,
  42. (self.year >> 16) & 0xff,
  43. (self.year >> 24) & 0xff,
  44. ])
  45. if y[0] == 0x00 and y[1] == 0x00:
  46. result.append(y[2])
  47. result.append(y[3])
  48. else:
  49. result.append(y[0])
  50. result.append(y[1])
  51. # 写入月份(BCD码)
  52. m = bytearray([
  53. self.month & 0xff,
  54. (self.month >> 8) & 0xff,
  55. (self.month >> 16) & 0xff,
  56. (self.month >> 24) & 0xff,
  57. ])
  58. result.append(m[0])
  59. # 写入日期(BCD码)
  60. d = bytearray([
  61. self.day & 0xff,
  62. (self.day >> 8) & 0xff,
  63. (self.day >> 16) & 0xff,
  64. (self.day >> 24) & 0xff,
  65. ])
  66. result.append(d[0])
  67. # 写入小时(BCD码)
  68. h = bytearray([
  69. self.hour & 0xff,
  70. (self.hour >> 8) & 0xff,
  71. (self.hour >> 16) & 0xff,
  72. (self.hour >> 24) & 0xff,
  73. ])
  74. result.append(h[0])
  75. # 写入分钟(BCD码)
  76. min_ = bytearray([
  77. self.minute & 0xff,
  78. (self.minute >> 8) & 0xff,
  79. (self.minute >> 16) & 0xff,
  80. (self.minute >> 24) & 0xff,
  81. ])
  82. result.append(min_[0])
  83. # 写入秒(BCD码)
  84. s = bytearray([
  85. self.second & 0xff,
  86. (self.second >> 8) & 0xff,
  87. (self.second >> 16) & 0xff,
  88. (self.second >> 24) & 0xff,
  89. ])
  90. result.append(s[0])
  91. # 写入星期(BCD码)
  92. week_ = bytearray([
  93. self.week & 0xff,
  94. (self.week >> 8) & 0xff,
  95. (self.week >> 16) & 0xff,
  96. (self.week >> 24) & 0xff,
  97. ])
  98. result.append(week_[0])
  99. return bytes(result)
  100. # uint转BCD
  101. def Uint2BCD(n, isBigEndian):
  102. b = bytearray()
  103. while True:
  104. h = (n // 10) % 10
  105. l = n % 10
  106. b.append((h << 4) | l)
  107. n = n // 100
  108. if n == 0:
  109. break
  110. if not isBigEndian:
  111. return b
  112. # 反转字节顺序
  113. l = len(b)
  114. r = bytearray(l)
  115. for i, v in enumerate(b):
  116. r[l-1-i] = v
  117. return r
  118. def NewBxCmdSystemClockCorrect(t):
  119. return CmdSystemClockCorrect(t)