探测器XP.Hardware.Detector类库为了更好集成新的探测器,统一接口方法,DetectorService重构为通过统一接口;

新增暗场校正和亮场校正帧数配置属性(默认 64,范围 1-128),config 加载校正帧数;
修正探测器IsConnected连接状态的判断逻辑。
This commit is contained in:
QI Mingxuan
2026-05-21 13:19:30 +08:00
parent 119d03a02b
commit 2d7cf17a3b
12 changed files with 333 additions and 69 deletions
@@ -1155,6 +1155,115 @@ namespace XP.Hardware.Detector.Implementations
#endregion
#region | Unified Interface Implementations
/// <summary>
/// 应用参数内部实现 | Apply parameters internal implementation
/// </summary>
protected override Task<DetectorResult> ApplyParametersInternalAsync(int binningIndex, int pga, decimal frameRate, CancellationToken cancellationToken)
{
return Task.Run(() =>
{
try
{
_logger?.Info($"应用参数:Binning={binningIndex}PGA={pga},帧率={frameRate} | Applying parameters: Binning={binningIndex}, PGA={pga}, FrameRate={frameRate}");
// 设置 Binning 模式 | Set binning mode
var binningMode = (BinningMode)binningIndex;
var result = XISLApi.Acquisition_SetCameraBinningMode(_hAcqDesc, (uint)binningMode + 1);
if (result != XISLApi.HIS_RETURN.HIS_ALL_OK)
{
return DetectorResult.Failure($"设置 Binning 模式失败 | Failed to set binning mode: {result}");
}
// Binning 变化时失效校正数据 | Invalidate correction data on binning change
if (_binningMode != binningMode)
{
_logger?.Info($"Binning 模式从 {_binningMode} 变更为 {binningMode},校正数据已失效 | Binning changed, correction data invalidated");
InvalidateCorrectionData();
}
_binningMode = binningMode;
// 设置增益模式 | Set gain mode
var gainMode = (GainMode)pga;
result = XISLApi.Acquisition_SetCameraGain(_hAcqDesc, (uint)gainMode);
if (result != XISLApi.HIS_RETURN.HIS_ALL_OK)
{
return DetectorResult.Failure($"设置增益模式失败 | Failed to set gain mode: {result}");
}
// PGA 变化时失效校正数据 | Invalidate correction data on PGA change
if (_gainMode != gainMode)
{
_logger?.Info($"PGA 从 {_gainMode} 变更为 {gainMode},校正数据已失效 | PGA changed, correction data invalidated");
InvalidateCorrectionData();
}
_gainMode = gainMode;
// 设置曝光时间(帧率→微秒)| Set exposure time (frame rate → microseconds)
uint exposureUs = frameRate > 0 ? (uint)(1_000_000m / frameRate) : 66667;
result = XISLApi.Acquisition_SetTimerSync(_hAcqDesc, ref exposureUs);
if (result != XISLApi.HIS_RETURN.HIS_ALL_OK)
{
return DetectorResult.Failure($"设置曝光时间失败 | Failed to set exposure time: {result}");
}
_exposureTime = exposureUs;
_logger?.Info("参数应用成功 | Parameters applied successfully");
return DetectorResult.Success("参数应用成功 | Parameters applied successfully");
}
catch (Exception ex)
{
return DetectorResult.Failure($"应用参数异常 | Apply parameters exception: {ex.Message}", ex);
}
}, cancellationToken);
}
/// <summary>
/// 获取 Varex 校正能力描述 | Get Varex correction capabilities
/// </summary>
public override CorrectionCapabilities GetCorrectionCapabilities()
{
return new CorrectionCapabilities
{
RequiresStopBeforeCorrection = true,
RequiresParameterApplyBeforeDark = true,
AutoBadPixelAfterGain = true,
PostStopDelayMs = 500,
DarkFrameCount = _config.DarkCorrectionFrameCount,
GainFrameCount = _config.GainCorrectionFrameCount,
FrameCountMin = 1,
FrameCountMax = 128
};
}
/// <summary>
/// 失效校正数据(释放校正缓冲区)| Invalidate correction data (free correction buffers)
/// </summary>
public override void InvalidateCorrectionData()
{
if (_pOffsetBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(_pOffsetBuffer);
_pOffsetBuffer = IntPtr.Zero;
_offsetBufferRows = 0;
_offsetBufferColumns = 0;
}
if (_pGainBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(_pGainBuffer);
_pGainBuffer = IntPtr.Zero;
}
if (_pCorrList != IntPtr.Zero)
{
Marshal.FreeHGlobal(_pCorrList);
_pCorrList = IntPtr.Zero;
}
_logger?.Debug("校正数据已失效并释放 | Correction data invalidated and freed");
}
#endregion
#region IVarexDetector | IVarexDetector Interface Implementations (Placeholders)
/// <summary>