From d0c990eaf6cfdbf77c1f4f8db272f4964ec43310 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期四, 15 一月 2026 15:58:15 +0800
Subject: [PATCH] 优化西门子S7通讯:支持多数据类型、UI布局调整及配置持久化修复   详细说明:    1. 通讯核心 (SiemensLBS7)        * 多类型支持:完善 SendMessage 和 ReceiveMsg,支持 Bool, Byte, Int, DInt, Real, Double, Word, DWord, String的读写。        * Bool 解析优化:增强布尔值解析逻辑,支持字符串 "1"/"0" 及 "True"/"False"(不区分大小写)。        * 错误反馈:捕获数据转换异常(如格式错误),将具体错误信息写入 Msg 属性,便于 UI 展示。

---
 LB_SmartVision/Program.cs |   77 ++++++++++++++++++++++++++++++++++++--
 1 files changed, 73 insertions(+), 4 deletions(-)

diff --git a/LB_SmartVision/Program.cs b/LB_SmartVision/Program.cs
index e0369dd..d27db66 100644
--- a/LB_SmartVision/Program.cs
+++ b/LB_SmartVision/Program.cs
@@ -1,3 +1,6 @@
+using LB_SmartVisionLoginUI;
+using System.Text;
+
 namespace LB_SmartVision
 {
     internal static class Program
@@ -8,10 +11,76 @@
         [STAThread]
         static void Main()
         {
-            // To customize application configuration such as set high DPI settings or default font,
-            // see https://aka.ms/applicationconfiguration.
-            ApplicationConfiguration.Initialize();
-            Application.Run(new VisionForm());
+            try
+            {
+                string halconDir = Environment.GetEnvironmentVariable("HALCONROOT");
+                if (string.IsNullOrEmpty(halconDir) || !Directory.Exists(halconDir))
+                {
+                    System.Windows.Forms.MessageBox.Show("环境变量缺少HALCONROOT,请确认是否安装正确!");
+                }
+                else
+                {
+                    bool ret;
+                    System.Threading.Mutex mutex = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out ret);
+                    if (ret)
+                    {
+                        ////登录验证权限
+                        MainWindow.InstanceLoginandConfirmation().ShowDialog();
+                        if (!MainWindow.InstanceLoginandConfirmation().isQuit && MainWindow.InstanceLoginandConfirmation().correctUser)
+                        {
+                            MainWindow.InstanceLoginandConfirmation().closeLoginFrm();
+                        }
+                        else
+                        {
+                            MainWindow.InstanceLoginandConfirmation().closeLoginFrm();
+                        }
+                        ////   Main   为你程序的主窗体,如果是控制台程序不用这句   
+                        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
+                        System.Windows.Forms.Application.EnableVisualStyles();
+                        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
+                        System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
+                        System.Windows.Forms.Application.ThreadException += (sender, e) => HandleException(e.Exception);
+                        AppDomain.CurrentDomain.UnhandledException += (sender, e) => HandleException(e.ExceptionObject as Exception);
+                        //// To customize application configuration such as set high DPI settings or default font,
+                        //// see https://aka.ms/applicationconfiguration.
+                        System.Windows.Forms.Application.Run(new VisionForm());
+                        mutex.ReleaseMutex();
+                    }
+                    else
+                    {
+                        System.Windows.Forms.MessageBox.Show("有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\r\n这个程序即将退出。", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                        //   提示信息,可以删除。   
+                        System.Windows.Forms.Application.Exit();//退出程序   
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                if (null != ex)
+                {
+                    string strMsg = "InitControl failed. Error Code: " + ex.Message;
+                    System.Windows.Forms.MessageBox.Show(strMsg);
+                }
+                else
+                {
+                    return;
+                }
+            }
+
+        }
+
+        static void HandleException(Exception ex)
+        {
+            // 记录异常信息
+            string logPath = @"\Logs\app_crash.log";
+            Directory.CreateDirectory(Path.GetDirectoryName(logPath));
+            File.AppendAllText(logPath, $"{DateTime.Now}: {ex}\n");
+
+            // 显示错误信息(可选)
+            System.Windows.Forms.MessageBox.Show($"程序发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
+
+            // 退出程序
+            Environment.Exit(1);
         }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3