-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRDSModbusSlave.cpp
372 lines (350 loc) · 10.9 KB
/
RDSModbusSlave.cpp
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "RDSModbusSlave.h"
#include <string.h>
#ifdef WIN32
typedef int socklen_t;
#endif
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief modbus initialization
* @param IP/PORT/debugflag
* @version v1
* @return null
* @date 2021/10/6
**************************************************************/
bool RDSModbusSlave::initModbus(std::string Host_Ip = "127.0.0.1", int port = 502, bool debugging = true)
{
ctx = modbus_new_tcp(Host_Ip.c_str(), port);
modbus_set_debug(ctx, debugging);
if (ctx == NULL)
{
fprintf(stderr, "There was an error allocating the modbus\n");
throw - 1;
}
m_modbusSocket = modbus_tcp_listen(ctx, 1);
/*设置线圈, 离散输入, 输入寄存器, 保持寄存器个数(数组元素个数))*/
mapping = modbus_mapping_new(m_numBits, m_numInputBits, m_numInputRegisters, m_numRegisters);
if (mapping == NULL)
{
fprintf(stderr, "Unable to assign mapping:%s\n", modbus_strerror(errno));
modbus_free(ctx);
m_initialized = false;
return false;
}
m_initialized = true;
return true;
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief Constructor
* @version v1
* @return null
* @date 2021/10/6
**************************************************************/
RDSModbusSlave::RDSModbusSlave(string host, uint16_t port)
{
initModbus(host, port, false);
//TODO:
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief Destructor
* @version v1
* @return null
* @date 2021/10/6
**************************************************************/
RDSModbusSlave::~RDSModbusSlave()
{
modbus_mapping_free(mapping);
modbus_close(ctx);
modbus_free(ctx);
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief loadFromConfigFile
* @version v1
* @return null
* @date 2021/10/18
**************************************************************/
void RDSModbusSlave::loadFromConfigFile()
{
return;
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief run
* @version v1
* @return null
* @date 2021/10/18
**************************************************************/
void RDSModbusSlave::run()
{
std::thread loop([this]()
{
while (true)
{
if (m_initialized)
{
recieveMessages();
}
else
{
m_initialized = true;
}
}
});
loop.detach();
return;
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief modbus_set_slave_id
* @param id
* @version v1
* @return null
* @date 2021/10/19
**************************************************************/
bool RDSModbusSlave::modbus_set_slave_id(int id)
{
int rc = modbus_set_slave(ctx, id);
if (rc == -1)
{
fprintf(stderr, "Invalid slave id\n");
modbus_free(ctx);
return false;
}
return true;
}
bool RDSModbusSlave::setInputRegisterValue(int registerStartaddress, uint16_t Value)
{
if (registerStartaddress > (m_numRegisters - 1))
{
return false;
}
slavemutex.lock();
mapping->tab_input_registers[registerStartaddress] = Value;
slavemutex.unlock();
return true;
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief setRegisterValue(设置保存寄存器的值,类型为uint16_t)
* @param registerStartaddress(保存寄存器的起始地址)
* @param Value(写入到保存寄存器里的值)
* @version v1
* @return null
* @date 2021/10/6
**************************************************************/
bool RDSModbusSlave::setHoldingRegisterValue(int registerStartaddress, uint16_t Value)
{
if (registerStartaddress > (m_numRegisters - 1))
{
return false;
}
slavemutex.lock();
mapping->tab_registers[registerStartaddress] = Value;
slavemutex.unlock();
return true;
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief getRegisterValue(获取保存寄存器的值)
* @param registerStartaddress(保存寄存器的起始地址)
* @version v1
* @return null
* @date 2021/10/6
**************************************************************/
uint16_t RDSModbusSlave::getHoldingRegisterValue(int registerStartaddress)
{
if (!m_initialized)
{
return -1;
}
return mapping->tab_registers[registerStartaddress];
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief setTab_Input_Bits(设置输入寄存器某一位的值)
* @param NumBit(输入寄存器的起始地址)
* @param Value(输入寄存器的值)
* @version v1
* @return null
* @date 2021/10/8
**************************************************************/
bool RDSModbusSlave::setTab_Input_Bits(int NumBit, uint8_t Value)
{
if (NumBit > (m_numInputBits - 1))
{
return false;
}
slavemutex.lock();
mapping->tab_input_bits[NumBit] = Value;
slavemutex.unlock();
return true;
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief getTab_Input_Bits(获取输入寄存器某一位的值)
* @param NumBit(输入寄存器相应的bit位)
* @version v1
* @return null
* @date 2021/10/8
**************************************************************/
uint8_t RDSModbusSlave::getTab_Input_Bits(int NumBit)
{
if (!m_initialized)
{
return -1;
}
return mapping->tab_input_bits[NumBit];
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief setRegisterFloatValue(设置浮点值)
* @param (Value:浮点值,registerStartaddress寄存器起始地址)
* @version v1
* @return null
* @date 2021/10/8
**************************************************************/
bool RDSModbusSlave::setHoldingRegisterValue(int registerStartaddress, float Value)
{
if (registerStartaddress > (m_numRegisters - 2))
{
return false;
}
/*小端模式*/
slavemutex.lock();
modbus_set_float(Value, &mapping->tab_registers[registerStartaddress]);
slavemutex.unlock();
return true;
}
bool RDSModbusSlave::setInputRegisterValue(int registerStartaddress, float Value)
{
if (registerStartaddress > (m_numRegisters - 2))
{
return false;
}
/*小端模式*/
slavemutex.lock();
modbus_set_float(Value, &mapping->tab_input_registers[registerStartaddress]);
slavemutex.unlock();
return true;
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief 获取寄存器里的浮点数
* @param registerStartaddress寄存器起始地址
* @version v1
* @return 两个uint16_t拼接而成的浮点值
* @date 2021/10/6
**************************************************************/
float RDSModbusSlave::getHoldingRegisterFloatValue(int registerStartaddress)
{
if (!m_initialized)
{
return -1.0f;
}
return modbus_get_float_badc(&mapping->tab_registers[registerStartaddress]);
}
/***************************************************************
* @file RDSModbusSlave.cpp
* @author seer-txj
* @brief 支持多个master同时连接
* @version v1
* @return null
* @date 2021/10/6
**************************************************************/
void RDSModbusSlave::recieveMessages()
{
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
int master_socket;
int rc;
fd_set refset;
fd_set rdset;
/* Maximum file descriptor number */
int fdmax;
/* Clear the reference set of socket */
FD_ZERO(&refset);
/* Add the server socket */
FD_SET(m_modbusSocket, &refset);
/* Keep track of the max file descriptor */
fdmax = m_modbusSocket;
while( true )
{
rdset = refset;
if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1)
{
perror("Server select() failure.");
break;
}
/* Run through the existing connections looking for data to be
* read */
for (master_socket = 0; master_socket <= fdmax; master_socket++)
{
if (!FD_ISSET(master_socket, &rdset))
{
continue;
}
if (master_socket == m_modbusSocket)
{
/* A client is asking a new connection */
socklen_t addrlen;
struct sockaddr_in clientaddr;
int newfd;
/* Handle new connections */
addrlen = sizeof(clientaddr);
memset(&clientaddr, 0, sizeof(clientaddr));
newfd = accept(m_modbusSocket, (struct sockaddr *)&clientaddr, &addrlen);
if (newfd == -1)
{
perror("Server accept() error");
} else
{
FD_SET(newfd, &refset);
if (newfd > fdmax)
{
/* Keep track of the maximum */
fdmax = newfd;
}
printf("New connection from %s:%d on socket %d\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
}
} else
{
modbus_set_socket(ctx, master_socket);
rc = modbus_receive(ctx, query);
if (rc > 0)
{
modbus_reply(ctx, query, rc, mapping);
} else if (rc == -1)
{
/* This example server in ended on connection closing or
* any errors. */
printf("Connection closed on socket %d\n", master_socket);
#ifdef _WIN32
closesocket(master_socket);
#else
close(master_socket);
#endif
/* Remove from reference set */
FD_CLR(master_socket, &refset);
if (master_socket == fdmax)
{
fdmax--;
}
}
}
}
}
m_initialized = false;
}