From cb61a204c29e9b14d0b3f4e677d8489fc40cba51 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 24 十二月 2025 11:20:33 +0800
Subject: [PATCH] 增加多流程JSON保存以及加载。
---
LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs | 199 ++++++++++++++++++++++++-------------------------
1 files changed, 99 insertions(+), 100 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/HRCamera.cs b/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
similarity index 95%
rename from LB_VisionProcesses/Cameras/HRCamera.cs
rename to LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
index f707aca..6aab716 100644
--- a/LB_VisionProcesses/Cameras/HRCamera.cs
+++ b/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
@@ -2,14 +2,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
-using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
-using System.Threading;
-using System.Windows.Forms;
+using System.Text;
+using System.Threading.Tasks;
-namespace LB_VisionProcesses.Cameras
+namespace LB_VisionProcesses.Cameras.HRCameras
{
/// <summary>
/// 鍗庣澘2D宸ヤ笟鐩告満瀹炵幇绫�
@@ -18,7 +17,7 @@
public class HRCamera : BaseCamera
{
#region 绉佹湁瀛楁
-
+
private MyCamera _camera; // 鍗庣澘鐩告満璁惧瀵硅薄
private Thread _grabThread; // 鍥惧儚閲囬泦绾跨▼
private bool _isGrabbing; // 閲囬泦鐘舵�佹爣蹇�
@@ -27,15 +26,15 @@
private Thread _callbackThread; // 鍥炶皟澶勭悊绾跨▼
private List<IMVDefine.IMV_Frame> _frameList; // 鍥惧儚缂撳瓨鍒楄〃
private readonly object _frameLock = new object(); // 甯х紦瀛橀攣
-
+
// CopyMemory API澹版槑
[System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
private static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
-
+
#endregion
#region 鏋勯�犲嚱鏁板拰鏋愭瀯鍑芥暟
-
+
/// <summary>
/// 鏋勯�犲嚱鏁�
/// </summary>
@@ -47,7 +46,7 @@
_isGrabbing = false;
_threadRunning = false;
}
-
+
/// <summary>
/// 鏋愭瀯鍑芥暟
/// </summary>
@@ -55,11 +54,11 @@
{
Dispose(false);
}
-
+
#endregion
#region 璁惧绠$悊鎿嶄綔
-
+
/// <summary>
/// 鑾峰彇鐩告満SN鏋氫妇鍒楄〃
/// </summary>
@@ -67,15 +66,15 @@
public override List<string> GetListEnum()
{
List<string> cameraList = new List<string>();
-
+
try
{
// 鏋氫妇鎵�鏈夎澶�
IMVDefine.IMV_DeviceList deviceList = new IMVDefine.IMV_DeviceList();
IMVDefine.IMV_EInterfaceType interfaceType = IMVDefine.IMV_EInterfaceType.interfaceTypeAll;
-
+
int result = MyCamera.IMV_EnumDevices(ref deviceList, (uint)interfaceType);
-
+
if (result == IMVDefine.IMV_OK && deviceList.nDevNum > 0)
{
for (int i = 0; i < deviceList.nDevNum; i++)
@@ -84,7 +83,7 @@
Marshal.PtrToStructure(
deviceList.pDevInfo + Marshal.SizeOf(typeof(IMVDefine.IMV_DeviceInfo)) * i,
typeof(IMVDefine.IMV_DeviceInfo));
-
+
string cameraInfo = $"{deviceInfo.cameraName}[{deviceInfo.serialNumber}]";
cameraList.Add(cameraInfo);
}
@@ -97,15 +96,15 @@
}
}
catch (Exception ex)
- {
+ {
// 璁板綍閿欒鏃ュ織
System.Diagnostics.Debug.WriteLine($"鑾峰彇鐩告満鍒楄〃澶辫触锛歿ex.Message}");
throw;
}
-
+
return cameraList;
}
-
+
/// <summary>
/// 鍒濆鍖栫浉鏈鸿澶�
/// </summary>
@@ -123,18 +122,18 @@
{
_camera.IMV_Close();
}
-
+
if (_handleCreated)
{
_camera.IMV_DestroyHandle();
_handleCreated = false;
}
}
-
+
// 鏋氫妇璁惧骞跺尮閰峉N
List<string> cameraList = GetListEnum();
int cameraIndex = -1;
-
+
for (int i = 0; i < cameraList.Count; i++)
{
if (cameraList[i].Contains(SN))
@@ -143,12 +142,12 @@
break;
}
}
-
+
if (cameraIndex == -1)
{
throw new Exception($"鏈壘鍒板簭鍒楀彿涓� {SN} 鐨勭浉鏈�");
}
-
+
// 鍒涘缓璁惧鍙ユ焺
int result = _camera.IMV_CreateHandle(IMVDefine.IMV_ECreateHandleMode.modeByIndex, cameraIndex);
if (result != IMVDefine.IMV_OK)
@@ -156,21 +155,21 @@
throw new Exception($"鍒涘缓璁惧鍙ユ焺澶辫触锛岄敊璇爜锛歿result}");
}
_handleCreated = true;
-
+
// 鎵撳紑璁惧
result = _camera.IMV_Open();
if (result != IMVDefine.IMV_OK)
{
throw new Exception($"鎵撳紑鐩告満澶辫触锛岄敊璇爜锛歿result}");
}
-
+
// 璁剧疆璁惧灞炴��
this.SN = SN;
isGrabbing = false;
-
+
// 璁剧疆缂撳瓨涓暟涓�8
_camera.IMV_SetBufferCount(8);
-
+
return true;
}
catch (Exception ex)
@@ -179,7 +178,7 @@
return false;
}
}
-
+
/// <summary>
/// 鍏抽棴鐩告満璁惧
/// </summary>
@@ -190,13 +189,13 @@
{
if (_camera == null)
return true;
-
+
// 鍋滄閲囬泦
if (_camera.IMV_IsGrabbing())
{
StopGrabbing();
}
-
+
// 鍏抽棴璁惧
if (_camera.IMV_IsOpen())
{
@@ -213,11 +212,11 @@
_camera.IMV_DestroyHandle();
_handleCreated = false;
}
-
+
// 閲婃斁璧勬簮
_isGrabbing = false;
isGrabbing = false;
-
+
// 娓呯悊甯х紦瀛�
lock (_frameLock)
{
@@ -228,7 +227,7 @@
}
_frameList.Clear();
}
-
+
return true;
}
catch (Exception ex)
@@ -237,11 +236,11 @@
return false;
}
}
-
+
#endregion
#region 鍥惧儚閲囬泦鎿嶄綔
-
+
/// <summary>
/// 寮�濮嬪浘鍍忛噰闆�
/// </summary>
@@ -254,29 +253,29 @@
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
// 鍋滄鐜版湁閲囬泦
if (_isGrabbing)
{
StopGrabbing();
}
-
+
// 鍚姩閲囬泦
int result = _camera.IMV_StartGrabbing();
if (result != IMVDefine.IMV_OK)
{
throw new Exception($"鍚姩閲囬泦澶辫触锛岄敊璇爜锛歿result}");
}
-
+
_isGrabbing = true;
isGrabbing = true;
-
+
// 鍚姩閲囬泦绾跨▼
_threadRunning = true;
_grabThread = new Thread(GrabThreadProc);
_grabThread.IsBackground = true;
_grabThread.Start();
-
+
return true;
}
catch (Exception ex)
@@ -287,7 +286,7 @@
return false;
}
}
-
+
/// <summary>
/// 鍋滄鍥惧儚閲囬泦
/// </summary>
@@ -298,7 +297,7 @@
{
if (_camera == null || !_isGrabbing)
return true;
-
+
// 鍋滄绾跨▼
_threadRunning = false;
if (_grabThread != null && _grabThread.IsAlive)
@@ -306,17 +305,17 @@
_grabThread.Join(1000);
_grabThread = null;
}
-
+
// 鍋滄閲囬泦
int result = _camera.IMV_StopGrabbing();
if (result != IMVDefine.IMV_OK)
{
System.Diagnostics.Debug.WriteLine($"鍋滄閲囬泦澶辫触锛岄敊璇爜锛歿result}");
}
-
+
_isGrabbing = false;
isGrabbing = false;
-
+
// 娓呯悊缂撳瓨
lock (_frameLock)
{
@@ -327,7 +326,7 @@
}
_frameList.Clear();
}
-
+
return true;
}
catch (Exception ex)
@@ -336,7 +335,7 @@
return false;
}
}
-
+
/// <summary>
/// 鎵ц杞Е鍙�
/// </summary>
@@ -349,7 +348,7 @@
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
int result = _camera.IMV_ExecuteCommandFeature("TriggerSoftware");
return result == IMVDefine.IMV_OK;
}
@@ -359,11 +358,11 @@
return false;
}
}
-
+
#endregion
#region 鍙傛暟璁剧疆鍜岃幏鍙�
-
+
/// <summary>
/// 璁剧疆瑙﹀彂妯″紡
/// </summary>
@@ -378,7 +377,7 @@
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
// 璁剧疆瑙﹀彂妯″紡
string triggerMode = mode == TriggerMode.On ? "On" : "Off";
int result = _camera.IMV_SetEnumFeatureSymbol("TriggerMode", triggerMode);
@@ -386,7 +385,7 @@
{
throw new Exception($"璁剧疆瑙﹀彂妯″紡澶辫触锛岄敊璇爜锛歿result}");
}
-
+
// 璁剧疆瑙﹀彂婧�
if (mode == TriggerMode.On)
{
@@ -397,7 +396,7 @@
throw new Exception($"璁剧疆瑙﹀彂婧愬け璐ワ紝閿欒鐮侊細{result}");
}
}
-
+
return true;
}
catch (Exception ex)
@@ -406,7 +405,7 @@
return false;
}
}
-
+
/// <summary>
/// 鑾峰彇瑙﹀彂妯″紡
/// </summary>
@@ -417,14 +416,14 @@
{
mode = TriggerMode.Off;
source = TriggerSource.Software;
-
+
try
{
if (_camera == null || !_camera.IMV_IsOpen())
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
// 鑾峰彇瑙﹀彂妯″紡
IMVDefine.IMV_String triggerMode = new IMVDefine.IMV_String();
int result = _camera.IMV_GetEnumFeatureSymbol("TriggerMode", ref triggerMode);
@@ -432,7 +431,7 @@
{
mode = triggerMode.str == "On" ? TriggerMode.On : TriggerMode.Off;
}
-
+
// 鑾峰彇瑙﹀彂婧�
IMVDefine.IMV_String triggerSource = new IMVDefine.IMV_String();
result = _camera.IMV_GetEnumFeatureSymbol("TriggerSource", ref triggerSource);
@@ -440,7 +439,7 @@
{
source = GetTriggerSourceFromString(triggerSource.str);
}
-
+
return true;
}
catch (Exception ex)
@@ -449,7 +448,7 @@
return false;
}
}
-
+
/// <summary>
/// 璁剧疆鏇濆厜鏃堕棿
/// </summary>
@@ -463,7 +462,7 @@
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
// 楠岃瘉鏇濆厜鏃堕棿鑼冨洿
double minExp = 0, maxExp = 0;
int result = _camera.IMV_GetDoubleFeatureMin("ExposureTime", ref minExp);
@@ -471,25 +470,25 @@
{
throw new Exception($"鑾峰彇鏇濆厜鏃堕棿鏈�灏忓�煎け璐ワ紝閿欒鐮侊細{result}");
}
-
+
result = _camera.IMV_GetDoubleFeatureMax("ExposureTime", ref maxExp);
if (result != IMVDefine.IMV_OK)
{
throw new Exception($"鑾峰彇鏇濆厜鏃堕棿鏈�澶у�煎け璐ワ紝閿欒鐮侊細{result}");
}
-
+
if (value < minExp || value > maxExp)
{
throw new Exception($"鏇濆厜鏃堕棿瓒呭嚭鑼冨洿锛屾湁鏁堣寖鍥达細{minExp} - {maxExp}");
}
-
+
// 璁剧疆鏇濆厜鏃堕棿
result = _camera.IMV_SetDoubleFeatureValue("ExposureTime", value);
if (result != IMVDefine.IMV_OK)
{
throw new Exception($"璁剧疆鏇濆厜鏃堕棿澶辫触锛岄敊璇爜锛歿result}");
}
-
+
return true;
}
catch (Exception ex)
@@ -498,7 +497,7 @@
return false;
}
}
-
+
/// <summary>
/// 鑾峰彇鏇濆厜鏃堕棿
/// </summary>
@@ -507,14 +506,14 @@
public override bool GetExpouseTime(out double value)
{
value = 0;
-
+
try
{
if (_camera == null || !_camera.IMV_IsOpen())
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
int result = _camera.IMV_GetDoubleFeatureValue("ExposureTime", ref value);
return result == IMVDefine.IMV_OK;
}
@@ -524,7 +523,7 @@
return false;
}
}
-
+
/// <summary>
/// 璁剧疆澧炵泭
/// </summary>
@@ -538,9 +537,9 @@
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
string gainFeature = _camera.IMV_FeatureIsValid("Gain") ? "Gain" : "GainRaw";
-
+
// 楠岃瘉澧炵泭鑼冨洿
double minGain = 0, maxGain = 0;
int result = _camera.IMV_GetDoubleFeatureMin(gainFeature, ref minGain);
@@ -548,23 +547,23 @@
{
throw new Exception($"鑾峰彇澧炵泭鏈�灏忓�煎け璐ワ紝閿欒鐮侊細{result}");
}
-
+
result = _camera.IMV_GetDoubleFeatureMax(gainFeature, ref maxGain);
if (result != IMVDefine.IMV_OK)
{
throw new Exception($"鑾峰彇澧炵泭鏈�澶у�煎け璐ワ紝閿欒鐮侊細{result}");
}
-
+
if (gain < minGain) gain = minGain;
if (gain > maxGain) gain = maxGain;
-
+
// 璁剧疆澧炵泭
result = _camera.IMV_SetDoubleFeatureValue(gainFeature, gain);
if (result != IMVDefine.IMV_OK)
{
throw new Exception($"璁剧疆澧炵泭澶辫触锛岄敊璇爜锛歿result}");
}
-
+
return true;
}
catch (Exception ex)
@@ -573,7 +572,7 @@
return false;
}
}
-
+
/// <summary>
/// 鑾峰彇澧炵泭鍊�
/// </summary>
@@ -582,14 +581,14 @@
public override bool GetGain(out double gain)
{
gain = 0;
-
+
try
{
if (_camera == null || !_camera.IMV_IsOpen())
{
throw new Exception("鐩告満鏈墦寮�");
}
-
+
string gainFeature = _camera.IMV_FeatureIsValid("Gain") ? "Gain" : "GainRaw";
int result = _camera.IMV_GetDoubleFeatureValue(gainFeature, ref gain);
return result == IMVDefine.IMV_OK;
@@ -600,7 +599,7 @@
return false;
}
}
-
+
/// <summary>
/// 璁剧疆瑙﹀彂鏋佹��
/// </summary>
@@ -611,10 +610,10 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
- string activation = (polarity == TriggerPolarity.RisingEdge || polarity == TriggerPolarity.HighLevel)
+
+ string activation = (polarity == TriggerPolarity.RisingEdge || polarity == TriggerPolarity.HighLevel)
? "RisingEdge" : "FallingEdge";
-
+
int result = _camera.IMV_SetEnumFeatureSymbol("TriggerActivation", activation);
return result == IMVDefine.IMV_OK;
}
@@ -636,7 +635,7 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
IMVDefine.IMV_String activation = new IMVDefine.IMV_String();
int result = _camera.IMV_GetEnumFeatureSymbol("TriggerActivation", ref activation);
if (result == IMVDefine.IMV_OK)
@@ -661,7 +660,7 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
// 鍗庣澘鐩告満閫氬父浣跨敤 LineDebouncerTime 鎺у埗婊ゆ尝
if (_camera.IMV_FeatureIsValid("LineDebouncerTime"))
{
@@ -686,7 +685,7 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
if (_camera.IMV_FeatureIsValid("LineDebouncerTime"))
{
int result = _camera.IMV_GetDoubleFeatureValue("LineDebouncerTime", ref flitertime);
@@ -709,7 +708,7 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
if (_camera.IMV_FeatureIsValid("TriggerDelay"))
{
int result = _camera.IMV_SetDoubleFeatureValue("TriggerDelay", delay);
@@ -733,7 +732,7 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
if (_camera.IMV_FeatureIsValid("TriggerDelay"))
{
int result = _camera.IMV_GetDoubleFeatureValue("TriggerDelay", ref delay);
@@ -756,11 +755,11 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
// 閫夋嫨绾胯矾
int result = _camera.IMV_SetEnumFeatureSymbol("LineSelector", line.ToString());
if (result != IMVDefine.IMV_OK) return false;
-
+
// 璁剧疆妯″紡
string lineMode = mode == LineMode.Input ? "Input" : "Output";
result = _camera.IMV_SetEnumFeatureSymbol("LineMode", lineMode);
@@ -781,11 +780,11 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
// 浠呭杈撳嚭绾胯矾鏈夋晥
int result = _camera.IMV_SetEnumFeatureSymbol("LineSelector", line.ToString());
if (result != IMVDefine.IMV_OK) return false;
-
+
bool status = linestatus == LineStatus.Hight;
result = _camera.IMV_SetBoolFeatureValue("UserOutputValue", status);
return result == IMVDefine.IMV_OK;
@@ -806,10 +805,10 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
int result = _camera.IMV_SetEnumFeatureSymbol("LineSelector", line.ToString());
if (result != IMVDefine.IMV_OK) return false;
-
+
bool status = false;
result = _camera.IMV_GetBoolFeatureValue("LineStatus", ref status);
if (result == IMVDefine.IMV_OK)
@@ -834,7 +833,7 @@
try
{
if (_camera == null || !_camera.IMV_IsOpen()) return false;
-
+
if (_camera.IMV_FeatureIsValid("BalanceWhiteAuto"))
{
int result = _camera.IMV_SetEnumFeatureSymbol("BalanceWhiteAuto", "Once");
@@ -1106,9 +1105,9 @@
#region IDisposable瀹炵幇
-
+
private bool _disposed = false;
-
+
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
@@ -1117,20 +1116,20 @@
{
// 閲婃斁鎵樼璧勬簮
}
-
+
// 閲婃斁闈炴墭绠¤祫婧�
CloseDevice();
-
+
_disposed = true;
}
}
-
+
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
-
+
#endregion
}
-}
\ No newline at end of file
+}
--
Gitblit v1.9.3