-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodbusmaster.cpp
More file actions
395 lines (365 loc) · 14.4 KB
/
Copy pathmodbusmaster.cpp
File metadata and controls
395 lines (365 loc) · 14.4 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "modbusmaster.h"
#include "modbusmastersub.h"
#include <QThread>
#include <QTimer>
#include <iostream>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <netinet/in.h>
#include <unistd.h>
#include <endian.h>
Q_DECLARE_METATYPE(ModBus::ModBusError)
ModBus::ModBusMaster::ModBusMaster(QString device, BaudRate br, QObject *parent) :
QObject(parent)
{
qRegisterMetaType<ModBus::ModBusError>();
sendQueue = new QQueue<mbTransaction_t *>();
sendQueue->clear();
deviceName = device;
baudRate = br;
exchangeState = STATE_IDLE;
lastTransactionId = 0;
eventThread = new QThread();
eventThread->start();
this->moveToThread(eventThread);
if (0 != (updateTimer = new QTimer()))
{
connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateTimeout()));
updateTimer->start(10);
}
}
void ModBus::ModBusMaster::startInitSlot()
{
exchangeState = STATE_INIT;
}
void ModBus::ModBusMaster::updateTimeout()
{
struct termios portOptions;
int result = 0;
mbTransaction_t *transaction = 0;
static uint8_t rxData[100];
uint8_t *rxCopy;
if (STATE_INIT == exchangeState)
{
exchangeState = STATE_ERROR;
if (-1 != (deviceDescriptor = open(deviceName.toUtf8().data(), O_RDWR | O_NOCTTY | O_NONBLOCK)))
{
if (0 == tcgetattr(deviceDescriptor, &portOptions))
{
std::cout << "[ModBus] " << deviceName.toStdString() << " options:" << std::endl;
std::cout << " c_iflag 0x" << std::hex << portOptions.c_iflag << std::endl;
std::cout << " c_oflag 0x" << std::hex << portOptions.c_oflag << std::endl;
std::cout << " c_cflag 0x" << std::hex << portOptions.c_cflag << std::endl;
std::cout << " c_lflag 0x" << std::hex << portOptions.c_lflag << std::endl;
std::cout << " c_ispeed " << std::dec << portOptions.c_ispeed << std::endl;
std::cout << " c_ospeed " << std::dec << portOptions.c_ospeed << std::endl;
switch(baudRate)
{
case BR_2400:
result = cfsetspeed(&portOptions, B2400);
break;
case BR_4800:
result = cfsetspeed(&portOptions, B4800);
break;
case BR_9600:
default:
result = cfsetspeed(&portOptions, B9600);
break;
}
if (0 == result)
{
portOptions.c_cflag &= ~PARENB;
portOptions.c_cflag &= ~CSTOPB;
portOptions.c_cflag &= ~CSIZE;
portOptions.c_cflag |= CS8 | CLOCAL | CREAD;
portOptions.c_lflag |= ICANON;
if (0 <= tcsetattr(deviceDescriptor, TCSANOW, &portOptions))
{
std::cout << "[ModBus] Port has been configured success!" << std::endl;
exchangeState = STATE_IDLE;
emit portConfigured();
}
else
{
std::cout << "[Modbus] Can`t set port options!" << std::endl;
emit error(MB_ERROR_SETOPTION);
}
}
else
{
std::cout << "[ModBus] Can`t set speed!" << std::endl;
emit error(MB_ERROR_SETSPEED);
}
}
else
{
std::cout << "[ModBus] Can`t get port options!" << std::endl;
emit error(MB_ERROR_PORTOPTION);
}
}
else
{
std::cout << "[ModBus] Can`t create device descriptor!" << std::endl;
emit error(MB_ERROR_DESCRIPTOR);
}
}
else if (STATE_TRANSMIT == exchangeState)
{
if (!sendQueue->isEmpty())
{
if (0 != (transaction = sendQueue->head()))
{
if (transaction->txSize != write(deviceDescriptor, transaction->txFrame->uint8, transaction->txSize))
{
std::cout << "[ModBus] Write len != transactionSize!" << std::endl;
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
emit transaction->sub->error(MB_ERROR_TRANSMIT);
delete transaction->txFrame;
delete transaction;
}
else
{
readTryCount = 0;
memset(rxData, 0, sizeof(rxData));
exchangeState = STATE_RECEIVE;
}
}
else
{
std::cout << "[ModBus] Transaction data has been corrupted!" << std::endl;
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
}
}
else
{
std::cout << "[ModBus] Send queue is empty!" << std::endl;
exchangeState = STATE_IDLE;
}
}
else if (STATE_RECEIVE == exchangeState)
{
if (!sendQueue->isEmpty())
{
if (0 != (transaction = sendQueue->head()))
{
if (-1 != (result = read(deviceDescriptor, &rxData[transaction->countReadBytes], sizeof(rxData))))
{
transaction->countReadBytes += result;
if (transaction->countReadBytes >= transaction->rxSize)
{
rxCopy = new uint8_t[transaction->rxSize];
memcpy(rxCopy, rxData, transaction->rxSize);
mbFrame_t *frame = reinterpret_cast<mbFrame_t *>(rxCopy);
transaction->rxFrame = frame;
transaction->crcCheck = checkCRC(rxData, transaction->rxSize);
emit transaction->sub->transactionFinished(transaction);
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
else
exchangeState = STATE_TRANSMIT;
}
else if (false == transaction->errorChecked && transaction->countReadBytes >= sizeof(ModBus::mbException_t))
{
transaction->errorChecked = true;
if (0 != reinterpret_cast<mbException_t *>(&rxData[0])->err)
{
rxCopy = reinterpret_cast<uint8_t *>(new ModBus::mbException_t);
memcpy(rxCopy, rxData, sizeof(ModBus::mbException_t));
mbFrame_t *frame = reinterpret_cast<mbFrame_t *>(rxCopy);
transaction->rxFrame = frame;
transaction->crcCheck = checkCRC(rxData, sizeof(ModBus::mbException_t));
emit transaction->sub->transactionFinished(transaction);
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
else
exchangeState = STATE_TRANSMIT;
}
}
else if (readTryCount++ > 10)
{
std::cout << "[ModBus] Read timeout!" << std::endl;
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
else
exchangeState = STATE_TRANSMIT;
emit transaction->sub->error(MB_ERROR_RECEIVE_TIMEOUT);
delete transaction->txFrame;
delete transaction;
}
}
else if (EAGAIN == errno)
{
if (readTryCount++ > 10)
{
std::cout << "[ModBus] Read timeout!!" << std::endl;
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
else
exchangeState = STATE_TRANSMIT;
emit transaction->sub->error(MB_ERROR_RECEIVE_TIMEOUT);
delete transaction->txFrame;
delete transaction;
}
}
else
{
std::cout << "[ModBus] Read error!" << std::endl;
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
else
exchangeState = STATE_TRANSMIT;
emit transaction->sub->error(MB_ERROR_RECEIVE);
delete transaction->txFrame;
delete transaction;
}
}
else
{
std::cout << "[ModBus] Transaction data has been corrupted!" << std::endl;
sendQueue->dequeue();
if (sendQueue->isEmpty())
exchangeState = STATE_IDLE;
else
exchangeState = STATE_TRANSMIT;
}
}
else
{
std::cout << "[ModBus] Send queue is empty!" << std::endl;
exchangeState = STATE_IDLE;
}
}
}
int ModBus::ModBusMaster::createRequest(ModBusMasterSub *sub, mbFuncId_t fid, uint8_t slaveId, uint16_t valAddr, uint16_t value)
{
mbTransaction_t *transaction = 0;
if (sendQueue->size() < 127)
{
if (0 != (transaction = new mbTransaction_t))
{
if (0 != (transaction->txFrame = reinterpret_cast<mbFrame_t *>(new mbReadRegsReq_t)))
{
transaction->sub = sub;
transaction->errorChecked = false;
transaction->txFrame->hdr.addr = slaveId;
transaction->txFrame->hdr.fid = fid;
switch (fid)
{
case MB_READ_HOLDING_REGISTERS_FID:
case MB_READ_INPUT_REGISTERS_FID:
fillReadRegsTransaction(transaction, valAddr, value);
break;
case MB_FORCE_SINGLE_COIL_FID:
case MB_FORCE_SINGLE_REGISTER_FID:
fillWriteSingleValueTransaction(transaction, valAddr, value);
break;
case MB_READ_EXCEPTION_STATUS_FID:
fillReadStatus(transaction);
break;
default:
delete transaction->txFrame;
delete transaction;
std::cout << "[ModBus] Unsupported function ID!" << std::endl;
return -1;
}
transaction->transactionId = lastTransactionId;
lastTransactionId++;
sendQueue->enqueue(transaction);
if (STATE_IDLE == exchangeState)
exchangeState = STATE_TRANSMIT;
return transaction->transactionId;
}
else
{
delete transaction;
return -1;
}
}
else return -1;
}
else return -1;
}
void ModBus::ModBusMaster::fillReadRegsTransaction(mbTransaction_t *transaction, uint16_t regAddr, uint16_t regsAmount)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
transaction->txFrame->readRegsReq.regAddr = htons(regAddr);
transaction->txFrame->readRegsReq.regsAmount = htons(regsAmount);
transaction->txFrame->readRegsReq.crc = htons(crcCalc(transaction->txFrame->uint8, sizeof(mbReadRegsReq_t) - 2));
#elif __BYTE_ORDER == __BIG_ENDIAN
transaction->txFrame->readRegsReq.regAddr = regAddr;
transaction->txFrame->readRegsReq.regsAmount = regsAmount;
transaction->txFrame->readRegsReq.crc = crcCalc(transaction->txFrame->uint8, sizeof(mbReadRegsReq_t) - 2);
#else
#error("Unknown byte order!")
#endif
transaction->txSize = sizeof(mbReadRegsReq_t);
transaction->rxSize = sizeof(mbReadRegsResp_t) + (sizeof(uint16_t) * regsAmount);
}
void ModBus::ModBusMaster::fillWriteSingleValueTransaction(mbTransaction_t *transaction, uint16_t addr, uint16_t value)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
transaction->txFrame->writeRegReq.regAddr = htons(addr);
transaction->txFrame->writeRegReq.regVal = htons(value);
transaction->txFrame->writeRegReq.crc = htons(crcCalc(transaction->txFrame->uint8, sizeof(mbWriteRegReq_t) - 2));
#elif __BYTE_ORDER == __BIG_ENDIAN
transaction->txFrame->writeRegReq.regAddr = addr;
transaction->txFrame->writeRegReq.regVal = value;
transaction->txFrame->writeRegReq.crc = crcCalc(transaction->txFrame->uint8, sizeof(mbWriteRegReq_t) - 2);
#else
#error("Unknown byte order!")
#endif
transaction->txSize = sizeof(mbWriteRegReq_t);
transaction->rxSize = sizeof(mbWriteRegResp_t);
}
void ModBus::ModBusMaster::fillReadStatus(mbTransaction_t *transaction)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
transaction->txFrame->readExceptionReq.crc = htons(crcCalc(transaction->txFrame->uint8, sizeof(mbWriteRegReq_t) - 2));
#elif __BYTE_ORDER == __BIG_ENDIAN
transaction->txFrame->readExceptionReq.crc = crcCalc(transaction->txFrame->uint8, sizeof(mbWriteRegReq_t) - 2);
#else
#error("Unknown byte order!")
#endif
transaction->txSize = sizeof(mbReadExceptionReq_t);
transaction->rxSize = sizeof(mbReadExceptionResp_t);
}
bool ModBus::ModBusMaster::checkCRC(uint8_t *buf, uint16_t len)
{
uint16_t calcCRC = crcCalc(buf, len - 2);
uint16_t messageCRC = (buf[len - 2] << 8) | buf[len - 1];
return (calcCRC == messageCRC);
}
uint16_t ModBus::ModBusMaster::crcCalc(uint8_t *buf, uint16_t len)
{
uint16_t crc = 0xFFFF;
uint16_t polynom = 0xA001;
uint16_t i = 0;
uint16_t j = 0;
for (i = 0; i < len; i++)
{
crc = crc ^ buf[i];
for (j = 0; j < 8; j++)
{
if (crc & 0x0001)
{
crc = crc >> 1;
crc = crc ^ polynom;
}
else
{
crc = crc >> 1;
}
}
}
return(crc & 0xFFFF);
}