C3032
2025-12-20 38c333f8091e5062be0bbdd81960315ebce69008
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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; // 是否正在重连
 
        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;
            }
        }
    }
}