public static function packetData$cmd=$subcmd=$content= $byte=array; $byte=self$symbolself$s1_1;帧头2位 $byte=self$symbolself$s1_2; $byte=self$symbolself$s1_3; $byte=se
func packetData(cmd string, subcmd string, content string) map[string]interface{} { byte := []string{} byte = append(byte, symbol+s1_1) //帧头2位 byte = append(byte, symbol+s1_2) byte = append(byte, symbol+s1_3) byte = append(byte, symbol+s1_4) byte = append(byte, symbol+cmd) //命令1位 if cmd == "62" { len := 19 if content != "" { content += " " gbkstr := mahonia.NewEncoder("GBK").ConvertString(content) //中文编码GBK len += len(gbkstr) lenTxt := len(gbkstr) } byte = append(byte, strconv.Itoa(len)) byte = append(byte, subcmd) //行号 byte = append(byte, "15") //(($len_txt>2)?'15':'00');//显示模式 byte = append(byte, "01") //显示速度 byte = append(byte, "00") //停留模式 byte = append(byte, "00") //停留时间 byte = append(byte, "01") //退出模式 byte = append(byte, "01") //退出速度 byte = append(byte, "03") //字体类型 byte = append(byte, "00") //显示次数 if subcmd == "00" || subcmd == "02" { byte = append(byte, "FF") //红色分量 byte = append(byte, "00") byte = append(byte, "00") } else { byte = append(byte, "FF") //绿色分量 byte = append(byte, "FF") byte = append(byte, "FF") } byte = append(byte, "00") byte = append(byte, "00") byte = append(byte, "00") byte = append(byte, "00") byte = append(byte, "00") byte = append(byte, strconv.Itoa(lenTxt)) byte = append(byte, "00") } else if cmd == "30" { len := 1 if content != "" { gbkstr := mahonia.NewEncoder("GBK").ConvertString(content) //中文编码GBK len += len(gbkstr) } byte = append(byte, strconv.Itoa(len)) byte = append(byte, "02") } else if cmd == "31" { byte = append(byte, "00") } else if cmd == "05" { byte = append(byte, "08") year := fmt.Sprintf("%04d", time.Now().Year()) byte = append(byte, year[2:]) byte = append(byte, year[:2]) byte = append(byte, strconv.Itoa(int(time.Now().Month()))) byte = append(byte, strconv.Itoa(time.Now().Day())) byte = append(byte, strconv.Itoa(int(time.Now().Weekday())+1)) byte = append(byte, strconv.Itoa(time.Now().Hour())) byte = append(byte, strconv.Itoa(time.Now().Minute())) byte = append(byte, strconv.Itoa(time.Now().Second())) } else if cmd == "0D" { byte = append(byte, "01") byte = append(byte, strconv.Itoa(strconv.Atoi(subcmd))) } else if cmd == "0F" { byte = append(byte, "06") byte = append(byte, "00") byte = append(byte, "01") byte = append(byte, "00") byte = append(byte, "00") byte = append(byte, "00") byte = append(byte, "05") }
if content != "" {
hex := str2hex(mahonia.NewEncoder("GBK").ConvertString(content))
contentArr := chunkSubN(hex, 2)
for _, v := range contentArr {
byte = append(byte, symbol+v)
}
}
test := ""
for _, v := range byte {
v = strings.ToUpper(v)
test += string(rune(hex2Dec(v)))
}
//CRC16校验
res := genCRC(test)
byte = append(byte, symbol+strings.ToUpper(res["lo"]))
byte = append(byte, symbol+strings.ToUpper(res["hi"]))
//base64
result := make(map[string]interface{})
result["byte"] = byte
result["dataLen"] = len(byte)
str := ""
for _, v := range byte {
str += string(rune(hex2Dec(v)))
}
result["data"] = base64.StdEncoding.EncodeToString([]byte(str))
return result
}
原文地址: https://www.cveoy.top/t/topic/b1Z6 著作权归作者所有。请勿转载和采集!