From 848ae3a7841a16322c23f9c0d10b2fab83a97c2c Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 15 四月 2026 14:25:36 +0800
Subject: [PATCH] 修改LB&HR相机外触发获取图像原。
---
LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs | 203 ++++++++++++++++++++++++++++++++++++++------------
1 files changed, 154 insertions(+), 49 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index 728e267..a459879 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -2,10 +2,13 @@
using LB_SmartVisionCameraSDK.PHM6000;
using LB_SmartVisionCommon;
using LB_VisionProcesses.Cameras;
+using OpenVinoSharp.Extensions.model;
+using SharpCompress.Common;
using Sunny.UI.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
+using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
@@ -33,12 +36,11 @@
{
private IntPtr _cameraHandle = IntPtr.Zero;
private PHM6000SensorConfig _sensorConfig;
-
// 閲囬泦鍥炶皟
private AcquisitionCallbackZA _acquisitionCallback;
private AcquisitionCompletedCallback _acquisitionCompletedCallback;
-
- private bool _isConnected = false;
+
+ public bool _isConnected = false;
private int _frameCount = 0; // 閲囬泦甯ц鏁�
// 鍥惧儚缂撳啿
@@ -96,11 +98,11 @@
{
byte[] moduleTypeBytes = new byte[64];
byte[] serialNumberBytes = new byte[64];
-
+
if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
{
string currentSn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
-
+
// 鍖归厤SN鎴朓P
if (currentSn == sn || sn.Contains(currentSn))
{
@@ -125,7 +127,7 @@
if (System.Net.IPAddress.TryParse(sn, out _))
{
targetIp = sn;
- targetPort = 5577;
+ targetPort = 5577;
}
else
{
@@ -140,7 +142,7 @@
var addr = Encoding.ASCII.GetBytes(targetIp);
int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, targetPort);
-
+
if (result == 0)
{
_isConnected = true;
@@ -167,8 +169,8 @@
AsyncLogHelper.Error($"LBCamera[{SN}]: ConnectToCamera failed, result={result}");
}
}
- catch (Exception ex)
- {
+ catch (Exception ex)
+ {
AsyncLogHelper.Error($"LBCamera: InitDevice寮傚父 - {ex.Message}");
if (tempHandle != IntPtr.Zero) PHM6000Profiler.DestroyCameraEntry(tempHandle);
}
@@ -205,7 +207,7 @@
if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
{
string sn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
- if (!string.IsNullOrEmpty(sn))
+ if (!string.IsNullOrEmpty(sn) && sn.Contains("L"))
{
cameraList.Add(sn);
}
@@ -247,7 +249,7 @@
public bool StartSingleGrab()
{
if (!_isConnected) return false;
-
+
_isContinuous = false;
InitBuffer();
AsyncLogHelper.Info($"LBCamera[{SN}]: 寮�濮嬪崟娆¢噰闆�");
@@ -289,7 +291,7 @@
_isContinuous = false;
if (!_isConnected) return true;
PHM6000Profiler.StopAcquisition(_cameraHandle);
-
+
// 鍋滄鏃跺鏋滄湁鏈樉绀虹殑缂撳瓨鏁版嵁锛屽皢鍏舵樉绀哄嚭鏉ワ紙鏀寔鏄剧ず涓嶅畬鏁寸殑甯э級
lock (_bufferLock)
{
@@ -300,7 +302,7 @@
_currentLineCount = 0;
}
}
-
+
isGrabbing = false;
return true;
}
@@ -322,16 +324,16 @@
{
if (!_isConnected) return false;
- if (triggerEnum == TriggerSource.Software)
- {
- _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.鍥哄畾棰戠巼;
- _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.杞Е鍙�;
- }
- else
- {
- _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.缂栫爜鍣�;
- _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.澶栭儴瑙﹀彂;
- }
+ //if (triggerEnum == TriggerSource.Software)
+ //{
+ // _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.鍥哄畾棰戠巼;
+ // _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.杞Е鍙�;
+ //}
+ //else
+ //{
+ // _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.缂栫爜鍣�;
+ // _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.澶栭儴瑙﹀彂;
+ //}
UpdateSensorConfig(_sensorConfig);
return true;
}
@@ -353,13 +355,45 @@
public override bool GetLineStatus(IOLines line, out LineStatus lineStatus) { lineStatus = LineStatus.Low; return true; }
public override bool AutoBalanceWhite() => true;
public override void SetCamConfig(CameraConfig config) { }
- public override void GetCamConfig(out CameraConfig config) { config = new CameraConfig(null); }
- public override bool GetImage(out Bitmap bitmap, int outtime = 3000) { bitmap = null; return false; }
- public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
+ public override void GetCamConfig(out CameraConfig config)
+ {
+ config = new CameraConfig(null);
+ //UpdateSensorConfig(config);
+ }
+ public override bool GetImage(out Bitmap bitmap, int outtime = 14500)
+ {
+ bitmap = null;
+ try
+ {
+ // 璁剧疆瓒呮椂鏃堕棿
+ DateTime lastTime = DateTime.Now.AddMilliseconds(outtime);
+ // 鍒ゆ柇鏄惁瓒呮椂
+ while (lastTime > DateTime.Now)// 璁剧疆瓒呮椂鏃堕棿涓� 3 绉�
+ {
+ if (CallBackImg != null)
+ {
+ lock (CallBackImg)
+ {
+ // 淇濆瓨鏃� Bitmap 骞堕噴鏀�
+ bitmap = CallBackImg; // 鍒涘缓鍓湰
+ }
+
+ //// 閲婃斁鏃ц祫婧�
+ //CallBackImg.Dispose();
+ //CallBackImg = null;
+ return true;
+ }
+ }
+
+ return false;
+ }
+ catch { return bitmap == null ? false : true; }
+ }
+ public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
{
// 绠�鍗曞疄鐜帮細杞Е鍙戠瓑寰�
bitmap = null;
- if(!_isConnected) return false;
+ if (!_isConnected) return false;
// 璁$畻鐞嗚鏈�灏忚�楁椂 (浠呭綋浣跨敤鍥哄畾棰戠巼瑙﹀彂鏃�)
int minTime = 0;
@@ -377,18 +411,20 @@
actualTimeout = minTime + 3000; // 棰勭暀3绉掍綑閲�
AsyncLogHelper.Warn($"LBCamera: Provided timeout {outtime}ms is too short for {minTime}ms scan. Extended to {actualTimeout}ms.");
}
-
+
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
Bitmap res = null;
- EventHandler<CameraEventArgs> handler = (s, e) => {
- if(e.Bitmap != null) {
- res = e.Bitmap.Clone() as Bitmap;
- waitHandle.Set();
+ EventHandler<CameraEventArgs> handler = (s, e) =>
+ {
+ if (e.Bitmap != null)
+ {
+ res = e.Bitmap.Clone() as Bitmap;
+ waitHandle.Set();
}
};
ImageGrabbed += handler;
-
+
if (StartSingleGrab())
{
if (!waitHandle.WaitOne(actualTimeout))
@@ -402,10 +438,10 @@
}
ImageGrabbed -= handler;
-
+
// 纭繚鍋滄閲囬泦
StopGrabbing();
-
+
bitmap = res;
return bitmap != null;
}
@@ -419,22 +455,81 @@
public void UpdateSensorConfig(PHM6000SensorConfig config)
{
- _sensorConfig = config;
- if (!_isConnected) return;
- SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
- SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
- if (config.LineScanTriggerSource == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
+ //_sensorConfig = config;
+ //if (!_isConnected) return;
+ //SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
+ //SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
+ //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
+ //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
+ //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
+ //if (config.LineScanTriggerSource == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
+ //{
+ // PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
+ //}
+
+ int result = 0;
+ var type = config.GetType();
+ var propLineScan = type.GetProperty(nameof(config.LineScanTriggerSource));
+ var val = (EnumLineScanTriggerSource)propLineScan.GetValue(config);
+ var props = config.GetType().GetProperties();
+
+ //鎺掗櫎Y杞�
+ props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.YResolution)).ToArray();
+ //鎺掗櫎涓嶉渶瑕佺殑椤�
+ if (val == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
{
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
+ props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.EncoderTriggerDirection) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerInterval) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerSignalCountingMode)).ToArray();
+ }
+ else
+ {
+ props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.SoftwareTriggerRate)).ToArray();
+ }
+ foreach (var p in props)
+ {
+ //璺宠繃鑷畾涔夊弬鏁�
+ var iscustomAttr = p.GetCustomAttribute<IsCustomAttribute>();
+ if (iscustomAttr != null) continue;
+ //鍒ゆ柇鏄�6030浼犳劅鍣ㄨ繕鏄櫘閫氫紶鎰熷櫒
+ if (SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGain))
+ {
+ continue;
+ }
+ if (!SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGainFor6030))
+ {
+ continue;
+ }
+ var id = Convert.ToInt32(Enum.Parse(typeof(EnumNameId), p.Name));
+ if (p.PropertyType == typeof(int))
+ {
+ var value = Convert.ToInt32(p.GetValue(config));
+ result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, value, 0, 0);
+ }
+ else if (p.PropertyType == typeof(float))
+ {
+ var value = Convert.ToDouble(p.GetValue(config));
+ result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, value, 0);
+ }
+ else
+ {
+ var value = Convert.ToInt32(p.GetValue(config));
+ result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, 0, value);
+ }
+ if (result == -1)
+ {
+ var disattr = p.GetCustomAttribute<DisplayNameAttribute>();
+ var name = disattr?.DisplayName ?? p.Name;
+ throw new Exception($"璁剧疆鍙傛暟{name}鏃朵笉鎴愬姛锛�");
+ }
+ }
+ var finalResult = PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
+ if (finalResult != 0)
+ {
}
PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
}
#endregion
-
+
#endregion
#region Callbacks
@@ -469,7 +564,7 @@
// 鎻愬彇鐏板害(Intensity/Alpha)鏁版嵁濉厖鍒� _rawPixelBuffer
// LBPointZA缁撴瀯: float(4) + res(3) + alpha(1). Alpha鍦ㄥ亸绉�7
int bufferOffset = _currentLineCount * _currentBitmapWidth;
-
+
for (int i = 0; i < points; i++)
{
if (bufferOffset + i < _rawPixelBuffer.Length)
@@ -484,7 +579,7 @@
if (_currentLineCount >= _currentBitmapHeight)
{
CreateAndFireBitmap();
-
+
// 閲嶇疆锛屽噯澶囦笅涓�甯� (濡傛灉鏄繛缁噰闆�)
_currentLineCount = 0;
// _rawPixelBuffer 鍙互澶嶇敤锛屼笉闇�瑕佺疆绌�
@@ -503,7 +598,7 @@
{
// 濡傛灉鍦ㄨ繛缁ā寮忎笅鏀跺埌缁撴潫淇″彿锛屽皾璇曡嚜鍔ㄩ噸鍚噰闆�
AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous mode frame ended, restarting...");
- Task.Run(() =>
+ Task.Run(() =>
{
if (_isContinuous && _isConnected)
{
@@ -529,6 +624,10 @@
}
}
}
+ }
+ else if (nOption == 2)
+ {
+
}
}
@@ -665,12 +764,18 @@
{
// 瑙﹀彂鍥惧儚浜嬩欢
ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, clone, true));
+ CallBackImg = (Bitmap)clone.Clone();
}
// 瑙﹀彂妯″紡鍒ゆ柇
if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
{
if (mode == TriggerMode.On && source != TriggerSource.Software)
+ {
+ TriggerRunMessageReceived?.Invoke(SN, source.ToString());
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 纭Е鍙戜簨浠� - {source}");
+ }
+ else
{
TriggerRunMessageReceived?.Invoke(SN, source.ToString());
AsyncLogHelper.Info($"LBCamera[{SN}]: 纭Е鍙戜簨浠� - {source}");
@@ -711,7 +816,7 @@
int intValue = 0;
double doubleValue = 0;
int enumValue = 0;
-
+
if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)nameId, ref intValue, ref doubleValue, ref enumValue) == 0)
{
if (p.PropertyType == typeof(int))
--
Gitblit v1.9.3