海波 张
7 天以前 b67787b031e357c60565d3e1aa8b829706e520e2
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using EasyModbus;
using HH.WCS.ZhongCeJinTan.util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using static HH.WCS.ZhongCeJinTan.device.TcpServer;
 
namespace HH.WCS.ZhongCeJinTan.device
{
    internal class ModbusHelper
    {
        private static Dictionary<string, ModbusClient> clients = new Dictionary<string, ModbusClient>();
 
        /// <summary>
        /// 读线圈
        /// </summary>
        /// <param name="address"></param>
        /// <param name="qty"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal bool[] ReadCoils(int address, int qty, string ip, int port = 502) {
            bool[] res = new bool[0];
            LogHelper.Info($"ReadCoils:SendMsg:Addr:{address},Qty:{qty},Ip:{ip},Port:{port}", "ModBus");
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                client.ConnectionTimeout = 3500;
                try
                {
                    res = client.ReadCoils(address, qty);
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
            }
            LogHelper.Info($"ReadCoils:ReqMsg:{JsonConvert.SerializeObject(res)},Addr:{address},Ip:{ip},Port:{port}", "ModBus");
            return res;
        }
        /// <summary>
        /// 读离散输入
        /// </summary>
        /// <param name="address"></param>
        /// <param name="qty"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal bool[] ReadDiscreteInputs(int address, int qty, string ip, int port = 502) {
            bool[] res = new bool[0];
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
                    res = client.ReadDiscreteInputs(address, qty);
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
 
        }
 
        static object ReadHoldingRegistersLock = new object();
        /// <summary>
        /// 读保持寄存器
        /// </summary>
        /// <param name="address"></param>
        /// <param name="qty"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static int[] ReadHoldingRegisters(int address, int qty) {
            int[] res = new int[0];
            lock (ReadHoldingRegistersLock)
            {
                LogHelper.Info($"ReadHoldingRegisters:SendMsg:Addr:{address},Qty:{qty},Ip:{TmIp},Port:{TmPort}", "ModBus");
 
                var client = GetClient(TmIp, TmPort);
                if (client != null && client.Connected)
                {
                    client.ConnectionTimeout = 5000;
                    try
                    {
                        res = client.ReadHoldingRegisters(address, qty);
                        client.Disconnect();
                    }
                    catch (Exception ex)
                    {
                        //LogHelper.Error(ex.Message, ex);
                        LogHelper.Info($"ReadHoldingRegisters:Error:{ex.Message}");
                    }
                }
                LogHelper.Info($"ReadHoldingRegisters:ReqMsg:{JsonConvert.SerializeObject(res)},Addr:{address},Ip:{TmIp},Port:{TmPort}", "ModBus");
            }
            return res;
        }
        /// <summary>
        /// 读输入寄存器
        /// </summary>
        /// <param name="address"></param>
        /// <param name="qty"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        internal int[] ReadInputRegisters(int address, int qty, string ip, int port = 502) {
            int[] res = new int[0];
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
                    res = client.ReadInputRegisters(address, qty);
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
 
        }
        /// <summary>
        /// 写单个线圈
        /// </summary>
        /// <param name="address"></param>
        /// <param name="value"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal bool WriteSingleCoil(int address, bool value, string ip, int port = 502) {
            var res = false;
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
                    client.WriteSingleCoil(address, value);
                    res = true;
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
 
        }
        /// <summary>
        /// 写多个线圈
        /// </summary>
        /// <param name="address"></param>
        /// <param name="values"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal bool WriteMultipleCoils(int address, bool[] values, string ip, int port = 502) {
            var res = false;
            LogHelper.Info($"WriteMultipleCoils:SendMsg:Addr:{address},Values:{values},Ip:{ip},Port:{port}","ModBus");
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                client.ConnectionTimeout = 3500;
                try {
                    client.WriteMultipleCoils(address, values);
                    res = true;
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
            }
            LogHelper.Info($"WriteMultipleCoils:ReqMsg:{JsonConvert.SerializeObject(res)},Addr:{address},Values:{values},Ip:{ip},Port:{port}", "ModBus");
            return res;
        }
 
 
        static string TmIp = Settings.TmDeviceIp;
        static int TmPort = Settings.TmDevicePort;
 
 
        /// <summary>
        /// 写单个寄存器
        /// </summary>
        /// <param name="address"></param>
        /// <param name="value"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal static bool WriteSingleRegister(int address, int value) {
            var res = false;
            var client = GetClient(TmIp, TmPort);
            if (client != null && client.Connected) {
                try {
                    client.WriteSingleRegister(address, value);
                    res = true;
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
        }
        /// <summary>
        /// 写多个寄存器
        /// </summary>
        /// <param name="address"></param>
        /// <param name="values"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        internal bool WriteMultipleRegisters(int address, int[] values, string ip, int port = 503) {
            var res = false;
            var client = GetClient(ip, port);
            if (client != null && client.Connected) {
                try {
                    client.WriteMultipleRegisters(address, values);
                    res = true;
                }
                catch (Exception ex) {
                    LogHelper.Error(ex.Message, ex);
                }
            }
            return res;
        }
 
        static ModbusClient GetClient(string ip, int port) {
            ModbusClient client = null;
            LogHelper.Info($"GetClient:ip:{ip},port:{port}","ModBus");
            if (!clients.ContainsKey(ip)) {
                LogHelper.Info($"GetClient:ip:{ip},port:{port}.链接不存在,开始链接", "ModBus");
                client = new ModbusClient(ip, port);
                try {
                    client.Connect();
                    //clients[ip] = client;
                    LogHelper.Info($"GetClient:ip:{ip},port:{port}.链接成功", "ModBus");
                }
                catch (Exception ex) {
                    LogHelper.Info($"GetClient:ip:{ip},port:{port}.链接异常:{ex.Message}", "ModBus");
                    LogHelper.Error(ex.Message, ex);
                }
            }
            return client;
        }
    }
 
    
}