From 8d4774964cf674a9a3ff1049b255d829248e0ad8 Mon Sep 17 00:00:00 2001 From: gebo Date: Wed, 24 Dec 2025 14:46:43 +0800 Subject: [PATCH] add libcomm for plc layer --- LibComm/CommWithCC24.cs | 73 ++++++++++++++++++++++++++++++ LibComm/ICommPLC.cs | 22 +++++++++ LibComm/LibComm.csproj | 56 +++++++++++++++++++++++ LibComm/Properties/AssemblyInfo.cs | 33 ++++++++++++++ TetraPackOCR/TetraPackOCR.csproj | 4 ++ TetraPark.OCR.sln | 11 +++++ 6 files changed, 199 insertions(+) create mode 100644 LibComm/CommWithCC24.cs create mode 100644 LibComm/ICommPLC.cs create mode 100644 LibComm/LibComm.csproj create mode 100644 LibComm/Properties/AssemblyInfo.cs diff --git a/LibComm/CommWithCC24.cs b/LibComm/CommWithCC24.cs new file mode 100644 index 0000000..d7ac619 --- /dev/null +++ b/LibComm/CommWithCC24.cs @@ -0,0 +1,73 @@ +using Bjcve.Comm.FFP; +using Cognex.VisionPro.Comm; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LibComm +{ + public class CommWithCC24 : ICommPLC + { + private CC24 cc24; + public event OnDataReceived OnDataReceived; + public event OnConnectStatus OnConnectStatus; + public event OnTrigCamera OnTrigCamera; + public event OnCameraStatus OnCameraStatus; + public void Initialize() + { + cc24 = new CC24(); + + cc24.VisionReceivedNewUserData += CC24_NewUserDataReceived; + cc24.PlcConnectionStatusChanged += CC24_PlcConnectionStatusChanged; + cc24.PlcTriggerCamAcqStart += CC24_PlcTriggerCamAcqStart; + cc24.PlcTriggerCamAcqStop += CC24_PlcTriggerCamAcqStop; + cc24.NotifyCamAcqEnabled += CC24_NotifyCamAcqEnabled; + cc24.NotifyCamAcqDisabled += CC24_NotifyCamAcqDisabled; + + cc24.Initialize(); + cc24.NotifyCamAcqEnable(0); + cc24.NotifyCamAcqEnable(1); + } + public void Stop() + { + cc24.VisionReceivedNewUserData -= CC24_NewUserDataReceived; + cc24.PlcConnectionStatusChanged -= CC24_PlcConnectionStatusChanged; + cc24.PlcTriggerCamAcqStart -= CC24_PlcTriggerCamAcqStart; + cc24.PlcTriggerCamAcqStop -= CC24_PlcTriggerCamAcqStop; + cc24.NotifyCamAcqEnabled -= CC24_NotifyCamAcqEnabled; + cc24.NotifyCamAcqDisabled -= CC24_NotifyCamAcqDisabled; + cc24.Shutdown(); + } + public void NoticeCamComplete(int index, byte[] datax) + { + cc24?.NotifyCamInspectionComplete(index, datax); + cc24?.NotifyCamAcqComplete(index); + } + private void CC24_NewUserDataReceived(object sender, CogNdmNewUserDataEventArgs e) + { + OnDataReceived?.Invoke(cc24.ReadBytesFromPLC(0, 4)); + } + private void CC24_PlcConnectionStatusChanged(object sender, CogNdmProtocolStatusChangedEventArgs e) + { + OnConnectStatus?.Invoke(e.ProtocolStatus == CogNdmConnectionStatusConstants.Connected); + } + private void CC24_PlcTriggerCamAcqStart(object sender, CogNdmTriggerAcquisitionEventArgs e) + { + OnTrigCamera?.Invoke(e.CameraIndex); + } + private void CC24_PlcTriggerCamAcqStop(object sender, CogNdmTriggerAcquisitionStopEventArgs e) + { + + } + private void CC24_NotifyCamAcqEnabled(int cameraIndex, bool isEnabled) + { + OnCameraStatus?.Invoke(cameraIndex, isEnabled); + } + private void CC24_NotifyCamAcqDisabled(int cameraIndex, bool isEnabled) + { + OnCameraStatus?.Invoke(cameraIndex, isEnabled); + } + } +} diff --git a/LibComm/ICommPLC.cs b/LibComm/ICommPLC.cs new file mode 100644 index 0000000..0a244f0 --- /dev/null +++ b/LibComm/ICommPLC.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LibComm +{ + public delegate void OnDataReceived(byte[] data); + public delegate void OnConnectStatus(bool connected); + public delegate void OnTrigCamera(int index); + public delegate void OnCameraStatus(int index, bool enable); + public interface ICommPLC + { + event OnDataReceived OnDataReceived; + event OnConnectStatus OnConnectStatus; + event OnTrigCamera OnTrigCamera; + void Initialize(); + void Stop(); + void NoticeCamComplete(int index, byte[] datax); + } +} diff --git a/LibComm/LibComm.csproj b/LibComm/LibComm.csproj new file mode 100644 index 0000000..c5719f6 --- /dev/null +++ b/LibComm/LibComm.csproj @@ -0,0 +1,56 @@ + + + + + Debug + AnyCPU + {E44B1774-093E-4617-AF7A-474A88B89100} + Library + Properties + LibComm + LibComm + v4.7.2 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\dll\Bjcve.Comm.FFP.dll + + + False + ..\dll\Cognex.VisionPro.Comm.dll + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LibComm/Properties/AssemblyInfo.cs b/LibComm/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e636582 --- /dev/null +++ b/LibComm/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("LibComm")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("HP")] +[assembly: AssemblyProduct("LibComm")] +[assembly: AssemblyCopyright("Copyright © HP 2025")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("e44b1774-093e-4617-af7a-474a88b89100")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TetraPackOCR/TetraPackOCR.csproj b/TetraPackOCR/TetraPackOCR.csproj index 2a94f03..3cf7a94 100644 --- a/TetraPackOCR/TetraPackOCR.csproj +++ b/TetraPackOCR/TetraPackOCR.csproj @@ -265,6 +265,10 @@ + + {e44b1774-093e-4617-af7a-474a88b89100} + LibComm + {d03a85cc-a53b-4434-a560-7a89563292e8} LibDataBase diff --git a/TetraPark.OCR.sln b/TetraPark.OCR.sln index d51b075..078d8dc 100644 --- a/TetraPark.OCR.sln +++ b/TetraPark.OCR.sln @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibDataBase", "LibDataBase\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestPackOCR", "TestPackOCR\TestPackOCR.csproj", "{717BA61B-8C31-473F-8A02-626337A90A5E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibComm", "LibComm\LibComm.csproj", "{E44B1774-093E-4617-AF7A-474A88B89100}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,14 @@ Global {717BA61B-8C31-473F-8A02-626337A90A5E}.Release|Any CPU.Build.0 = Release|Any CPU {717BA61B-8C31-473F-8A02-626337A90A5E}.Release|x64.ActiveCfg = Release|Any CPU {717BA61B-8C31-473F-8A02-626337A90A5E}.Release|x64.Build.0 = Release|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Debug|x64.ActiveCfg = Debug|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Debug|x64.Build.0 = Debug|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Release|Any CPU.Build.0 = Release|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Release|x64.ActiveCfg = Release|Any CPU + {E44B1774-093E-4617-AF7A-474A88B89100}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -70,6 +80,7 @@ Global GlobalSection(NestedProjects) = preSolution {B88B2FA9-0D9D-4EBB-A87A-4DE2DC2DD70F} = {C0079401-F420-4256-B991-3BF16D3296C7} {D03A85CC-A53B-4434-A560-7A89563292E8} = {C0079401-F420-4256-B991-3BF16D3296C7} + {E44B1774-093E-4617-AF7A-474A88B89100} = {C0079401-F420-4256-B991-3BF16D3296C7} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F550CA1D-FC09-446C-B501-170B642AAC00}