From ebcc1d53f14112363bbf539bcaf0202aadcdc9d7 Mon Sep 17 00:00:00 2001
From: C3032 <1057644574@qq.com>
Date: 星期一, 13 四月 2026 12:58:58 +0800
Subject: [PATCH] 2D取图计数功能完成
---
LB_SmartVision/VisionForm.cs | 143 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 143 insertions(+), 0 deletions(-)
diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index 8a041a1..216c564 100644
--- a/LB_SmartVision/VisionForm.cs
+++ b/LB_SmartVision/VisionForm.cs
@@ -1,6 +1,7 @@
锘縰sing HalconDotNet;
using LB_SmartVision.Forms;
using LB_SmartVision.Forms.Pages;
+using LB_SmartVision.Forms.Pages.BarcodeReaderPage;
using LB_SmartVision.Forms.Pages.CameraPage;
using LB_SmartVision.Forms.Pages.CommunicatorPage;
using LB_SmartVision.Forms.Pages.HistoricalData;
@@ -18,6 +19,8 @@
using LB_VisionProcesses.Cameras;
using LB_VisionProcesses.Cameras.HRCameras;
using LB_VisionProcesses.Cameras.LBCameras;
+using LB_VisionProcesses.BarcodeReaders;
+using LB_VisionProcesses.BarcodeReaders.Huayray;
using LB_VisionProcesses.Communicators;
using LB_VisionProcesses.Communicators.SiemensS7;
using LB_VisionProcesses.Communicators.TCom;
@@ -46,6 +49,7 @@
{
AllProcessesPage AllProcessesPages = new AllProcessesPage();
CamerasEditPage CamerasEditPage = new CamerasEditPage();
+ BarcodeReadersEditPage BarcodeReadersEditPage = new BarcodeReadersEditPage();
HistoricalDataEditPage HistoricalDataEditPage = new HistoricalDataEditPage();
CommunicatorsEditPage CommunicatorsEditPage = new CommunicatorsEditPage();
SettingEditPage SettingEditPage = new SettingEditPage();
@@ -68,6 +72,7 @@
GlobalVar.dicCommunicators.DictionaryChanged += CommunicatorsChanged;
GlobalVar.dicCameras.DictionaryChanged += CamerasChanged;
+ GlobalVar.dicBarcodeReaders.DictionaryChanged += BarcodeReadersChanged;
GlobalVar.dicProcesses.DictionaryChanged += ProcessRunBllChanged;
//鏈�寮�濮嬪氨娓呯┖鎵�鏈塗ab椤�
@@ -80,6 +85,9 @@
CamerasEditPage.LogInfo += LogInfo;
materialTabControl.Controls.Add(new MyPage(CamerasEditPage));
+
+ BarcodeReadersEditPage.LogInfo += LogInfo;
+ materialTabControl.Controls.Add(new MyPage(BarcodeReadersEditPage));
CommunicatorsEditPage.LogInfo += LogInfo;
materialTabControl.Controls.Add(new MyPage(CommunicatorsEditPage));
@@ -573,6 +581,26 @@
catch { }
}
+ private void BarcodeReadersChanged(object sender, DictionaryChangedEventArgs<string, BarcodeReaderBase> e)
+ {
+ try
+ {
+ switch (e.ChangeType)
+ {
+ case DictionaryChangeType.Added:
+ LogInfo($"璇荤爜鍣╗{e.NewValue.SN}]鍔犺浇鎴愬姛", LogInfoType.INFO);
+ e.NewValue.SN = e.NewKey;
+ break;
+ case DictionaryChangeType.Removed:
+ if (e.OldValue != null)
+ e.OldValue.Dispose();
+ LogInfo($"璇荤爜鍣╗{e.OldValue.SN}]宸茬Щ闄�", LogInfoType.INFO);
+ break;
+ }
+ }
+ catch { }
+ }
+
private void VisionForm_Load(object sender, EventArgs e)
{
XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
@@ -623,6 +651,21 @@
else
{
LogInfo("鐩告満鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
+
+ //鍔犺浇璇荤爜鍣�
+ foreach (var reader in GlobalVar.dicBarcodeReaders.Values)
+ {
+ reader.Dispose();
+ }
+ GlobalVar.dicBarcodeReaders.Clear();
+ if (LoadAllBarcodeReaders(GlobalVar.allBarcodeReadersConnectionStringPath))
+ {
+ LogInfo("璇荤爜鍣ㄥ姞杞芥垚鍔�", LogInfoType.PASS);
+ }
+ else
+ {
+ LogInfo("璇荤爜鍣ㄥ姞杞藉け璐�", LogInfoType.ERROR);
}
//鍔犺浇鍏ㄥ眬鍙橀噺
IProcess.dicGlobalVars.Clear();
@@ -705,6 +748,7 @@
SaveAllProcessVars();
SaveAllCommunicators();
SaveAllCameras();
+ SaveAllBarcodeReaders();
SaveAllProcessSetting();
SaveAllLayout();
SaveAllCsv();
@@ -862,6 +906,105 @@
catch { return false; }
}
+ public bool LoadAllBarcodeReaders(string allBarcodeReadersConnectionStringPath)
+ {
+ if (!File.Exists(allBarcodeReadersConnectionStringPath))
+ {
+ Debug.WriteLine("璇荤爜鍣ㄩ厤缃枃浠朵笉瀛樺湪锛屽垱寤虹┖鏂囦欢");
+ SaveAllBarcodeReaders();
+ return true;
+ }
+ string strJson = string.Empty;
+ using (StreamReader streamReader = new StreamReader(allBarcodeReadersConnectionStringPath, Encoding.UTF8))
+ {
+ strJson = streamReader.ReadToEnd();
+ streamReader.Close();
+ }
+ GlobalVar.allBarcodeReadersConnectionString = JsonConvert.DeserializeObject<ConcurrentDictionary<string, string>>(strJson);
+ if (GlobalVar.allBarcodeReadersConnectionString == null)
+ {
+ MessageBox.Show("璇荤爜鍣ㄥ姞杞藉け璐�!", "寮傚父");
+ return false;
+ }
+ BarcodeReaderBase reader = null;
+ foreach (var ReaderConnectionString in GlobalVar.allBarcodeReadersConnectionString)
+ {
+ Enum.TryParse<BarcodeReaderBrand>(ReaderConnectionString.Value, out BarcodeReaderBrand brand);
+ switch (brand)
+ {
+ case BarcodeReaderBrand.Huayray:
+ {
+ reader = new HRBarcodeReader();
+ break;
+ }
+ default:
+ {
+ MessageBox.Show($"[{ReaderConnectionString.Key}]璇荤爜鍣ㄥ搧鐗屼笉鏀寔!", "寮傚父");
+ continue;
+ }
+ }
+ reader.SN = ReaderConnectionString.Key;
+ if (!reader.Open(ReaderConnectionString.Key))
+ {
+ LogInfo($"鍒濆鍖栬鐮佸櫒[{ReaderConnectionString.Key}]澶辫触", LogInfoType.ERROR);
+ reader.IsConnected = false;
+ reader.IsGrabbing = false;
+ }
+ else
+ {
+ LogInfo($"鍒濆鍖栬鐮佸櫒[{ReaderConnectionString.Key}]鎴愬姛", LogInfoType.PASS);
+ }
+
+ GlobalVar.dicBarcodeReaders.TryAdd(ReaderConnectionString.Key, reader);
+ }
+
+ return true;
+ }
+
+ public bool SaveAllBarcodeReaders()
+ {
+ try
+ {
+ string strJson = string.Empty;
+ GlobalVar.allBarcodeReadersConnectionString = new ConcurrentDictionary<string, string>();
+
+ foreach (var item in GlobalVar.dicBarcodeReaders)
+ {
+ string ReaderSN = item.Value.SN;
+ string ReaderBrand = item.Value.Brand.ToString();
+
+ if (string.IsNullOrEmpty(ReaderSN) || string.IsNullOrEmpty(ReaderBrand))
+ {
+ break;
+ }
+ GlobalVar.allBarcodeReadersConnectionString.TryAdd(ReaderSN, ReaderBrand);
+ }
+ var settings = new JsonSerializerSettings
+ {
+ Formatting = Formatting.Indented,
+ ContractResolver = new DefaultContractResolver
+ {
+ NamingStrategy = new CamelCaseNamingStrategy()
+ }
+ };
+
+ strJson = JsonConvert.SerializeObject(GlobalVar.allBarcodeReadersConnectionString, settings);
+ string directoryPath = Path.GetDirectoryName(GlobalVar.allBarcodeReadersConnectionStringPath);
+ if (!Directory.Exists(directoryPath))
+ {
+ try
+ {
+ Directory.CreateDirectory(directoryPath);
+ }
+ catch (Exception)
+ { }
+ }
+ File.WriteAllText(GlobalVar.allBarcodeReadersConnectionStringPath, strJson, Encoding.UTF8);
+ return true;
+ }
+ catch { return false; }
+ }
+
public bool LoadAllCommunicators(string allCommunicatorsConnectionStringPath)
{
--
Gitblit v1.9.3