radar_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "testing"
  6. )
  7. func Test_verify(t *testing.T) {
  8. //Name := "光传感器"
  9. //decodeAddress, _ := hex.DecodeString("01")
  10. //dataPack := envSensor.DataPack{Address: decodeAddress, Start: []byte{0x01, 0xF8}, DataLen: []byte{0x00, 0x08}}
  11. //
  12. //if Name == "光传感器" {
  13. // dataPack = envSensor.DataPack{Address: decodeAddress, Start: []byte{0x00, 0x06}, DataLen: []byte{0x00, 0x01}}
  14. //}
  15. //buffer := dataPack.GetEnvGatherCommand()
  16. //fmt.Println("++++++++", hex.EncodeToString(buffer.Bytes()))
  17. //
  18. //xxx := "010310030900ba012c000000000000000100016324" //42 14
  19. //
  20. //gz, _ := strconv.ParseInt(xxx[30:38], 16, 0)
  21. ////s := xxx[30:38]
  22. //fmt.Println("截取: ", gz)
  23. //hexStr := "7830303940790d0a"
  24. //// 调用解析函数
  25. //
  26. //hex.EncodeToString([]byte{})
  27. //result := parseHexString(hexStr)
  28. //// 输出结果
  29. //fmt.Println(result)
  30. //xxx := "7830313940790d0a"
  31. //char := parseHexString("7830313940790d0a")
  32. //fmt.Println("+++++++", char)
  33. //fmt.Println("len++", len(xxx))
  34. //fmt.Println(",,,", xxx[0:2], xxx[8:16])
  35. //dataPack := envSensor.DataPack{Address: "02", Start: []byte{0x00, 0x06}, DataLen: []byte{0x00, 0x01}}
  36. //command := dataPack.GetEnvGatherCommand()
  37. //fmt.Println("+++++", hex.EncodeToString(command.Bytes()))
  38. //updates := map[string]interface{}{
  39. // "id": 1,
  40. // "startTime": "00:30",
  41. // "endTime": "00:20",
  42. //}
  43. //err := config.UpdateYAMLConfig(updates)
  44. //if err != nil {
  45. // fmt.Println(err)
  46. //}
  47. //pack := switchRelay.DataPack{Address: "fe", Way: 2, Command: 0}
  48. //command := pack.GetSwitchRelayCommand()
  49. //fmt.Println("MingLing。。。", hex.EncodeToString(command.Bytes()))
  50. ints := []int{1, 2, 3}
  51. state := generateHexSwitchState(ints)
  52. fmt.Println("转换后:", hex.EncodeToString(state))
  53. }
  54. func parseHexString(hexStr string) string {
  55. // 将十六进制字符串转换为字节数组
  56. bytes, err := hex.DecodeString(hexStr)
  57. if err != nil {
  58. fmt.Println("错误: 十六进制字符串解析失败", err)
  59. return ""
  60. }
  61. // 将字节数组转换为对应的字符
  62. return string(bytes)
  63. }
  64. func generateHexSwitchState(switches []int) []byte {
  65. var state int
  66. // 遍历切片,设置对应位置的开关为 1
  67. for _, s := range switches {
  68. state |= 1 << (s - 1) // 设置第s位为1,s从1开始
  69. }
  70. // 返回字节切片,使用 []byte{} 来包裹十六进制值
  71. return []byte{byte(state)}
  72. }
  73. // UpdateYAMLConfig 用于更新 YAML 配置文件中的多个值