-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZModbusRTU.cpp
More file actions
488 lines (443 loc) · 15.7 KB
/
Copy pathZModbusRTU.cpp
File metadata and controls
488 lines (443 loc) · 15.7 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "ZModbusRTU.h"
// Constructor ZModbusRTU
ZModbusRTU::ZModbusRTU(HardwareSerial &serialPort, int baudrate, int slaveid = 1) {
_slaveid = slaveid;
_serial = &serialPort; // Store the reference to the Serial object
_baudrate = baudrate;
// Initialize pointers to null
CoilStatus = nullptr;
InputStatus = nullptr;
HoldingReg = nullptr;
InputReg = nullptr;
setMaximum(_Max_CoilStatus, _Max_InputStatus, _Max_HoldingReg, _Max_InputReg);//Set as default size
}
void ZModbusRTU::setdebug(bool debug){
_debug = debug;
}
void ZModbusRTU::setWriteRegisterCallback(WriteRegisterCallback callback) {
writeRegisterCallback = callback;
}
void ZModbusRTU::setWriteCoilCallback(WriteCoilCallback callback) {
writeCoilCallback = callback;
}
//Default Maximum each function is 100
void ZModbusRTU::setMaximum(int coilstatus,
int inputstatus,
int holdingregister,
int inputregister){
_Max_CoilStatus = coilstatus;
_Max_InputStatus = inputstatus;
_Max_HoldingReg = holdingregister;
_Max_InputReg = inputregister;
// Initialize arrays with default sizes
setArraySize(_Max_CoilStatus, _Max_InputStatus, _Max_HoldingReg, _Max_InputReg); // Default sizes
}
void ZModbusRTU::begin() {
_state = true;
_serial->begin(_baudrate);
if(_debug){
_serial->print("_slaveid: ");
_serial->println(_slaveid);
_serial->print("COILSTATUS: ");
_serial->println(_Max_CoilStatus);
_serial->print("INPUTSTATUS: ");
_serial->println(_Max_InputStatus);
_serial->print("HOLDINGREG: ");
_serial->println(_Max_HoldingReg);
_serial->print("INPUTREG: ");
_serial->println(_Max_InputReg);
}
}
void ZModbusRTU::stop() {
if (_state) {
_state = false;
if(_debug){_serial->println("Modbus communication stopped.");}
_serial->end(); // End the serial communication
}
}
// Dynamically change baudrate
void ZModbusRTU::setBaudrate(int newBaudrate) {
// Stop the current communication
stop();
// Set the new baudrate
_baudrate = newBaudrate;
// Restart communication with the new baudrate
begin();
if(_debug){_serial->print("Baudrate changed to: ");}
if(_debug){_serial->println(_baudrate);}
}
//Default = 8ms
void ZModbusRTU::setFetchTimer(uint16_t ms){
_fetchtimer = ms;
}
void ZModbusRTU::handle() {
if(_state == true){
timer = millis();
if(timer - timer1 >= _fetchtimer){
if(_serial->available() > 0){
_rx_buff[_rx_index] = _serial->read();
_rx_index++;
//delay(1);
}
else{// if(_rx_index>0)
//analyze here
if(_rx_index > 0){
modbusfunction(_rx_buff, _rx_index);
}
//reset index buffer
_rx_index = 0;
}
timer1 = timer;
}
}
}
void ZModbusRTU::modbusfunction(uint8_t* buff, uint16_t length){
uint16_t deviceid = 0;
uint16_t function = 0;
uint16_t start = 0;
uint16_t quantity = 0;
uint16_t value = 0;
uint16_t crc = 0;
if(_debug){_serial->print("length: ");}
if(_debug){_serial->println(length);}
if(length >= 8){
deviceid = buff[0];
if(_debug){_serial->print("deviceid: ");}
if(_debug){_serial->println(deviceid);}
if(deviceid == _slaveid){
function = buff[1];
if(function >= 0x01 && function <= 0x04){
start = buff[2]*256;
start += buff[3];
quantity = buff[4]*256;
quantity += buff[5];
crc = buff[7]*256;
crc += buff[6];
}
else if(function >= 0x05 && function <= 0x06){
start = buff[2]*256;
start += buff[3];
value = buff[4]*256;
value += buff[5];
crc = buff[7]*256;
crc += buff[6];
}
else if(function >= 0x0F && function <= 0x10){
}
bool crcValid = crcCheck(buff, length - 2, crc);
if(_debug){_serial->print("crcValid: ");}
if(_debug){_serial->println(crcValid);}
if(crcValid){
if(_debug){_serial->print("function: ");}
if(_debug){_serial->println(function);}
if(function == 0x01){//(0x01) Read Coils
// 0 to 10 < if max = 20 (0-19)
if(start + quantity <= _Max_CoilStatus && start + quantity <= 65535){
uint8_t ft = 0;
if(quantity % 8 != 0){
ft = 1;
}
uint16_t coillen = (quantity/8) + ft;
uint8_t coil[coillen] = {0};
// Initialize the coil array to 0
for (uint16_t i = 0; i < coillen; i++) {
coil[i] = 0;
}
//Mapping array to bits
uint16_t id = 0;
for(uint16_t c = 0; c < coillen; c++){
for(uint8_t b = 0; b < 8; b++){
//CoilStatus[(c * 8) + b] = random(0, 1);
if(id < quantity){
coil[c] += markbit[b] * CoilStatus[id];
id++;
}
}
}
// Mapping CoilStatus to coil array
// Mapping CoilStatus to coil array
/*for (uint16_t c = 0; c < coillen; c++) {
for (uint8_t b = 0; b < 8; b++) {
uint16_t index = start + (c * 8) + b;
if (index < quantity) {
// Set the bit in coil[c], LSB first
coil[c] |= (CoilStatus[index] << b);
}
}
}*/
if(_debug){_serial->print("coil[0]: ");}
if(_debug){_serial->printf("0=%d, len=%d", CoilStatus[0], coillen);}
//delay(16);
response(_slaveid, function, coil, coillen);
}
else{
//Out of length
}
}
else if(function == 0x02){//(0x02) Read Discrete Inputs(Input status)
// 0 to 10 < if max = 20 (0-19)
if(start + quantity <= _Max_InputStatus && start + quantity <= 65535){
uint8_t ft = 0;
if(quantity % 8 != 0){
ft = 1;
}
uint16_t coillen = (quantity/8) + ft;
uint8_t coil[coillen] = {0};
for (uint16_t i = 0; i < coillen; i++) {
coil[i] = 0;
}
//Mapping array to bits
for(uint16_t c = 0; c < coillen; c++){
for(uint8_t b = 0; b < 8; b++){
//InputStatus[(c * 8) + b] = random(0, 1);
coil[c] += markbit[b] * InputStatus[(c * 8) + b];
}
}
if(_debug){_serial->print("coil[0]: ");}
if(_debug){_serial->printf("0=%d, len=%d", InputStatus[0], coillen);}
//delay(16);
response(_slaveid, function, coil, coillen);
}
else{
//Out of length
}
}
else if(function == 0x03){//(0x03) Read Holding Registers
// 0 to 10 < if max = 20 (0-19)
if(start + quantity <= _Max_HoldingReg && start + quantity <= 65535){
uint16_t reglen = quantity * 2;
uint8_t reg[reglen] = {0};
for (uint16_t i = 0; i < reglen; i++) {
reg[i] = 0;
}
//Mapping array to bits
for(uint16_t c = 0; c < reglen; c += 2){
reg[c] = HoldingReg[start + (c / 2)] / 256;
reg[c + 1] = HoldingReg[start + (c / 2)] % 256;
}
if(_debug){_serial->print("coil[0]: ");}
if(_debug){_serial->printf("0=%d,1=%d, len=%d", reg[0], reg[1], reglen);}
//delay(16);
response(_slaveid, function, reg, reglen);
}
else{
//Out of length
}
}
else if(function == 0x04){//(0x04) Read Input Registers
// 0 to 10 < if max = 20 (0-19)
if(start + quantity <= _Max_InputReg && start + quantity <= 65535){
uint16_t reglen = quantity * 2;
uint8_t reg[reglen] = {0};
for (uint16_t i = 0; i < reglen; i++) {
reg[i] = 0;
}
//Mapping array to bits
for(uint16_t c = 0; c < reglen; c += 2){
reg[c] = InputReg[(c / 2)] / 256;
reg[c + 1] = InputReg[(c / 2)] % 256;
}
if(_debug){_serial->print("coil[0]: ");}
if(_debug){_serial->printf("0=%d,1=%d, len=%d", reg[0], reg[1], reglen);}
//delay(16);
response(_slaveid, function, reg, reglen);
}
else{
//Out of length
}
}
else if(function == 0x05){//(0x05) Write Single Coil
if(start < _Max_CoilStatus && start < 65535){
CoilStatus[start] = (value==0xFF00?1:(value==0x0100?1:0));
if(_debug){_serial->printf("CoilStatus[%d]: %d\n", start, CoilStatus[start]);}
uint8_t cs[4] = {start/256, start%256, (CoilStatus[start]==1?0xFF:0), 0};
writeCoilCallback(start, CoilStatus[start]);
responsewrite(_slaveid, function, cs, 4);
}
}
else if(function == 0x06){//(0x06) Write Single Register
if(start < _Max_HoldingReg && start < 65535){
HoldingReg[start] = value;
if(_debug){_serial->printf("HoldingReg[%d]: %d\n", start, HoldingReg[start]);}
uint8_t hr[4] = {start/256, start%256, (HoldingReg[start]/256), (HoldingReg[start]%256)};
// Trigger the callback if set
if (writeRegisterCallback != nullptr) {
writeRegisterCallback(start, value);
}
responsewrite(_slaveid, function, hr, 4);
}
}
else if(function == 0xF0){//(0x0F) Write Multiple Coils
}
else if(function == 0x10){//(0x10) Write Multiple Registers
}
}
/*if(function == 0x01){//(0x01) Read Coils
}
else if(function == 0x02){//(0x02) Read Discrete Inputs(Input status)
}
else if(function == 0x03){//(0x03) Read Holding Registers
}
else if(function == 0x04){//(0x04) Read Input Registers
}*/
/*
01 (0x01) Read Coils
02 (0x02) Read Discrete Inputs
03 (0x03) Read Holding Registers
04 (0x04) Read Input Registers
05 (0x05) Write Single Coil
06 (0x06) Write Single Register
08 (0x08) Diagnostics (Serial Line only)
11 (0x0B) Get Comm Event Counter (Serial Line only)
15 (0x0F) Write Multiple Coils
16 (0x10) Write Multiple Registers
17 (0x11) Report Server ID (Serial Line only)
22 (0x16) Mask Write Register
23 (0x17) Read/Write Multiple Registers
43 / 14 (0x2B / 0x0E) Read Device Identification
*/
}
}
}
// Function to construct and send a Modbus RTU response
void ZModbusRTU::response(uint8_t slaveID, uint8_t function, uint8_t* data, uint16_t dataLength) {
// Calculate the length of the response message
uint8_t responseLength = 3 + dataLength + 2; // Slave ID + Function Code + Data + CRC
// Create a buffer for the response message
uint8_t response[responseLength];
// Populate the response message
response[0] = slaveID; // Slave ID
response[1] = function; // Function code
response[2] = dataLength; // Function code
// Copy the data into the response message
/*if (dataLength > 0) {
memcpy(&response[3], data, dataLength); // Copy data into the response array
}*/
for(uint16_t j = 0; j < dataLength; j++){
response[3 + j] = data[j];
}
//delay(16);
// Calculate the CRC for the response
uint16_t crc = calculateCRC(response, responseLength - 2); // Exclude CRC bytes from calculation
response[responseLength - 2] = crc%256;// crc & 0xFF; // Low byte of CRC
response[responseLength - 1] = crc/256;//(crc >> 8) & 0xFF; // High byte of CRC
// Send the response message over the serial port
if(_debug){
for (uint16_t i = 0; i < responseLength; i++) {
_serial->printf("%2X ", response[i]);
}
_serial->printf("\n");
}
_serial->write(response, responseLength);
}
// Function to construct and send a Modbus RTU response
void ZModbusRTU::responsewrite(uint8_t slaveID, uint8_t function, uint8_t* data, uint16_t dataLength) {
// Calculate the length of the response message
uint8_t responseLength = 2 + dataLength + 2; // Slave ID + Function Code + Data + CRC
// Create a buffer for the response message
uint8_t response[responseLength];
// Populate the response message
response[0] = slaveID; // Slave ID
response[1] = function; // Function code
// Copy the data into the response message
/*if (dataLength > 0) {
memcpy(&response[3], data, dataLength); // Copy data into the response array
}*/
for(uint8_t j = 0; j < dataLength; j++){
response[2 + j] = data[j];
}
//delay(16);
// Calculate the CRC for the response
uint16_t crc = calculateCRC(response, responseLength - 2); // Exclude CRC bytes from calculation
response[responseLength - 2] = crc%256;// crc & 0xFF; // Low byte of CRC
response[responseLength - 1] = crc/256;//(crc >> 8) & 0xFF; // High byte of CRC
// Send the response message over the serial port
if(_debug){
for (uint8_t i = 0; i < responseLength; i++) {
_serial->printf("%2X ", response[i]);
}
_serial->printf("\n");
}
_serial->write(response, responseLength);
}
// CRC-16 calculation for Modbus RTU
uint16_t ZModbusRTU::calculateCRC(uint8_t* message, uint16_t length) {
uint16_t crc = 0xFFFF; // Initial value for CRC
for (uint16_t i = 0; i < length; i++) {
crc ^= message[i]; // XOR byte into least significant byte of crc
for (uint8_t j = 0; j < 8; j++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0xA001; // If LSB is set, shift and XOR with polynomial
} else {
crc = crc >> 1; // Else just shift
}
}
}
return crc;
}
// Function to check CRC from incoming Modbus message
bool ZModbusRTU::crcCheck(uint8_t* message, uint16_t length, uint16_t crc) {
uint16_t calculatedCRC = calculateCRC(message, length); // Calculate CRC for the message
return (calculatedCRC == crc);
}
// Function to check CRC from incoming Modbus message
/*bool ZModbusRTU::crcCheck(uint8_t* message, uint16_t length, uint16_t crcH, uint16_t crcL) {
uint16_t calculatedCRC = calculateCRC(message, length); // Calculate CRC for the message
// Split the calculated CRC into high and low bytes
uint8_t calculatedCrcH = (calculatedCRC >> 8) & 0xFF; // High byte
uint8_t calculatedCrcL = calculatedCRC & 0xFF; // Low byte
// Compare calculated CRC with the received crcH and crcL
return (calculatedCrcH == crcH) && (calculatedCrcL == crcL);
}*/
/*void ZModbusRTU::setArraySize(uint16_t maxCoilStatus, uint16_t maxInputStatus,
uint16_t maxHoldingReg, uint16_t maxInputReg) {
// Delete previous arrays if they exist
delete[] CoilStatus;
delete[] InputStatus;
delete[] HoldingReg;
delete[] InputReg;
// Allocate new memory for the arrays
_Max_CoilStatus = maxCoilStatus;
_Max_InputStatus = maxInputStatus;
_Max_HoldingReg = maxHoldingReg;
_Max_InputReg = maxInputReg;
CoilStatus = new uint8_t[_Max_CoilStatus];
InputStatus = new uint8_t[_Max_InputStatus];
HoldingReg = new uint8_t[_Max_HoldingReg];
InputReg = new uint8_t[_Max_InputReg];
}*/
void ZModbusRTU::setArraySize(uint16_t maxCoilStatus, uint16_t maxInputStatus,
uint16_t maxHoldingReg, uint16_t maxInputReg) {
// Delete previous arrays if they exist
delete[] CoilStatus;
delete[] InputStatus;
delete[] HoldingReg;
delete[] InputReg;
// Allocate new arrays with specified sizes
CoilStatus = new uint8_t[maxCoilStatus];
InputStatus = new uint8_t[maxInputStatus];
HoldingReg = new uint16_t[maxHoldingReg];
InputReg = new uint16_t[maxInputReg];
for(int i=0;i<maxCoilStatus;i++){
CoilStatus[i] = 0;
}
for(int i=0;i<maxInputStatus;i++){
InputStatus[i] = 0;
}
for(int i=0;i<maxHoldingReg;i++){
HoldingReg[i] = 0;
}
for(int i=0;i<maxInputReg;i++){
InputReg[i] = 0;
}
}
void ZModbusRTU::setCoilStatusValue(uint16_t address, uint8_t value){
if(address < _Max_CoilStatus){
CoilStatus[address] = (value==0xFF?1:(value==0x01?1:0));
}
}
void ZModbusRTU::setHoldingRegisterValue(uint16_t address, uint16_t value){
if(address < _Max_HoldingReg){
HoldingReg[address] = value;
}
}