C3204
2026-01-15 a987bb05357a451c8476098067573059287dc008
LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
@@ -1,4 +1,7 @@
using MVSDK_Net;
using HalconDotNet;
using LB_SmartVisionCommon;
using LB_VisionProcesses.Cameras.LBCameras;
using MVSDK_Net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -26,6 +29,18 @@
        private Thread _callbackThread; // 回调处理线程
        private List<IMVDefine.IMV_Frame> _frameList; // 图像缓存列表
        private readonly object _frameLock = new object(); // 帧缓存锁
        // 新增:CollectedImages操作锁,保证线程安全
        private readonly object _collectedImagesLock = new object();
        private IMVDefine.IMV_EPixelType type = IMVDefine.IMV_EPixelType.gvspPixelMono8;
        private IMVDefine.IMV_PixelConvertParam stPixelConvertParam = new IMVDefine.IMV_PixelConvertParam();
        HObject Hobj = new HObject();
        IntPtr pTemp = IntPtr.Zero;
        IntPtr pConvertDstBuffer = IntPtr.Zero;
        int nConvertBufSize = 0;
        private IMVDefine.IMV_FrameCallBack frameCallBack;
        // CopyMemory API声明
        [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
@@ -92,13 +107,15 @@
                {
                    // 记录日志或抛出异常
                    //throw new Exception($"枚举设备失败,错误码:{result}");
                    Debug.WriteLine("枚举设备失败!");
                    AsyncLogHelper.Error("枚举设备失败!");
                    System.Diagnostics.Debug.WriteLine("枚举设备失败!");
                }
            }
            catch (Exception ex)
            {
                // 记录错误日志
                System.Diagnostics.Debug.WriteLine($"获取相机列表失败:{ex.Message}");
                AsyncLogHelper.Error($"获取相机列表失败:{ex.Message}");
                throw;
            }
@@ -145,6 +162,7 @@
                if (cameraIndex == -1)
                {
                    AsyncLogHelper.Error($"未找到序列号为 {SN} 的相机");
                    throw new Exception($"未找到序列号为 {SN} 的相机");
                }
@@ -152,6 +170,7 @@
                int result = _camera.IMV_CreateHandle(IMVDefine.IMV_ECreateHandleMode.modeByIndex, cameraIndex);
                if (result != IMVDefine.IMV_OK)
                {
                    AsyncLogHelper.Error($"创建设备句柄失败,错误码:{result}");
                    throw new Exception($"创建设备句柄失败,错误码:{result}");
                }
                _handleCreated = true;
@@ -160,6 +179,7 @@
                result = _camera.IMV_Open();
                if (result != IMVDefine.IMV_OK)
                {
                    AsyncLogHelper.Error($"打开相机失败,错误码:{result}");
                    throw new Exception($"打开相机失败,错误码:{result}");
                }
@@ -167,17 +187,228 @@
                this.SN = SN;
                isGrabbing = false;
                // 设置缓存个数为8
                _camera.IMV_SetBufferCount(8);
                // 注册数据帧回调函数
                // Register data frame callback function
                frameCallBack = onGetFrame;
                int res = _camera.IMV_AttachGrabbing(frameCallBack, IntPtr.Zero);
                if (res != IMVDefine.IMV_OK)
                {
                    System.Diagnostics.Debug.WriteLine("Attach grabbing failed! ErrorCode:[{0}]", res);
                    AsyncLogHelper.Error("Attach grabbing failed! ErrorCode:[{0}]" + res);
                }
                // 设置缓存个数为12
                _camera.IMV_SetBufferCount(12);
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"初始化相机失败:{ex.Message}");
                AsyncLogHelper.Error($"初始化相机失败:{ex.Message}");
                return false;
            }
        }
        // 数据帧回调函数
        // Data frame callback function
        private void onGetFrame(ref IMVDefine.IMV_Frame frame, IntPtr pUser)
        {
            if (frame.frameHandle == IntPtr.Zero)
            {
                AsyncLogHelper.Info(SN + "frame is NULL");
                return;
            }
            try
            {
                // 1. 将帧转换为Bitmap
                Bitmap bitmap = ConvertFrameToBitmap(frame);
                // 释放原始帧数据(SDK层面释放)
                _camera.IMV_ReleaseFrame(ref frame);
                // 2. 空值校验:转换失败则直接返回
                if (bitmap == null)
                {
                    AsyncLogHelper.Warn(SN + "帧转换为Bitmap失败,跳过处理");
                    return;
                }
                // 3. 线程安全地将Bitmap添加到CollectedImages字典
                lock (_collectedImagesLock)
                {
                    // 确保当前相机SN对应的列表存在
                    if (!CollectedImages.ContainsKey(SN))
                    {
                        CollectedImages[SN] = new List<Bitmap>();
                    }
                    CollectedImages[SN].Add(bitmap);
                    AsyncLogHelper.Info(SN + $"图像已加入缓存,当前缓存数量:{CollectedImages[SN].Count}");
                }
                // 4. 处理CollectedImages中的图像:遍历消费列表第一个元素直到为空
                ProcessCollectedImages();
                //Task.Factory.StartNew(() =>
                //{
                //    CallBackImg = (Bitmap)bitmap.Clone();
                //    if (CallBackImg == null)
                //    {
                //        return;
                //    }
                //    if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                //    {
                //        if (mode == TriggerMode.On && source != TriggerSource.Software)
                //            TriggerRunMessageReceived?.Invoke(SN, source.ToString());  // 触发运行事件
                //    }
                //    bitmap.Dispose();
                //});
            }
            catch { }
            AsyncLogHelper.Info(SN + "Get frame blockId = {0}" + frame.frameInfo.blockId);
        }
        /// <summary>
        /// 处理CollectedImages中的缓存图像
        /// 核心逻辑:遍历取第一个图像 -> 赋值给CallBackImg -> 触发事件 -> 释放并移除
        /// </summary>
        private void ProcessCollectedImages()
        {
            Task.Factory.StartNew(() =>
            {
                // 加锁保证线程安全,防止多线程同时操作列表
                lock (_collectedImagesLock)
                {
                    // 校验当前相机的图像列表是否存在且有数据
                    if (!CollectedImages.ContainsKey(SN) || CollectedImages[SN].Count == 0)
                    {
                        AsyncLogHelper.Info(SN + "当前无缓存图像,跳过处理");
                        return;
                    }
                    // 循环处理:直到列表为空
                    while (CollectedImages[SN].Count > 0)
                    {
                        try
                        {
                            // 1 取列表第一个索引的图像赋值给CallBackImg
                            Bitmap firstBitmap = CollectedImages[SN][0];
                            ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, firstBitmap, true));
                            CallBackImg = (Bitmap)firstBitmap.Clone(); // 克隆避免原对象被释放后引用失效
                            // 2 获取触发模式并判断是否触发运行事件
                            if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                            {
                                // 硬触发模式下触发运行事件
                                if (mode == TriggerMode.On && source != TriggerSource.Software)
                                {
                                    AsyncLogHelper.Info(SN + $"触发硬触发事件,触发源:{source}");
                                    TriggerRunMessageReceived?.Invoke(SN, source.ToString());
                                }
                            }
                            else
                            {
                                AsyncLogHelper.Warn(SN + "获取触发模式失败,跳过事件触发");
                            }
                            // 3 释放第一个图像资源并从列表移除
                            // 先释放Bitmap内存,再移除列表元素
                            firstBitmap.Dispose();
                            CollectedImages[SN].RemoveAt(0);
                            AsyncLogHelper.Info(SN + $"已消费缓存图像,剩余缓存数量:{CollectedImages[SN].Count}");
                        }
                        catch (Exception ex)
                        {
                            AsyncLogHelper.Error(SN + $"处理缓存图像异常:{ex.Message}", ex);
                            // 单个图像处理失败时,移除该图像避免阻塞后续处理
                            if (CollectedImages[SN].Count > 0)
                            {
                                try
                                {
                                    CollectedImages[SN][0]?.Dispose(); // 尝试释放
                                    CollectedImages[SN].RemoveAt(0);
                                }
                                catch (Exception innerEx)
                                {
                                    AsyncLogHelper.Error(SN + $"清理异常图像失败:{innerEx.Message}", innerEx);
                                }
                            }
                            // 单个图像处理失败不终止循环,继续处理下一个
                            // 4. 所有图像处理完成后,清空CallBackImg
                            if (CallBackImg != null)
                            {
                                CallBackImg.Dispose();
                                CallBackImg = null;
                            }
                            continue;
                        }
                        // 4. 所有图像处理完成后,清空CallBackImg
                        if (CallBackImg != null)
                        {
                            CallBackImg.Dispose();
                            CallBackImg = null;
                        }
                    }
                }
            });
        }
        /// <summary>
        /// 图像是否为Mono格式
        /// </summary>
        /// <param name="enType"></param>
        /// <returns></returns>
        private bool IsMonoPixelFormat(IMVDefine.IMV_EPixelType enType)
        {
            switch (enType)
            {
                case IMVDefine.IMV_EPixelType.gvspPixelMono8:
                case IMVDefine.IMV_EPixelType.gvspPixelMono10:
                case IMVDefine.IMV_EPixelType.gvspPixelMono10Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelMono12:
                case IMVDefine.IMV_EPixelType.gvspPixelMono12Packed:
                    return true;
                default:
                    return false;
            }
        }
        /// <summary>
        /// 图像是否为彩色
        /// </summary>
        /// <param name="enType"></param>
        /// <returns></returns>
        private bool IsColorPixelFormat(IMVDefine.IMV_EPixelType enType)
        {
            switch (enType)
            {
                case IMVDefine.IMV_EPixelType.gvspPixelRGB8:
                case IMVDefine.IMV_EPixelType.gvspPixelBGR8:
                case IMVDefine.IMV_EPixelType.gvspPixelRGBA8:
                case IMVDefine.IMV_EPixelType.gvspPixelBGRA8:
                case IMVDefine.IMV_EPixelType.gvspPixelYUV422_8:
                case IMVDefine.IMV_EPixelType.gvspPixelYUV422_8_UYVY:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGR8:
                case IMVDefine.IMV_EPixelType.gvspPixelBayRG8:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGB8:
                case IMVDefine.IMV_EPixelType.gvspPixelBayBG8:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGB10:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGB10Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelBayBG10:
                case IMVDefine.IMV_EPixelType.gvspPixelBayBG10Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelBayRG10:
                case IMVDefine.IMV_EPixelType.gvspPixelBayRG10Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGR10:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGR10Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGB12:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGB12Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelBayBG12:
                case IMVDefine.IMV_EPixelType.gvspPixelBayBG12Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelBayRG12:
                case IMVDefine.IMV_EPixelType.gvspPixelBayRG12Packed:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGR12:
                case IMVDefine.IMV_EPixelType.gvspPixelBayGR12Packed:
                    return true;
                default:
                    return false;
            }
        }
        /// <summary>
        /// 关闭相机设备
@@ -203,6 +434,7 @@
                    if (result != IMVDefine.IMV_OK)
                    {
                        System.Diagnostics.Debug.WriteLine($"关闭相机失败,错误码:{result}");
                        AsyncLogHelper.Info(SN + $"关闭相机失败,错误码:{result}");
                    }
                }
@@ -233,6 +465,7 @@
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"关闭相机失败:{ex.Message}");
                AsyncLogHelper.Info(SN + $"关闭相机失败:{ex.Message}");
                return false;
            }
        }
@@ -251,7 +484,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                // 停止现有采集
@@ -264,7 +498,8 @@
                int result = _camera.IMV_StartGrabbing();
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"启动采集失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"启动采集失败,错误码:{result}");
                    throw new Exception(SN + $"启动采集失败,错误码:{result}");
                }
                _isGrabbing = true;
@@ -280,7 +515,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"启动采集失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"启动采集失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"启动采集失败:{ex.Message}");
                _isGrabbing = false;
                isGrabbing = false;
                return false;
@@ -310,7 +546,8 @@
                int result = _camera.IMV_StopGrabbing();
                if (result != IMVDefine.IMV_OK)
                {
                    System.Diagnostics.Debug.WriteLine($"停止采集失败,错误码:{result}");
                    System.Diagnostics.Debug.WriteLine(SN + $"停止采集失败,错误码:{result}");
                    AsyncLogHelper.Info(SN + $"停止采集失败,错误码:{result}");
                }
                _isGrabbing = false;
@@ -331,7 +568,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"停止采集失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"停止采集失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"停止采集失败:{ex.Message}");
                return false;
            }
        }
@@ -346,7 +584,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                int result = _camera.IMV_ExecuteCommandFeature("TriggerSoftware");
@@ -354,7 +593,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"软触发失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"软触发失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"软触发失败:{ex.Message}");
                return false;
            }
        }
@@ -375,7 +615,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                // 设置触发模式
@@ -383,7 +624,8 @@
                int result = _camera.IMV_SetEnumFeatureSymbol("TriggerMode", triggerMode);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"设置触发模式失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"设置触发模式失败,错误码:{result}");
                    throw new Exception(SN + $"设置触发模式失败,错误码:{result}");
                }
                // 设置触发源
@@ -393,7 +635,8 @@
                    result = _camera.IMV_SetEnumFeatureSymbol("TriggerSource", triggerSource);
                    if (result != IMVDefine.IMV_OK)
                    {
                        throw new Exception($"设置触发源失败,错误码:{result}");
                        AsyncLogHelper.Error(SN + $"设置触发源失败,错误码:{result}");
                        throw new Exception(SN + $"设置触发源失败,错误码:{result}");
                    }
                }
@@ -401,7 +644,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置触发模式失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置触发模式失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置触发模式失败:{ex.Message}");
                return false;
            }
        }
@@ -421,7 +665,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                // 获取触发模式
@@ -444,7 +689,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取触发模式失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"获取触发模式失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"获取触发模式失败:{ex.Message}");
                return false;
            }
        }
@@ -460,7 +706,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                // 验证曝光时间范围
@@ -468,32 +715,37 @@
                int result = _camera.IMV_GetDoubleFeatureMin("ExposureTime", ref minExp);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取曝光时间最小值失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"获取曝光时间最小值失败,错误码:{result}");
                    throw new Exception(SN + $"获取曝光时间最小值失败,错误码:{result}");
                }
                result = _camera.IMV_GetDoubleFeatureMax("ExposureTime", ref maxExp);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取曝光时间最大值失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"获取曝光时间最大值失败,错误码:{result}");
                    throw new Exception(SN + $"获取曝光时间最大值失败,错误码:{result}");
                }
                if (value < minExp || value > maxExp)
                {
                    throw new Exception($"曝光时间超出范围,有效范围:{minExp} - {maxExp}");
                    AsyncLogHelper.Error(SN + $"曝光时间超出范围,有效范围:{minExp} - {maxExp}");
                    throw new Exception(SN + $"曝光时间超出范围,有效范围:{minExp} - {maxExp}");
                }
                // 设置曝光时间
                result = _camera.IMV_SetDoubleFeatureValue("ExposureTime", value);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"设置曝光时间失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"设置曝光时间失败,错误码:{result}");
                    throw new Exception(SN + $"设置曝光时间失败,错误码:{result}");
                }
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置曝光时间失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置曝光时间失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置曝光时间失败:{ex.Message}");
                return false;
            }
        }
@@ -511,7 +763,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                int result = _camera.IMV_GetDoubleFeatureValue("ExposureTime", ref value);
@@ -519,7 +772,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取曝光时间失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"获取曝光时间失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"获取曝光时间失败:{ex.Message}");
                return false;
            }
        }
@@ -535,7 +789,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                string gainFeature = _camera.IMV_FeatureIsValid("Gain") ? "Gain" : "GainRaw";
@@ -545,13 +800,15 @@
                int result = _camera.IMV_GetDoubleFeatureMin(gainFeature, ref minGain);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取增益最小值失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"获取增益最小值失败,错误码:{result}");
                    throw new Exception(SN + $"获取增益最小值失败,错误码:{result}");
                }
                result = _camera.IMV_GetDoubleFeatureMax(gainFeature, ref maxGain);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"获取增益最大值失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"获取增益最大值失败,错误码:{result}");
                    throw new Exception(SN + $"获取增益最大值失败,错误码:{result}");
                }
                if (gain < minGain) gain = minGain;
@@ -561,14 +818,16 @@
                result = _camera.IMV_SetDoubleFeatureValue(gainFeature, gain);
                if (result != IMVDefine.IMV_OK)
                {
                    throw new Exception($"设置增益失败,错误码:{result}");
                    AsyncLogHelper.Error(SN + $"设置增益失败,错误码:{result}");
                    throw new Exception(SN + $"设置增益失败,错误码:{result}");
                }
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置增益失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置增益失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置增益失败:{ex.Message}");
                return false;
            }
        }
@@ -586,7 +845,8 @@
            {
                if (_camera == null || !_camera.IMV_IsOpen())
                {
                    throw new Exception("相机未打开");
                    AsyncLogHelper.Error(SN + "相机未打开");
                    throw new Exception(SN + "相机未打开");
                }
                string gainFeature = _camera.IMV_FeatureIsValid("Gain") ? "Gain" : "GainRaw";
@@ -595,7 +855,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取增益失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"获取增益失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"获取增益失败:{ex.Message}");
                return false;
            }
        }
@@ -619,7 +880,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置触发极性失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置触发极性失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置触发极性失败:{ex.Message}");
                return false;
            }
        }
@@ -647,7 +909,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取触发极性失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"获取触发极性失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"获取触发极性失败:{ex.Message}");
                return false;
            }
        }
@@ -671,7 +934,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置触发滤波失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置触发滤波失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置触发滤波失败:{ex.Message}");
                return false;
            }
        }
@@ -695,7 +959,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取触发滤波失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"获取触发滤波失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"获取触发滤波失败:{ex.Message}");
                return false;
            }
        }
@@ -718,7 +983,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置触发延时失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置触发延时失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置触发延时失败:{ex.Message}");
                return false;
            }
        }
@@ -742,7 +1008,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取触发延时失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"获取触发延时失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"获取触发延时失败:{ex.Message}");
                return false;
            }
        }
@@ -767,7 +1034,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置信号线模式失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置信号线模式失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置信号线模式失败:{ex.Message}");
                return false;
            }
        }
@@ -791,7 +1059,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"设置信号线状态失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"设置信号线状态失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"设置信号线状态失败:{ex.Message}");
                return false;
            }
        }
@@ -820,7 +1089,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取信号线状态失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"获取信号线状态失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"获取信号线状态失败:{ex.Message}");
                return false;
            }
        }
@@ -843,7 +1113,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"自动白平衡失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"自动白平衡失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"自动白平衡失败:{ex.Message}");
                return false;
            }
        }
@@ -880,14 +1151,16 @@
                        if ((uint)result != 0x80000001 && result != -119 && result != -102) // 超时错误代码
                        {
                            // 非超时错误
                            System.Diagnostics.Debug.WriteLine($"获取图像帧失败,错误码:{result}");
                            System.Diagnostics.Debug.WriteLine(SN + $"获取图像帧失败,错误码:{result}");
                            AsyncLogHelper.Error(SN + $"获取图像帧失败,错误码:{result}");
                            Thread.Sleep(10); // 出错时稍作等待
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine($"采集线程异常:{ex.Message}");
                    System.Diagnostics.Debug.WriteLine(SN + $"采集线程异常:{ex.Message}");
                    AsyncLogHelper.Error(SN + $"采集线程异常:{ex.Message}");
                    Thread.Sleep(10);
                }
@@ -918,7 +1191,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"处理图像帧失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"处理图像帧失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"处理图像帧失败:{ex.Message}");
            }
            finally
            {
@@ -957,7 +1231,8 @@
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"图像格式转换失败:{ex.Message}");
                System.Diagnostics.Debug.WriteLine(SN + $"图像格式转换失败:{ex.Message}");
                AsyncLogHelper.Error(SN + $"图像格式转换失败:{ex.Message}");
                return null;
            }
        }