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
}