获取当前帧bytes

This commit is contained in:
lijun 2025-01-23 22:40:07 +08:00
parent c6bc92c4de
commit 2a120d85f4
1 changed files with 15 additions and 2 deletions

View File

@ -97,6 +97,8 @@ func GetDeviceCurrentFrameV4(deviceId int, curCount int) (buf []byte, cnt int) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Printf("设备:%d 错误: %v\n", deviceId, err) log.Printf("设备:%d 错误: %v\n", deviceId, err)
// 异常时释放 buf
buf = nil
} }
}() }()
//获取读写锁 //获取读写锁
@ -120,21 +122,32 @@ func GetDeviceCurrentFrameV4(deviceId int, curCount int) (buf []byte, cnt int) {
return nil, frameCount return nil, frameCount
} }
//获取当前帧 //获取当前帧
var tempBuf []byte
switch deviceId { switch deviceId {
case 1: case 1:
if Device1CurrentFrame.Empty() { if Device1CurrentFrame.Empty() {
return nil, -1 return nil, -1
} }
buf = Device1CurrentFrame.ToBytes() tempBuf = Device1CurrentFrame.ToBytes()
case 50: case 50:
if Device50CurrentFrame.Empty() { if Device50CurrentFrame.Empty() {
return nil, -1 return nil, -1
} }
buf = Device50CurrentFrame.ToBytes() tempBuf = Device50CurrentFrame.ToBytes()
case 73: case 73:
if Device73CurrentFrame.Empty() { if Device73CurrentFrame.Empty() {
return nil, -1 return nil, -1
} }
tempBuf = Device73CurrentFrame.ToBytes()
default:
return nil, -1
}
// 避免直接返回大数组,可考虑复制必要的数据
if len(tempBuf) > 0 {
buf = make([]byte, len(tempBuf))
copy(buf, tempBuf)
// 释放临时变量
tempBuf = nil
} }
return buf, frameCount return buf, frameCount
} }