You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package m9z
import (
"fmt"
)
// 版本信息结构体(类似SunRiseSet的设计)
type VersionInfo struct {
Version string // 版本号,如"v4.5"
RawByte byte // 原始字节值,如0x45
}
// ReadVersion 读取设备固件版本号(风格与SunRiseSetRead保持一致)
func ReadVersion(deviceId string) (*VersionInfo, error) {
var versionReadCmd = []byte{0xEE, 0x0E, 0x01, 0x00, 0x06, 0x00, 0x50, 0x01, 0x01, 0x08, 0x20, 0x00, 0xC2, 0xFF}
cmdRespData, err := pushCommand(deviceId, versionReadCmd, "读取设备版本号")
if err != nil {
return nil, err
}
frame, err := ParseFrame(cmdRespData)
if err != nil {
return nil, err
}
response := frame.(*ReadResponse)
data := response.Data
// 校验数据长度(版本号应为1字节,参考协议中"数据项"格式)
if len(data) < 1 {
return nil, fmt.Errorf("版本数据长度不足,期望至少1字节,实际%v字节", len(data))
}
// 解析版本字节(核心逻辑:高4位为主版本,低4位为次版本)
versionByte := data[23]
mainVersion := (versionByte >> 4) & 0x0F // 提取高4位
subVersion := versionByte & 0x0F // 提取低4位
// 构造版本信息结构体
versionInfo := &VersionInfo{
Version: fmt.Sprintf("v%d.%d", mainVersion, subVersion),
RawByte: versionByte,
}
return versionInfo, nil
}