BxCmdSystemClockCorrect.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package bx
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "time"
  8. )
  9. type CmdSystemClockCorrect struct {
  10. baseBxCmd
  11. sysTime time.Time
  12. year, month, day, hour, minute, second, week int
  13. }
  14. func NewBxCmdSystemClockCorrect(t time.Time) CmdSystemClockCorrect {
  15. fmt.Println(t.Year())
  16. fmt.Println(t.Month())
  17. fmt.Println(t.Day())
  18. fmt.Println(t.Hour())
  19. fmt.Println(t.Minute())
  20. fmt.Println(t.Second())
  21. fmt.Println(t.Weekday())
  22. year, _ := BIN2Uint64(Uint2BCD(uint64(t.Year()), false), binary.LittleEndian)
  23. month, _ := BIN2Uint64(Uint2BCD(uint64(t.Month()), false), binary.LittleEndian)
  24. day, _ := BIN2Uint64(Uint2BCD(uint64(t.Day()), false), binary.LittleEndian)
  25. hour, _ := BIN2Uint64(Uint2BCD(uint64(t.Hour()), false), binary.LittleEndian)
  26. minute, _ := BIN2Uint64(Uint2BCD(uint64(t.Minute()), false), binary.LittleEndian)
  27. second, _ := BIN2Uint64(Uint2BCD(uint64(t.Second()), false), binary.LittleEndian)
  28. week, _ := BIN2Uint64(Uint2BCD(uint64(t.Weekday()), false), binary.LittleEndian)
  29. if week == 0 {
  30. week = 7
  31. }
  32. return CmdSystemClockCorrect{
  33. baseBxCmd: newBaseCmd(CMD_SYSTEM_CLOCK_CORRECT.group, CMD_SYSTEM_CLOCK_CORRECT.code),
  34. sysTime: t,
  35. year: int(year),
  36. month: int(month),
  37. day: int(day),
  38. hour: int(hour),
  39. minute: int(minute),
  40. second: int(second),
  41. week: int(week),
  42. }
  43. }
  44. // uint转BCD
  45. func Uint2BCD(n uint64, isBigEndian bool) []byte {
  46. var b []byte
  47. //if n < 256 {
  48. // b = []byte{0}
  49. //}
  50. for i := 0; ; i++ {
  51. h := (n / 10) % 10
  52. l := n % 10
  53. b = append(b, byte(h<<4|l))
  54. n = n / 100
  55. if n == 0 {
  56. break
  57. }
  58. }
  59. if !isBigEndian {
  60. return b
  61. }
  62. l := len(b)
  63. var r = make([]byte, l)
  64. for i, v := range b {
  65. r[l-1-i] = v
  66. }
  67. return r
  68. }
  69. func BIN2Uint64(bin []byte, order binary.ByteOrder) (uint64, error) {
  70. len := len(bin)
  71. switch len {
  72. case 1:
  73. return uint64(bin[0]), nil
  74. case 2:
  75. return uint64(order.Uint16(bin)), nil
  76. case 3, 4:
  77. bin4 := make([]byte, 8)
  78. copy(bin4[4-len:], bin) //前面字节填充0
  79. return uint64(order.Uint32(bin4)), nil
  80. case 5, 6, 7, 8:
  81. bin8 := make([]byte, 8)
  82. copy(bin8[8-len:], bin)
  83. return order.Uint64(bin8), nil
  84. default:
  85. return 0, errors.New("不符合字节长度范围1-8")
  86. }
  87. }
  88. func (this CmdSystemClockCorrect) Build() []byte {
  89. w := bytes.NewBuffer(make([]byte, 0, 1024))
  90. binary.Write(w, binary.LittleEndian, this.Group())
  91. binary.Write(w, binary.LittleEndian, this.Cmd())
  92. binary.Write(w, binary.LittleEndian, this.ReqResp())
  93. binary.Write(w, binary.LittleEndian, byte(0x00))
  94. binary.Write(w, binary.LittleEndian, byte(0x00))
  95. //BCD码:年(2)+月(1)+日(1)+时(1)+分(1)+秒(1)+星期(1);先地位再高位
  96. //年:低端发送,低位在前
  97. y := []byte{
  98. byte(this.year),
  99. byte(this.year >> 8),
  100. byte(this.year >> 16),
  101. byte(this.year >> 24),
  102. }
  103. if y[0] == 0x00 && y[1] == 0x00 {
  104. binary.Write(w, binary.LittleEndian, y[2])
  105. binary.Write(w, binary.LittleEndian, y[3])
  106. } else {
  107. binary.Write(w, binary.LittleEndian, y[0])
  108. binary.Write(w, binary.LittleEndian, y[1])
  109. }
  110. //月
  111. m := []byte{
  112. byte(this.month),
  113. byte(this.month >> 8),
  114. byte(this.month >> 16),
  115. byte(this.month >> 24),
  116. }
  117. binary.Write(w, binary.LittleEndian, m[0])
  118. d := []byte{
  119. byte(this.day),
  120. byte(this.day >> 8),
  121. byte(this.day >> 16),
  122. byte(this.day >> 24),
  123. }
  124. binary.Write(w, binary.LittleEndian, d[0])
  125. h := []byte{
  126. byte(this.hour),
  127. byte(this.hour >> 8),
  128. byte(this.hour >> 16),
  129. byte(this.hour >> 24),
  130. }
  131. binary.Write(w, binary.LittleEndian, h[0])
  132. min := []byte{
  133. byte(this.minute),
  134. byte(this.minute >> 8),
  135. byte(this.minute >> 16),
  136. byte(this.minute >> 24),
  137. }
  138. binary.Write(w, binary.LittleEndian, min[0])
  139. s := []byte{
  140. byte(this.second),
  141. byte(this.second >> 8),
  142. byte(this.second >> 16),
  143. byte(this.second >> 24),
  144. }
  145. binary.Write(w, binary.LittleEndian, s[0])
  146. week := []byte{
  147. byte(this.week),
  148. byte(this.week >> 8),
  149. byte(this.week >> 16),
  150. byte(this.week >> 24),
  151. }
  152. binary.Write(w, binary.LittleEndian, week[0])
  153. return w.Bytes()
  154. }