From d0ded5cd9bf5070a120bad58b5be21fe2ac6a4ff Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期六, 20 十二月 2025 16:41:09 +0800
Subject: [PATCH] test

---
 IDViewer_2D/ModbusTCPClient.cs |  197 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 197 insertions(+), 0 deletions(-)

diff --git a/IDViewer_2D/ModbusTCPClient.cs b/IDViewer_2D/ModbusTCPClient.cs
new file mode 100644
index 0000000..b4f5fdc
--- /dev/null
+++ b/IDViewer_2D/ModbusTCPClient.cs
@@ -0,0 +1,197 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows;
+using NModbus;
+using NModbus.Device;
+using NModbus.Extensions.Enron;
+using OpenCvSharp;
+
+namespace SmartScanner
+{
+    class ModbusTCPClient
+    {
+        public TcpClient _client;
+        public IModbusMaster _master;
+        public ModbusFactory factory = new ModbusFactory();
+        public int isconnnum = 1;
+        public ushort[] previousValus_trigger = { 0, 0 };   //瀵勫瓨鍣ㄥ綋鍓嶅��
+        public ushort[] currentValus_trigger = { 0, 0 };    //瀵勫瓨鍣ㄥ墠涓�涓��
+        public ushort[] previousValus_ROIModel = { 0, 0 };
+        public ushort[] currentValus_ROIModel = { 0, 0 };
+        public bool istrigger = false;
+        
+        // 閲嶈繛鐩稿叧瀛楁
+        public const int MaxReconnectAttempts = 5; // 鏈�澶ч噸杩炴鏁�
+        public const int ReconnectInterval = 5000; // 閲嶈繛闂撮殧鏃堕棿(姣)
+        private int _reconnectAttempts = 0; // 褰撳墠閲嶈繛灏濊瘯娆℃暟
+        private bool _isReconnecting = false; // 鏄惁姝e湪閲嶈繛
+
+        public void ModbudTCP_Connect(string ipAddress, int port)
+        {
+            try
+            {
+                _client = new TcpClient(ipAddress, port);
+                if (_client.Connected)
+                {
+                    // 鍒涘缓 TCP 瀹㈡埛绔�
+                    //MessageBox.Show("杩炴帴鍒版湇鍔″櫒鎴愬姛");
+                    // 鍒涘缓 Modbus 宸ュ巶鍜屼富鏈�
+                    _master = (ModbusMaster)factory.CreateMaster(_client);
+                    if (_master == null)
+                    {
+                        MessageBox.Show("鏃犳硶鍒涘缓 Modbus master 瀹炰緥銆�");
+                        return;
+                    }
+                    // 杩炴帴鎴愬姛锛岄噸缃噸杩炶鏁�
+                    _reconnectAttempts = 0;
+                }
+                else
+                {
+                    if (isconnnum++ < 5)
+                    {
+                        ModbudTCP_Connect(ipAddress, port);
+                        Thread.Sleep(1000);
+                    }
+                    else
+                    {
+                        MessageBox.Show("杩炴帴鍒版湇鍔″櫒瓒呮椂");
+                        return;
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show($"杩炴帴鍒版湇鍔″櫒澶辫触锛歿ex.Message}", "閿欒", MessageBoxButton.OK, MessageBoxImage.Error);
+                Console.WriteLine("杩炴帴鍒版湇鍔″櫒澶辫触");
+                return;
+            }
+        }
+
+        // 閲嶈繛鏂规硶
+        public bool Reconnect(string ipAddress, int port)
+        {
+            // 閬垮厤閲嶅閲嶈繛
+            if (_isReconnecting) return false;
+            
+            _isReconnecting = true;
+            
+            try
+            {
+                // 閲婃斁鍘熸湁杩炴帴
+                _master?.Dispose();
+                _client?.Close();
+                _client = null;
+                
+                // 妫�鏌ユ槸鍚﹁秴杩囨渶澶ч噸杩炴鏁�
+                if (_reconnectAttempts >= MaxReconnectAttempts)
+                {
+                    Console.WriteLine("Modbus閲嶈繛澶辫触锛屽凡杈惧埌鏈�澶ч噸杩炴鏁�");
+                    return false;
+                }
+                
+                _reconnectAttempts++;
+                Console.WriteLine($"灏濊瘯绗瑊_reconnectAttempts}娆¢噸杩�...");
+                
+                // 灏濊瘯閲嶆柊杩炴帴
+                _client = new TcpClient();
+                var connectTask = _client.ConnectAsync(ipAddress, port);
+                
+                // 绛夊緟杩炴帴瀹屾垚锛屾渶澶氱瓑寰�5绉�
+                if (connectTask.Wait(5000))
+                {
+                    if (_client.Connected)
+                    {
+                        _master = (ModbusMaster)factory.CreateMaster(_client);
+                        if (_master != null)
+                        {
+                            // 閲嶈繛鎴愬姛锛岄噸缃鏁�
+                            _reconnectAttempts = 0;
+                            Console.WriteLine("Modbus閲嶈繛鎴愬姛");
+                            return true;
+                        }
+                    }
+                }
+                
+                return false;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"閲嶈繛澶辫触: {ex.Message}");
+                return false;
+            }
+            finally
+            {
+                _isReconnecting = false;
+            }
+        }
+
+        public ushort[] HoldingRegisterRead(ushort starAddress) //璇讳繚鎸佸瘎瀛樺櫒
+        {
+            try
+            {
+                ushort[] registerValues = _master.ReadHoldingRegisters(1, starAddress, 1);
+                return registerValues;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"璇诲彇淇濇寔瀵勫瓨鍣ㄥけ璐�: {ex.Message}");
+                throw;
+            }
+        }
+
+        public void HoldingRegisterWrite(ushort registerAddress, ushort WriteValue) //鍐欎繚瀛樺瘎瀛樺櫒
+        {
+            try
+            {
+                _master.WriteSingleRegister(1, registerAddress, WriteValue);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"鍐欏叆淇濇寔瀵勫瓨鍣ㄥけ璐�: {ex.Message}");
+                throw;
+            }
+        }
+        public void SingleCoilWrite(ushort CoilAddress, bool valueToWrite) //鍐欏崟绾垮湀
+        {
+            try
+            {
+                _master.WriteSingleCoil(1, CoilAddress, valueToWrite);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"鍐欏叆鍗曠嚎鍦堝け璐�: {ex.Message}");
+                throw;
+            }
+        }
+        public void MultipleRegistersWrite(ushort starAddress, ushort[] valueToWrite)
+        {
+            try
+            {
+                _master.WriteMultipleRegisters(1, starAddress, valueToWrite);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"鍐欏叆澶氫釜瀵勫瓨鍣ㄥけ璐�: {ex.Message}");
+                throw;
+            }
+        }
+        public ushort[] InputRegistersRead(ushort starAddress)
+        {
+            try
+            {
+                ushort[] registerValues = _master.ReadInputRegisters(1, starAddress, 1);
+                return registerValues;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"璇诲彇杈撳叆瀵勫瓨鍣ㄥけ璐�: {ex.Message}");
+                throw;
+            }
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3