diff --git a/server/src/Peripherals/CameraClient.cs b/server/src/Peripherals/CameraClient.cs index e207999..00c1cf2 100644 --- a/server/src/Peripherals/CameraClient.cs +++ b/server/src/Peripherals/CameraClient.cs @@ -303,6 +303,20 @@ class Camera return true; } + public async ValueTask> ReadRegisters(UInt16 register) + { + var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.taskID, this.timeout); + + // Convert 16-bit register address to byte array + var registerBytes = new byte[] { (byte)(register >> 8), (byte)(register & 0xFF) }; + + var ret = await i2c.ReadData(CAM_I2C_ADDR, registerBytes, 1, CAM_PROTO); + if (!ret.IsSuccessful) + return new(ret.Error); + + return new Result(ret.Value[0]); + } + /// /// 配置摄像头分辨率和相关参数 /// @@ -1282,11 +1296,11 @@ class Camera // 步骤2: 读取寄存器 0x3029 的状态,如果返回值为 0x10,代表对焦已完成 int iteration = 5000; - byte focusStatus = 0x00; - + byte focusStatus; + do { - var readResult = await ReadRegister(0x3029); + var readResult = await ReadRegisters(0x3029); if (!readResult.IsSuccessful) { logger.Error($"读取对焦状态寄存器(0x3029)失败: {readResult.Error}"); @@ -1307,7 +1321,7 @@ class Camera return new(new Exception($"自动对焦超时,状态: 0x{focusStatus:X2}")); } - await Task.Delay(10); + await Task.Delay(100); } while (focusStatus != 0x10); diff --git a/server/src/Peripherals/I2cClient.cs b/server/src/Peripherals/I2cClient.cs index 7863f70..a783a67 100644 --- a/server/src/Peripherals/I2cClient.cs +++ b/server/src/Peripherals/I2cClient.cs @@ -201,7 +201,7 @@ public class I2c /// 操作结果,成功返回读取到的数据,否则返回异常信息 public async ValueTask> ReadData(UInt32 devAddr, byte[] data, int dataReadLength, I2cProtocol proto) { - if (dataReadLength <= 0 || dataReadLength > 0x0000_FFFF) + if (dataReadLength < 1 || dataReadLength > 0x0000_FFFF) { logger.Error($"Read length {dataReadLength} is invalid or exceeds maximum allowed 0x0000_FFFF"); return new(new ArgumentException($"Read length {dataReadLength} is invalid or exceeds maximum allowed 0x0000_FFFF"));