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 | 569 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 512 insertions(+), 57 deletions(-)
diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index 7b6a9bf..216c564 100644
--- a/LB_SmartVision/VisionForm.cs
+++ b/LB_SmartVision/VisionForm.cs
@@ -1,21 +1,28 @@
锘縰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;
using LB_SmartVision.Forms.Pages.MESPage;
using LB_SmartVision.Forms.Pages.MotionControlPage;
using LB_SmartVision.Forms.Pages.ProcessPage;
using LB_SmartVision.Forms.Pages.SettingPage;
using LB_SmartVision.Forms.Pages.UserManagementPage;
using LB_SmartVision.ProcessRun;
+using LB_SmartVision.SQL;
using LB_SmartVision.Tool;
using LB_SmartVisionCommon;
using LB_SmartVisionLoginUI;
using LB_VisionProcesses;
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;
using LB_VisionProcesses.Forms;
using log4net.Config;
@@ -42,6 +49,8 @@
{
AllProcessesPage AllProcessesPages = new AllProcessesPage();
CamerasEditPage CamerasEditPage = new CamerasEditPage();
+ BarcodeReadersEditPage BarcodeReadersEditPage = new BarcodeReadersEditPage();
+ HistoricalDataEditPage HistoricalDataEditPage = new HistoricalDataEditPage();
CommunicatorsEditPage CommunicatorsEditPage = new CommunicatorsEditPage();
SettingEditPage SettingEditPage = new SettingEditPage();
MESEditPage MESEditPage = new MESEditPage();
@@ -62,6 +71,8 @@
Assembly_LB_VisionProcessesDll = Assembly.Load(Assembly_LB_VisionProcessesBytes);
GlobalVar.dicCommunicators.DictionaryChanged += CommunicatorsChanged;
+ GlobalVar.dicCameras.DictionaryChanged += CamerasChanged;
+ GlobalVar.dicBarcodeReaders.DictionaryChanged += BarcodeReadersChanged;
GlobalVar.dicProcesses.DictionaryChanged += ProcessRunBllChanged;
//鏈�寮�濮嬪氨娓呯┖鎵�鏈塗ab椤�
@@ -69,9 +80,14 @@
AllProcessesPages.controlsPanel.Dock = DockStyle.Fill;
materialTabControl.Controls.Add(AllProcessesPages);
+ HistoricalDataEditPage.LogInfo += LogInfo;
+ materialTabControl.Controls.Add(new MyPage(HistoricalDataEditPage));
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));
@@ -233,7 +249,30 @@
private void CommunicatorsChanged(object? sender, DictionaryChangedEventArgs<string, BaseCommunicator> e)
{
-
+ try
+ {
+ switch (e.ChangeType)
+ {
+ case DictionaryChangeType.Added:
+ e.NewValue.TriggerRunMessageReceived += TriggerRunMessageReceived;
+ LogInfo($"閫氳鍙{e.NewValue.CommunicatorName}]鍔犺浇瑙﹀彂閫氳", LogInfoType.INFO);
+ e.NewValue.CommunicatorName = e.NewKey;
+ break;
+ case DictionaryChangeType.Renamed:
+ string OldCommunicatorName = e.OldKey;
+ string NewCommunicatorName = e.NewKey;
+ LogInfo(string.Format("閲嶅懡鍚嶉�氳鍙e悕[{0}]淇敼涓篬{1}]", OldCommunicatorName, NewCommunicatorName), LogInfoType.INFO);
+ e.NewValue.CommunicatorName = NewCommunicatorName;
+ break;
+ case DictionaryChangeType.Removed:
+ if (e.OldValue != null && e.OldValue is BaseCommunicator)
+ e.OldValue.Disconnect();
+ e.OldValue.TriggerRunMessageReceived -= TriggerRunMessageReceived;
+ LogInfo($"閫氳鍙{e.OldValue.CommunicatorName}]绉婚櫎瑙﹀彂閫氳", LogInfoType.INFO);
+ break;
+ }
+ }
+ catch { }
}
private void LogInfo(string strLog, LogInfoType infoType)
@@ -358,7 +397,9 @@
}
catch { }
}
+
File.WriteAllText(GlobalVar.allLayoutPath, strJson, Encoding.UTF8);
+
return true;
}
catch { return false; }
@@ -518,6 +559,48 @@
}
}
+ private void CamerasChanged(object sender, DictionaryChangedEventArgs<string, BaseCamera> e)
+ {
+ try
+ {
+ switch (e.ChangeType)
+ {
+ case DictionaryChangeType.Added:
+ e.NewValue.TriggerRunMessageReceived += TriggerRunMessageReceived;
+ LogInfo($"鐩告満[{e.NewValue.SN}]鍔犺浇瑙﹀彂閫氳", LogInfoType.INFO);
+ e.NewValue.SN = e.NewKey;
+ break;
+ case DictionaryChangeType.Removed:
+ if (e.OldValue != null && e.OldValue is BaseCommunicator)
+ e.OldValue.CloseDevice();
+ e.OldValue.TriggerRunMessageReceived -= TriggerRunMessageReceived;
+ LogInfo($"鐩告満[{e.OldValue.SN}]绉婚櫎瑙﹀彂閫氳", LogInfoType.INFO);
+ break;
+ }
+ }
+ 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"));
@@ -569,6 +652,21 @@
{
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();
if (LoadAllProcessVars(GlobalVar.allProcessVarsPath))
@@ -579,7 +677,15 @@
{
LogInfo("鍏ㄥ眬鍙橀噺鍔犺浇澶辫触", LogInfoType.ERROR);
}
-
+ GlobalVar.dicMotionControlData.Clear();
+ if (LoadMotionControlDatas(GlobalVar.allMotionControlDataPath))
+ {
+ LogInfo("杩愬姩鎺у埗鍙傛暟鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
+ else
+ {
+ LogInfo("杩愬姩鎺у埗鍙傛暟鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
//鍔犺浇娴佺▼
GlobalVar.dicProcesses.Clear();
if (LoadAllProcess(GlobalVar.allProcessConnectionStringPath))
@@ -633,6 +739,7 @@
com_ProductName.Items.Add("鏂板");
com_ProductName.Text = GlobalVar.strProductName;
this.WindowState = FormWindowState.Maximized;
+ DatabaseRecordProductDataHelper.InitializeDatabase();
}
public void SaveAllSetting()
@@ -641,9 +748,58 @@
SaveAllProcessVars();
SaveAllCommunicators();
SaveAllCameras();
+ SaveAllBarcodeReaders();
SaveAllProcessSetting();
SaveAllLayout();
SaveAllCsv();
+ SaveMotionControlDatas();
+ }
+ public bool LoadMotionControlDatas(string alMotionControlDataPath)
+ {
+ try
+ {
+ GlobalVar.dicMotionControlData = ConfigManager<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>.LoadConfig<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>(alMotionControlDataPath);
+ }
+ catch
+ {
+ return false;
+ }
+ return true;
+ }
+ public bool SaveMotionControlDatas()
+ {
+ try
+ {
+ ConcurrentDictionary<string, List<string>> removeCameraSN = new ConcurrentDictionary<string, List<string>>();
+ foreach (var item in GlobalVar.dicMotionControlData.Keys)
+ {
+ List<string> list = new List<string>();
+ foreach (var itemSN in GlobalVar.dicMotionControlData[item].Keys)
+ {
+ if (!GlobalVar.dicCameras.Keys.Contains(itemSN))
+ {
+ list.Add(itemSN);
+ }
+ }
+ if (list.Count > 0)
+ {
+ removeCameraSN.TryAdd(item, list);
+ }
+ }
+ foreach (var item in removeCameraSN.Keys)
+ {
+ foreach (var itemSN in removeCameraSN[item])
+ {
+ GlobalVar.dicMotionControlData[item].Keys.Remove(itemSN);
+ }
+ }
+ ConfigManager<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>.SaveConfig<ConcurrentDictionary<string, ConcurrentDictionary<string, RecordMotionControlData>>>(GlobalVar.dicMotionControlData, GlobalVar.allMotionControlDataPath);
+ }
+ catch
+ {
+ return false;
+ }
+ return true;
}
public bool LoadAllCameras(string allCamerasConnectionStringPath)
@@ -681,7 +837,7 @@
}
case CameraBrand.LBCamera:
{
- //camera = new LBCamera();
+ camera = new LBCamera();
break;
}
default:
@@ -691,7 +847,7 @@
}
}
camera.SN = CameraConnectionString.Key;
- if (!camera.InitDevice(CameraConnectionString.Key, this.Handle))
+ if (!camera.InitDevice(CameraConnectionString.Key, IntPtr.Zero))
{
LogInfo($"鍒濆鍖栫浉鏈篬{CameraConnectionString.Key}]澶辫触", LogInfoType.ERROR);
if (camera != null)
@@ -750,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)
{
@@ -784,65 +1039,121 @@
{
string CommunicatorName = CommunicatorConnectionString.Key;
string CommunicatorAddress = CommunicatorConnectionString.Value;
-
- // 瀹氫箟姝e垯琛ㄨ揪寮忎互鎻愬彇鍗忚銆両P 鍦板潃鍜岀鍙�
- //1. \((.*?)\)锛歕(鍜� \) 鏄敤浜庡尮閰嶆嫭鍙风殑杞箟瀛楃銆�
- // (.*?) 鏄竴涓潪璐┆鐨勫尮閰嶏紝鐢ㄦ潵鍖归厤绫诲悕锛圡yProcesses.Communicators.TCPServer 鎴� MyProcesses.Communicators.UARTPort锛夈��
- //2. ([^:] +)锛氬尮閰嶅啋鍙蜂箣鍓嶇殑閮ㄥ垎锛屽嵆鍦板潃锛�127.0.0.1 鎴� COM5锛夈�傝繖閲屼娇鐢ㄤ簡[^:] 鏉ュ尮閰嶉櫎浜嗗啋鍙蜂箣澶栫殑浠绘剰瀛楃銆�
- //3. (\d +) 锛氬尮閰嶇鍙e彿锛岀‘淇濆畠鍖归厤涓�涓垨澶氫釜鏁板瓧銆�
-
- string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<PORT>[^]]+)\]$";
- Match match = Regex.Match(CommunicatorAddress, pattern);
-
- if (match.Success)
+ if (!string.IsNullOrEmpty(CommunicatorAddress) && CommunicatorAddress.Contains("SiemensLBS7"))
{
- string ClassName = match.Groups["ClassName"].Value; // "TCP"
- string IP = match.Groups["IP"].Value; // "127.0.0.1"
- string PORT = match.Groups["PORT"].Value; // "1111"
+ // 瀹氫箟姝e垯琛ㄨ揪寮忎互鎻愬彇鍗忚銆両P 鍦板潃鍜岀鍙�
+ // 鏇存柊姝e垯浠ユ敮鎸佸彲閫夌殑鏁版嵁绫诲瀷瀛楁
+ string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<Slot>[^]]+)\]\[(?<CpuType>[^]]+)\]\[(?<PlcAddress>[^]]+)\](?:\[(?<DataType>[^]]+)\])?$";
+ Match match = Regex.Match(CommunicatorAddress, pattern);
- if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
- break;
-
- //鍒╃敤鍙嶅皠鍒涘缓瀹炰緥
- Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
- if (type == null)
+ if (match.Success)
{
- Debug.WriteLine("Class not found.");
- return false;
- }
- var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator;
+ string ClassName = match.Groups["ClassName"].Value; // "TCP"
+ string IP = match.Groups["IP"].Value; // "127.0.0.1"
+ string Slot = match.Groups["Slot"].Value; // "1111"
+ string CpuType = match.Groups["CpuType"].Value;
+ string PlcAddress = match.Groups["PlcAddress"].Value;
+ string DataType = match.Groups["DataType"].Success ? match.Groups["DataType"].Value : "String";
- if (Communicator == null)
- {
- Debug.WriteLine("BaseCommunicator not found.");
- return false;
- }
+ if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(Slot) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress))
+ break;
- //TCP瀹㈡埛绔渶鍚庡啀杩炴帴
- if (Communicator is TCPClient)
- {
- clientsCommunicatorsConnectionString.TryAdd(CommunicatorConnectionString.Key, CommunicatorConnectionString.Value);
- continue;
- }
+ //鍒╃敤鍙嶅皠鍒涘缓瀹炰緥
+ Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
+ if (type == null)
+ {
+ Debug.WriteLine("Class not found.");
+ return false;
+ }
+ var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator;
- Communicator.CommunicatorConnections.Add("鍦板潃", IP);
- Communicator.CommunicatorConnections.Add("绔彛", PORT);
- Communicator.CommunicatorName = CommunicatorName;
- if (!Communicator.Connect())
- {
- LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]澶辫触,鍘熷洜鏄瘂Communicator.Msg}", LogInfoType.ERROR);
+ if (Communicator == null)
+ {
+ Debug.WriteLine("BaseCommunicator not found.");
+ return false;
+ }
+
+ Communicator.CommunicatorConnections.Add("鍦板潃", IP);
+ Communicator.CommunicatorConnections.Add("绔彛", Slot);
+ Communicator.CommunicatorConnections.Add("鍨嬪彿", CpuType);
+ Communicator.CommunicatorConnections.Add("鍙橀噺鍦板潃", PlcAddress);
+ Communicator.CommunicatorConnections.Add("鏁版嵁绫诲瀷", DataType);
+ Communicator.CommunicatorName = CommunicatorName;
+ if (!Communicator.Connect())
+ {
+ LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]澶辫触,鍘熷洜鏄瘂Communicator.Msg}", LogInfoType.ERROR);
+ }
+ else
+ {
+ LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]鎴愬姛", LogInfoType.PASS);
+ }
+ GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
}
else
{
- LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]鎴愬姛", LogInfoType.PASS);
+ Debug.WriteLine("No match found.");
}
- GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
}
else
{
- Debug.WriteLine("No match found.");
- }
+ // 瀹氫箟姝e垯琛ㄨ揪寮忎互鎻愬彇鍗忚銆両P 鍦板潃鍜岀鍙�
+ //1. \((.*?)\)锛歕(鍜� \) 鏄敤浜庡尮閰嶆嫭鍙风殑杞箟瀛楃銆�
+ // (.*?) 鏄竴涓潪璐┆鐨勫尮閰嶏紝鐢ㄦ潵鍖归厤绫诲悕锛圡yProcesses.Communicators.TCPServer 鎴� MyProcesses.Communicators.UARTPort锛夈��
+ //2. ([^:] +)锛氬尮閰嶅啋鍙蜂箣鍓嶇殑閮ㄥ垎锛屽嵆鍦板潃锛�127.0.0.1 鎴� COM5锛夈�傝繖閲屼娇鐢ㄤ簡[^:] 鏉ュ尮閰嶉櫎浜嗗啋鍙蜂箣澶栫殑浠绘剰瀛楃銆�
+ //3. (\d +) 锛氬尮閰嶇鍙e彿锛岀‘淇濆畠鍖归厤涓�涓垨澶氫釜鏁板瓧銆�
+ string pattern = @"^\((?<ClassName>[^)]+)\)\[(?<IP>[^]]+)\]\[(?<PORT>[^]]+)\]$";
+ Match match = Regex.Match(CommunicatorAddress, pattern);
+
+ if (match.Success)
+ {
+ string ClassName = match.Groups["ClassName"].Value; // "TCP"
+ string IP = match.Groups["IP"].Value; // "127.0.0.1"
+ string PORT = match.Groups["PORT"].Value; // "1111"
+
+ if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
+ break;
+
+ //鍒╃敤鍙嶅皠鍒涘缓瀹炰緥
+ Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
+ if (type == null)
+ {
+ Debug.WriteLine("Class not found.");
+ return false;
+ }
+ var Communicator = Activator.CreateInstance(type, CommunicatorName) as BaseCommunicator;
+
+ if (Communicator == null)
+ {
+ Debug.WriteLine("BaseCommunicator not found.");
+ return false;
+ }
+
+ //TCP瀹㈡埛绔渶鍚庡啀杩炴帴
+ if (Communicator is TCPClient)
+ {
+ clientsCommunicatorsConnectionString.TryAdd(CommunicatorConnectionString.Key, CommunicatorConnectionString.Value);
+ continue;
+ }
+
+ Communicator.CommunicatorConnections.Add("鍦板潃", IP);
+ Communicator.CommunicatorConnections.Add("绔彛", PORT);
+ Communicator.CommunicatorName = CommunicatorName;
+ if (!Communicator.Connect())
+ {
+ LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]澶辫触,鍘熷洜鏄瘂Communicator.Msg}", LogInfoType.ERROR);
+ }
+ else
+ {
+ LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]鎴愬姛", LogInfoType.PASS);
+ }
+ GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
+ }
+ else
+ {
+ Debug.WriteLine("No match found.");
+ }
+ }
}
//TCP瀹㈡埛绔渶鍚庤繛鎺�
@@ -917,19 +1228,35 @@
{
string strJson = string.Empty;
GlobalVar.allCommunicatorsConnectionString = new ConcurrentDictionary<string, string>();
-
foreach (var item in GlobalVar.dicCommunicators)
{
string ClassName = item.Value.GetType().FullName;// "TCP"
string IP = item.Value.CommunicatorConnections["鍦板潃"].ToString();//"127.0.0.1"
string PORT = item.Value.CommunicatorConnections["绔彛"].ToString();//"1111"
-
- if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
+ if (!string.IsNullOrEmpty(ClassName) && ClassName.Contains("SiemensLBS7"))
{
- break;
+ string CpuType = item.Value.CommunicatorConnections["鍨嬪彿"].ToString();
+ string PlcAddress = item.Value.CommunicatorConnections["鍙橀噺鍦板潃"].ToString();
+ string DataType = item.Value.CommunicatorConnections.Contains("鏁版嵁绫诲瀷") ?
+ item.Value.CommunicatorConnections["鏁版嵁绫诲瀷"].ToString() : "String";
+
+ if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT) || string.IsNullOrEmpty(CpuType) || string.IsNullOrEmpty(PlcAddress))
+ {
+ break;
+ }
+ string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}][{CpuType}][{PlcAddress}][{DataType}]";
+ GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
}
- string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]";
- GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
+ else
+ {
+ if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
+ {
+ break;
+ }
+ string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]";
+ GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
+ }
+ GlobalVar.dicCommunicators[item.Key].ClassName = ClassName;
}
var settings = new JsonSerializerSettings
{
@@ -954,6 +1281,11 @@
{ }
}
File.WriteAllText(GlobalVar.allCommunicatorsConnectionStringPath, strJson, Encoding.UTF8);
+ foreach (var item in GlobalVar.dicCommunicators)
+ {
+ string ClassName = item.Value.GetType().FullName;// "TCP"
+ }
+ ConfigManager<ObservableConcurrentDictionary<string, BaseCommunicator>>.SaveConfig<ObservableConcurrentDictionary<string, BaseCommunicator>>(GlobalVar.dicCommunicators, GlobalVar.strApplicationPath + "\\鎵�鏈変骇鍝乗\" + GlobalVar.strProductName + "\\dicCommunicators.json");
return true;
}
catch { return false; }
@@ -1213,6 +1545,7 @@
GlobalVar.strProductName = com_ProductName.SelectedItem?.ToString();
foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
{
+ camera.TriggerRunMessageReceived -= TriggerRunMessageReceived;
camera.Dispose();
}
GlobalVar.dicCameras.Clear();
@@ -1437,8 +1770,8 @@
catch { }
}
// 鐢熸垚鍥剧墖骞舵樉绀哄埌鎺т欢涓�
- HImage InputImage = null;
- HImage RecordImage = null;
+ HObject InputImage = null;
+ HObject RecordImage = null;
foreach (var layout in GlobalVar.dicLayout.Values
.Where(layout => layout.ProcessName == ProcessName)
@@ -1526,6 +1859,128 @@
communicator.Disconnect();
}
FormClosing -= VisionForm_FormClosing;
+
+ //try
+ //{
+ // Process[] processes = System.Diagnostics.Process.GetProcesses(); //鑾峰緱鎵�鏈夎繘绋�
+ // foreach (Process p in processes)
+ // {
+ // if (p.ProcessName == "LB_SmartVision" && p.StartTime < DateTime.Now.AddMilliseconds(-300))
+ // {
+ // p.Kill();
+ // }
+ // }
+ //}
+ //catch { }
+
+ KillAllTargetProcesses();
+ }
+
+ /// <summary>
+ /// 鏉�杩涚▼閫昏緫
+ /// </summary>
+ private void KillAllTargetProcesses()
+ {
+ try
+ {
+ // 1. 鑾峰彇褰撳墠杩涚▼ID锛岄伩鍏嶆潃姝昏嚜宸�
+ int currentProcessId = Process.GetCurrentProcess().Id;
+ // 2. 妯$硦鍖归厤杩涚▼鍚嶏紙蹇界暐澶у皬鍐欙級锛岃鐩杤shost/鍚庣紑绛夋儏鍐�
+ var targetProcesses = Process.GetProcesses()
+ .Where(p =>
+ p.ProcessName.IndexOf("LB_SmartVision", StringComparison.OrdinalIgnoreCase) >= 0
+ && p.Id == currentProcessId);
+
+ foreach (var p in targetProcesses)
+ {
+ try
+ {
+ if (!p.HasExited)
+ {
+ // 鍏堝皾璇曚紭闆呭叧闂璚inForm绋嬪簭锛堟瘮鐩存帴Kill鏇村弸濂斤級
+ p.CloseMainWindow();
+ // 绛夊緟500ms锛岀湅鏄惁姝e父閫�鍑�
+ if (!p.WaitForExit(500))
+ {
+ p.Kill(); // 寮哄埗鏉�姝�
+ p.WaitForExit(1000); // 绛夊緟杩涚▼瀹屽叏缁堟
+ }
+ }
+ p.Dispose(); // 閲婃斁杩涚▼璧勬簮锛岄伩鍏嶅彞鏌勬硠婕�
+ }
+ catch (Exception ex)
+ {
+ // 璁板綍寮傚父浣嗕笉涓柇锛屾瘮濡傛潈闄愪笉瓒崇殑鎯呭喌
+ System.IO.File.AppendAllText("kill_process_log.txt", $"[{DateTime.Now}] 鏉�姝昏繘绋嬪け璐�: {ex.Message}\r\n");
+ }
+ }
+
+ // 棰濆寤惰繜锛岀‘淇濇枃浠跺彞鏌勫畬鍏ㄩ噴鏀�
+ Thread.Sleep(1000);
+ }
+ catch (Exception)
+ {
+ // 闈欓粯澶辫触锛屼笉褰卞搷绋嬪簭閫�鍑�
+ }
+ }
+
+ private void btn_Run_Click(object sender, EventArgs e)
+ {
+ var matchedItems = GlobalVar.dicProcessSetting
+ .Where(item =>
+ {
+ var value = item.Value;
+ var triggerComm = value["瑙﹀彂閫氳"];
+ var triggerChar = value["瑙﹀彂瀛楃"];
+ return triggerComm != null;
+ //return triggerComm != null && triggerComm.Equals(name) &&
+ // (string.IsNullOrEmpty(triggerChar?.ToString()) ||
+ // msg.StartsWith(triggerChar.ToString()));
+ })
+ .ToList(); // 閬垮厤閲嶅瀛楀吀璁块棶鍜岃绠辨搷浣�
+ if (matchedItems.Count <= 0)
+ {
+ return;
+ }
+ TriggerRunMessageReceived(matchedItems[0].Value["瑙﹀彂閫氳"], matchedItems[0].Value["瑙﹀彂瀛楃"]);
+ }
+
+ private void btn_RunContinuously_Click(object sender, EventArgs e)
+ {
+ if (btn_RunContinuously.Text.Equals("杩炵画杩愯"))
+ {
+ btn_RunContinuously.Text = "鏆傚仠杩愯";
+ Task.Factory.StartNew(() =>
+ {
+ var matchedItems = GlobalVar.dicProcessSetting
+ .Where(item =>
+ {
+ var value = item.Value;
+ var triggerComm = value["瑙﹀彂閫氳"];
+ var triggerChar = value["瑙﹀彂瀛楃"];
+ return triggerComm != null;
+ //return triggerComm != null && triggerComm.Equals(name) &&
+ // (string.IsNullOrEmpty(triggerChar?.ToString()) ||
+ // msg.StartsWith(triggerChar.ToString()));
+ })
+ .ToList(); // 閬垮厤閲嶅瀛楀吀璁块棶鍜岃绠辨搷浣�
+ while (btn_RunContinuously.Text.Equals("鏆傚仠杩愯"))
+ {
+
+ TriggerRunMessageReceived(matchedItems[0].Value["瑙﹀彂閫氳"], matchedItems[0].Value["瑙﹀彂瀛楃"]);
+ Thread.Sleep(500);
+ }
+ //this.Invoke(() =>
+ //{
+ //});
+ });
+ }
+ else if (btn_RunContinuously.Text.Equals("鏆傚仠杩愯"))
+ {
+ btn_RunContinuously.Text = "杩炵画杩愯";
+ Thread.Sleep(100);
+ }
+
}
}
}
--
Gitblit v1.9.3