-
Notifications
You must be signed in to change notification settings - Fork 111
/
ModbusModule.cpp
354 lines (264 loc) · 9.65 KB
/
ModbusModule.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
///////////////////////////////////////////////////////////////////////////////
// FILE: ModbusModule.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: DeviceAdapters
//-----------------------------------------------------------------------------
// DESCRIPTION: Modbus Adapter using libmodbus
//
// AUTHOR: Christian Sachs <[email protected]>
//
// COPYRIGHT: Forschungszentrum Jülich
// LICENSE: BSD (2-clause/FreeBSD license)
#define _WINSOCK2API_
#define _WINSOCKAPI_
#include "ModbusModule.h"
#include <string>
#include <iostream>
#include "ModuleInterface.h"
using namespace std;
///////////////////////////////////////////////////////////////////////////////
// Exported MMDevice API
///////////////////////////////////////////////////////////////////////////////
const char* g_ModbusDeviceName = "Modbus";
MODULE_API void InitializeModuleData()
{
RegisterDevice(g_ModbusDeviceName, MM::GenericDevice, g_ModbusDeviceName);
}
MODULE_API MM::Device* CreateDevice(const char* deviceName)
{
if (deviceName == 0)
return 0;
if (strcmp(deviceName, g_ModbusDeviceName) == 0)
{
return new CModbusDevice();
}
return 0;
}
MODULE_API void DeleteDevice(MM::Device* pDevice)
{
delete pDevice;
}
//
void CModbusDevice::GetName(char* pszName) const {
CDeviceUtils::CopyLimitedString(pszName, g_ModbusDeviceName);
}
bool CModbusDevice::Busy() {
return false;
}
//
CModbusDevice::CModbusDevice() {
coilBuffer = NULL;
registerBuffer = NULL;
ready = false;
debugFlag = 0;
connectionURI = "tcp://127.0.0.1:502";
deviceString = "name=device type=coils write_address=0 read_address=0 count=8";
CreateProperty("ConnectionURI", "", MM::String, false, new DeviceAction(this, &CModbusDevice::OnConnectionURI), true);
CreateProperty("Devices", "", MM::String, false, new DeviceAction(this, &CModbusDevice::OnDeviceString), true);
CreateProperty("DebugFlag", "0", MM::Integer, false, new DeviceAction(this, &CModbusDevice::OnDebugFlag), true);
AddAllowedValue("DebugFlag", "0");
AddAllowedValue("DebugFlag", "1");
}
CModbusDevice::~CModbusDevice() {
// nop
}
int CModbusDevice::OnConnectionURI(MM::PropertyBase *pProp, MM::ActionType eAct) {
if(eAct == MM::BeforeGet)
{
pProp->Set(connectionURI.c_str());
}
else if(eAct == MM::AfterSet)
{
pProp->Get(connectionURI);
}
return DEVICE_OK;
}
int CModbusDevice::OnDeviceString(MM::PropertyBase *pProp, MM::ActionType eAct) {
if(eAct == MM::BeforeGet)
{
pProp->Set(deviceString.c_str());
}
else if(eAct == MM::AfterSet)
{
pProp->Get(deviceString);
}
return DEVICE_OK;
}
int CModbusDevice::OnDebugFlag(MM::PropertyBase *pProp, MM::ActionType eAct) {
if(eAct == MM::BeforeGet)
{
pProp->Set(debugFlag);
}
else if(eAct == MM::AfterSet)
{
pProp->Get(debugFlag);
}
return DEVICE_OK;
}
inline int CModbusDevice::checkContextAndContinue() {
if(ctx == NULL)
return DEVICE_ERR;
modbus_set_debug(ctx, (int)debugFlag);
if(modbus_connect(ctx) == -1) {
LogMessage(string("libmodbus returned the following error: ") + modbus_strerror(errno)); // WHY is errno a global variable? ...
modbus_free(ctx);
return DEVICE_ERR;
}
return DEVICE_OK;
}
int CModbusDevice::Initialize() {
URI conuri(connectionURI);
if(conuri.protocol == "tcp") {
int port = MODBUS_TCP_DEFAULT_PORT;
if(conuri.port != "")
port = atoi(conuri.port.c_str());
LogMessage(string("Trying to perform a tcp connection to ") + conuri.hostandaddress + ", port: " + CDeviceUtils::ConvertToString(port));
ctx = modbus_new_tcp(conuri.hostandaddress.c_str(), port);
} else if(conuri.protocol == "tcp+pi") {
if(conuri.port == "")
conuri.port = "1502";
LogMessage(string("Trying to perform a tcp+pi connection to ") + conuri.hostandaddress + ", port: " + conuri.port);
ctx = modbus_new_tcp_pi(conuri.hostandaddress.c_str(), conuri.port.c_str());
} else if(conuri.protocol == "rtu") {
int baud = 115200;
char parity = 'N';
int data_bit = 8;
int stop_bit = 1;
map<string, string>::iterator it;
if((it = conuri.parameters.find("baud")) != conuri.parameters.end())
baud = atoi(it->second.c_str());
if((it = conuri.parameters.find("data_bit")) != conuri.parameters.end())
data_bit = atoi(it->second.c_str());
if((it = conuri.parameters.find("stop_bit")) != conuri.parameters.end())
stop_bit = atoi(it->second.c_str());
if((it = conuri.parameters.find("parity")) != conuri.parameters.end())
parity = it->second[0];
char parityChars[2] = {0, 0};
parityChars[0] = parity;
LogMessage(string("Trying to perform a rtu connection via ") + conuri.hostandaddress + " baud: " + CDeviceUtils::ConvertToString(baud) + " parity: " + parityChars + " data_bit: " + CDeviceUtils::ConvertToString(data_bit) + " stop_bit: " + CDeviceUtils::ConvertToString(stop_bit));
ctx = modbus_new_rtu(conuri.hostandaddress.c_str(), baud, parity, data_bit, stop_bit);
} else {
LogMessage("Unsupported protocol part passed as connectionURI (supported is tcp, tcp+pi, rtu)");
return DEVICE_ERR;
}
if(checkContextAndContinue() == DEVICE_ERR) {
return DEVICE_ERR;
}
LogMessage("Modbus connected. Adding devices ...");
pair<string, string> splits, innerSplits, kv;
splits.second = deviceString;
//name=valves type=coils write_address=0 read_address=512 count=8
while((splits=split(splits.second, ";")).first != "") {
ModbusDevice d;
innerSplits.second = splits.first;
while((innerSplits=split(innerSplits.second, " ")).first != "") {
kv = split(innerSplits.first, "=");
if(kv.first == "name") {
d.name = kv.second;
} else if(kv.first == "type") {
d.typeStr = kv.second;
if(kv.second == "coils") {
d.type = MD_COILS;
} else if(kv.second == "registers") {
LogMessage("Not yet implemented.");
return DEVICE_ERR;
} else {
LogMessage("Unsupported type passed.");
}
} else if(kv.first == "read_address") {
d.read_address = atoi(kv.second.c_str());
} else if(kv.first == "write_address") {
d.write_address = atoi(kv.second.c_str());
} else if(kv.first == "count") {
d.count = atoi(kv.second.c_str());
}
}
if(d.name != "") {
deviceConfiguration.push_back(d);
} else {
LogMessage("Device without name encountered?! Ignoring.");
}
}
CreateProperty("general-connURI", connectionURI.c_str(), MM::String, true, NULL, false);
CreateProperty("general-libmodbus-version", LIBMODBUS_VERSION_STRING, MM::String, true, NULL, false);
string devicesToAdd = "";
for(vector<ModbusDevice>::iterator i = deviceConfiguration.begin(); i != deviceConfiguration.end(); i++) {
devicesToAdd += "," + i->name;
}
devicesToAdd = devicesToAdd.substr(1);
CreateProperty("general-devices", devicesToAdd.c_str(), MM::String, true, NULL, false);
int currentNumber = 0;
largestCount = 0;
for(vector<ModbusDevice>::iterator i = deviceConfiguration.begin(); i != deviceConfiguration.end(); i++) {
largestCount = largestCount > i->count ? largestCount : i->count;
string prefix = "";
prefix += i->name;
CreateProperty((prefix + "-name").c_str(), i->name.c_str(), MM::String, true, NULL, false);
CreateProperty((prefix + "-type").c_str(), i->typeStr.c_str(), MM::String, true, NULL, false);
CreateProperty((prefix + "-read-address").c_str(), CDeviceUtils::ConvertToString(i->read_address), MM::Integer, true, NULL, false);
CreateProperty((prefix + "-write-address").c_str(), CDeviceUtils::ConvertToString(i->write_address), MM::Integer, true, NULL, false);
CreateProperty((prefix + "-count").c_str(), CDeviceUtils::ConvertToString(i->count), MM::Integer, true, NULL, false);
CreateProperty((prefix).c_str(), "", MM::String, false, new DeviceActionEx(this, &CModbusDevice::OnDeviceValue, currentNumber), false);
currentNumber ++;
}
coilBuffer = new unsigned char[largestCount];
registerBuffer = new unsigned short[largestCount];
return DEVICE_OK;
}
int CModbusDevice::OnDeviceValue(MM::PropertyBase *pProp, MM::ActionType eAct, long data) {
string helper;
ModbusDevice d = deviceConfiguration[data];
if(d.type == MD_COILS) {
memset(coilBuffer, 0, sizeof(unsigned char) * (largestCount));
if(eAct == MM::BeforeGet)
{
int result;
result = modbus_read_bits(ctx, d.read_address, d.count, coilBuffer);
for(int i = 0; i < d.count; i++) {
if(coilBuffer[i]) {
helper += "1";
} else {
helper += "0";
}
}
//
pProp->Set(helper.c_str());
}
else if(eAct == MM::AfterSet)
{
pProp->Get(helper);
if(helper == "off") {
for(int i = 0; i < d.count; i++) coilBuffer[i] = 0;
} else if(helper == "on") {
for(int i = 0; i < d.count; i++) coilBuffer[i] = 1;
} else {
if(helper.size() != (unsigned) d.count) {
LogMessage("ERROR. Malfomed Value set! Rereading from device.");
return OnDeviceValue(pProp, MM::BeforeGet, data);
}
for(int i = 0; i < d.count; i++) {
if(helper[i] == '1') {
coilBuffer[i] = 1;
} else if(helper[i] == '0') {
coilBuffer[i] = 0;
} else {
LogMessage("ERROR. Malfomed Value set! Rereading from device.");
return OnDeviceValue(pProp, MM::BeforeGet, data);
}
}
}
modbus_write_bits(ctx, d.write_address, d.count, coilBuffer);
}
return DEVICE_OK;
} else {
return DEVICE_ERR;
}
}
int CModbusDevice::Shutdown() {
ready = false;
if(coilBuffer != NULL)
delete coilBuffer;
if(registerBuffer != NULL)
delete registerBuffer;
return DEVICE_OK;
}