Repository navigation
Expand file tree
/
Copy pathSensorModbusMaster.cpp
More file actions
809 lines (726 loc) · 28.4 KB
/
Copy pathSensorModbusMaster.cpp
File metadata and controls
809 lines (726 loc) · 28.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
*SensorModbusMaster.cpp
*/
#include "SensorModbusMaster.h"
// A define on the debugging to make sure we never attempt to access a null pointer
// Thank you Neil Hancock for this catch!
#define _debugStream if (NULL != _debugStream1) _debugStream1
// initialize the response buffer
byte modbusMaster::responseBuffer[RESPONSE_BUFFER_SIZE] = {0x00,};
byte modbusMaster::crcFrame[2] = {0x00,};
//----------------------------------------------------------------------------
// HIGHEST LEVEL FUNCTIONS
//----------------------------------------------------------------------------
// This function sets up the communication
// It should be run during the arduino "setup" function.
// The "stream" device must be initialized and begun prior to running this.
bool modbusMaster::begin(byte modbusSlaveID, Stream *stream, int enablePin)
{
// Give values to variables;
_slaveID = modbusSlaveID;
_stream = stream;
_enablePin = enablePin;
// Set pin mode for the enable pin
if (_enablePin >= 0) pinMode(_enablePin, OUTPUT);
recieverEnable();
_stream->setTimeout(modbusFrameTimeout);
return true;
}
bool modbusMaster::begin(byte modbusSlaveID, Stream &stream, int enablePin)
{return begin(modbusSlaveID, &stream, enablePin);}
// These functions return a variety of data from a data register
uint16_t modbusMaster::uint16FromRegister(byte regType, int regNum, endianness endian)
{
getRegisters(regType, regNum, UINT16_SIZE/2);
return uint16FromFrame(endian);
}
int16_t modbusMaster::int16FromRegister(byte regType, int regNum, endianness endian)
{
getRegisters(regType, regNum, INT16_SIZE/2);
return int16FromFrame(endian);
}
float modbusMaster::float32FromRegister(byte regType, int regNum, endianness endian)
{
getRegisters(regType, regNum, FLOAT32_SIZE/2);
return float32FromFrame(endian);
}
uint32_t modbusMaster::uint32FromRegister(byte regType, int regNum, endianness endian)
{
getRegisters(regType, regNum, UINT32_SIZE/2);
return uint32FromFrame(endian);
}
int32_t modbusMaster::int32FromRegister(byte regType, int regNum, endianness endian)
{
getRegisters(regType, regNum, INT32_SIZE/2);
return int32FromFrame(endian);
}
uint32_t modbusMaster::TAI64FromRegister(byte regType, int regNum)
{
getRegisters(regType, regNum, TAI64_SIZE/2);
return TAI64FromFrame();
}
uint32_t modbusMaster::TAI64NFromRegister(byte regType, int regNum, uint32_t &nanoseconds)
{
getRegisters(regType, regNum, TAI64N_SIZE/2);
return TAI64NFromFrame(nanoseconds);
}
uint32_t modbusMaster::TAI64NAFromRegister(byte regType, int regNum, uint32_t &nanoseconds, uint32_t &attoseconds)
{
getRegisters(regType, regNum, TAI64NA_SIZE/2);
return TAI64NAFromFrame(nanoseconds, attoseconds);
}
byte modbusMaster::byteFromRegister(byte regType, int regNum, int byteNum)
{
getRegisters(regType, regNum, 1);
if (byteNum == 1) return byteFromFrame();
else return byteFromFrame(4);
}
uint16_t modbusMaster::pointerFromRegister(byte regType, int regNum, endianness endian)
{
getRegisters(regType, regNum, POINTER_SIZE/2);
return pointerFromFrame(endian);
}
int8_t modbusMaster::pointerTypeFromRegister(byte regType, int regNum, endianness endian)
{
getRegisters(regType, regNum, POINTER_SIZE/2);
return pointerTypeFromFrame(endian);
}
String modbusMaster::StringFromRegister(byte regType, int regNum, int charLength)
{
getRegisters(regType, regNum, charLength/2);
return StringFromFrame(charLength);
}
void modbusMaster::charFromRegister(byte regType, int regNum, char outChar[], int charLength)
{
getRegisters(regType, regNum, charLength/2);
charFromFrame(outChar, charLength);
}
// These set data in registers to a variety of data types
// For data types that can be only one register long, the modbus command to pre-set
// a single register (0x06) will be used by default. To force the use of the pre-set
// multiple registers command, set the boolean input for forceMultiple to true.
bool modbusMaster::uint16ToRegister(int regNum, uint16_t value, endianness endian, bool forceMultiple)
{
byte inputData[UINT16_SIZE] = {0x00,};
uint16ToFrame(value, endian, inputData);
return setRegisters(regNum, UINT16_SIZE/2, inputData, forceMultiple);
}
bool modbusMaster::int16ToRegister(int regNum, int16_t value, endianness endian, bool forceMultiple)
{
byte inputData[INT16_SIZE] = {0x00,};
int16ToFrame(value, endian, inputData);
return setRegisters(regNum, INT16_SIZE/2, inputData, forceMultiple);
}
bool modbusMaster::float32ToRegister(int regNum, float value, endianness endian)
{
byte inputData[FLOAT32_SIZE] = {0x00,};
float32ToFrame(value, endian, inputData);
return setRegisters(regNum, FLOAT32_SIZE/2, inputData);
}
bool modbusMaster::uint32ToRegister(int regNum, uint32_t value, endianness endian)
{
byte inputData[UINT32_SIZE] = {0x00,};
uint32ToFrame(value, endian, inputData);
return setRegisters(regNum, UINT32_SIZE/2, inputData);
}
bool modbusMaster::int32ToRegister(int regNum, int32_t value, endianness endian)
{
byte inputData[INT32_SIZE] = {0x00,};
int32ToFrame(value, endian, inputData);
return setRegisters(regNum, INT32_SIZE/2, inputData);
}
bool modbusMaster::TAI64ToRegister(int regNum, uint32_t seconds)
{
byte inputData[TAI64_SIZE] = {0x00,};
TAI64ToFrame(seconds, inputData);
return setRegisters(regNum, TAI64_SIZE/2, inputData);
}
bool modbusMaster::TAI64NToRegister(int regNum, uint32_t seconds, uint32_t nanoseconds)
{
byte inputData[TAI64N_SIZE] = {0x00,};
TAI64NToFrame(seconds, nanoseconds, inputData);
return setRegisters(regNum, TAI64N_SIZE/2, inputData);
}
bool modbusMaster::TAI64NAToRegister(int regNum, uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds)
{
byte inputData[TAI64NA_SIZE] = {0x00,};
TAI64NAToFrame(seconds, nanoseconds, attoseconds, inputData);
return setRegisters(regNum, TAI64NA_SIZE/2, inputData);
}
bool modbusMaster::byteToRegister(int regNum, int byteNum, byte value, bool forceMultiple)
{
byte inputData[2] = {0x00,};
byteToFrame(value, byteNum, inputData);
return setRegisters(regNum, 1, inputData, forceMultiple);
}
bool modbusMaster::pointerToRegister(int regNum, uint16_t value, pointerType point, endianness endian, bool forceMultiple)
{
byte inputData[UINT16_SIZE] = {0x00,};
pointerToFrame(value, point, endian, inputData);
return setRegisters(regNum, UINT16_SIZE/2, inputData, forceMultiple);
}
bool modbusMaster::StringToRegister(int regNum, String value, bool forceMultiple)
{
int charLength = value.length();
byte inputData[charLength];
StringToFrame(value, inputData);
return setRegisters(regNum, charLength/2, inputData, forceMultiple);
}
bool modbusMaster::charToRegister(int regNum, char inChar[], int charLength, bool forceMultiple)
{
byte inputData[charLength];
charToFrame(inChar, charLength, inputData);
return setRegisters(regNum, charLength/2, inputData, forceMultiple);
}
//----------------------------------------------------------------------------
// MID LEVEL FUNCTIONS
//----------------------------------------------------------------------------
// These functions return a variety of data from an input modbus RTU frame.
// Currently, the only "frame" available is the response buffer.
uint16_t modbusMaster::uint16FromFrame(endianness endian, int start_index)
{return leFrameFromFrame(UINT16_SIZE, endian, start_index).uInt16[0];}
int16_t modbusMaster::int16FromFrame(endianness endian, int start_index)
{return leFrameFromFrame(INT16_SIZE, endian, start_index).Int16[0];}
float modbusMaster::float32FromFrame(endianness endian, int start_index)
{return leFrameFromFrame(FLOAT32_SIZE, endian, start_index).Float32;}
uint32_t modbusMaster::uint32FromFrame(endianness endian, int start_index)
{return leFrameFromFrame(UINT32_SIZE, endian, start_index).uInt32;}
int32_t modbusMaster::int32FromFrame(endianness endian, int start_index)
{return leFrameFromFrame(INT32_SIZE, endian, start_index).Int32;}
uint32_t modbusMaster::TAI64FromFrame(int start_index)
{return leFrameFromFrame(4, bigEndian, start_index + 4).uInt32;}
uint32_t modbusMaster::TAI64NFromFrame(uint32_t &nanoseconds, int start_index)
{
nanoseconds = leFrameFromFrame(4, bigEndian, start_index + 8).uInt32;
return leFrameFromFrame(4, bigEndian, start_index + 4).uInt32;
}
uint32_t modbusMaster::TAI64NAFromFrame(uint32_t &nanoseconds, uint32_t &attoseconds, int start_index)
{
attoseconds = leFrameFromFrame(4, bigEndian, start_index + 12).uInt32;
nanoseconds = leFrameFromFrame(4, bigEndian, start_index + 8).uInt32;
return leFrameFromFrame(4, bigEndian, start_index + 4).uInt32;
}
byte modbusMaster::byteFromFrame(int start_index)
{return responseBuffer[start_index];}
uint16_t modbusMaster::pointerFromFrame(endianness endian, int start_index)
{
leFrame fram;
if (endian == bigEndian)
{
fram.Byte[0] = responseBuffer[start_index + 1]>>2; // Shift the lower address bit DOWN two
fram.Byte[1] = responseBuffer[start_index];
}
else
{
fram.Byte[0] = responseBuffer[start_index]>>2; // Shift the lower address bit DOWN two
fram.Byte[1] = responseBuffer[start_index + 1];
}
return fram.Int16[0];
}
int8_t modbusMaster::pointerTypeFromFrame(endianness endian, int start_index)
{
uint8_t pointerRegType;
// Mask to get the last two bits, which are the type
if (endian == bigEndian) pointerRegType = responseBuffer[start_index + 1] & 3;
else pointerRegType = responseBuffer[start_index] & 3;
return pointerRegType;
}
String modbusMaster::StringFromFrame(int charLength, int start_index)
{
char charString[charLength+1];
int j = 0;
for (int i = start_index; i < start_index + charLength; i++)
{
// check that it's a printable character
if (responseBuffer[i] >= 0x20 && responseBuffer[i] <= 0x7E)
{
charString[j] = responseBuffer[i]; // converts from "byte" type to "char" type
j++;
}
}
if (j < charLength+1) for (int i = j; i < + charLength+1; i++) charString[j] = '\0';
String string = String(charString);
string.trim();
return string;
}
void modbusMaster::charFromFrame(char outChar[], int charLength, int start_index)
{
int j = 0;
for (int i = start_index; i < start_index + charLength; i++)
{
// check that it's a printable character
if (responseBuffer[i] >= 0x20 && responseBuffer[i] <= 0x7E)
{
outChar[j] = responseBuffer[i]; // converts from "byte" type to "char" type
j++;
}
}
if (j < charLength) for (int i = j; i < + charLength; i++) outChar[j] = '\0';
}
// These insert values into a longer modbus data frame.
void modbusMaster::uint16ToFrame(uint16_t value, endianness endian, byte modbusFrame[], int start_index)
{
leFrame fram;
fram.uInt16[0] = value;
int end_index = (start_index + UINT16_SIZE) - 1;
for (int i = 0; i < UINT16_SIZE; i++)
{
if (endian == bigEndian)
modbusFrame[end_index - i] = fram.Byte[i];
else modbusFrame[start_index + i] = fram.Byte[i];
}
}
void modbusMaster::int16ToFrame(int16_t value, endianness endian, byte modbusFrame[], int start_index)
{
leFrame fram;
fram.Int16[0] = value;
int end_index = (start_index + INT16_SIZE) - 1;
for (int i = 0; i < INT16_SIZE; i++)
{
if (endian == bigEndian)
modbusFrame[end_index - i] = fram.Byte[i];
else modbusFrame[start_index + i] = fram.Byte[i];
}
}
void modbusMaster::float32ToFrame(float value, endianness endian, byte modbusFrame[], int start_index)
{
leFrame fram;
fram.Float32 = value;
int end_index = (start_index + FLOAT32_SIZE) - 1;
for (int i = 0; i < FLOAT32_SIZE; i++)
{
if (endian == bigEndian)
modbusFrame[end_index - i] = fram.Byte[i];
else modbusFrame[start_index + i] = fram.Byte[i];
}
}
void modbusMaster::uint32ToFrame(uint32_t value, endianness endian, byte modbusFrame[], int start_index)
{
leFrame fram;
fram.uInt32 = value;
int end_index = (start_index + UINT32_SIZE) - 1;
for (int i = 0; i < UINT32_SIZE; i++)
{
if (endian == bigEndian)
modbusFrame[end_index - i] = fram.Byte[i];
else modbusFrame[start_index + i] = fram.Byte[i];
}
}
void modbusMaster::int32ToFrame(int32_t value, endianness endian, byte modbusFrame[], int start_index)
{
leFrame fram;
fram.Int32 = value;
int end_index = (start_index + INT32_SIZE) - 1;
for (int i = 0; i < INT32_SIZE; i++)
{
if (endian == bigEndian)
modbusFrame[end_index - i] = fram.Byte[i];
else modbusFrame[start_index + i] = fram.Byte[i];
}
}
void modbusMaster::TAI64ToFrame(uint32_t seconds, byte modbusFrame[], int start_index)
{
// The first 4 bytes of the 64-bit value will be 0x400000 until the year 2106
modbusFrame[start_index] = 0x40;
leFrame fram;
fram.Int32 = seconds;
// The 32-bit seconds start 4 bytes after the 64-bit value starts
int byte_offset_sec = 4;
int end_index_sec = start_index + byte_offset_sec + UINT32_SIZE - 1;
for (int i = 0; i < UINT32_SIZE; i++)
{
modbusFrame[end_index_sec - i] = fram.Byte[i];
}
}
void modbusMaster::TAI64NToFrame(uint32_t seconds, uint32_t nanoseconds, byte modbusFrame[], int start_index)
{
// The first 4 bytes of the 64-bit value will be 0x400000 until the year 2106
modbusFrame[start_index] = 0x40;
leFrame fram;
fram.Int32 = seconds;
// The 32-bit seconds start 4 bytes after the 64-bit value starts
int byte_offset_sec = 4;
int end_index_sec = start_index + byte_offset_sec + UINT32_SIZE - 1;
for (int i = 0; i < UINT32_SIZE; i++)
{
modbusFrame[end_index_sec - i] = fram.Byte[i];
}
fram.Int32 = nanoseconds;
// The 32-bit nanoseconds start 8 bytes after the 64-bit value starts
int byte_offset_nanosec = 8;
int end_index_nanosec = start_index + byte_offset_nanosec + UINT32_SIZE - 1;
for (int i = 0; i < UINT32_SIZE; i++)
{
modbusFrame[end_index_nanosec - i] = fram.Byte[i];
}
}
void modbusMaster::TAI64NAToFrame(uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds, byte modbusFrame[], int start_index)
{
// The first 4 bytes of the 64-bit value will be 0x400000 until the year 2106
modbusFrame[start_index] = 0x40;
leFrame fram;
fram.Int32 = seconds;
// The 32-bit seconds start 4 bytes after the 64-bit value starts
int byte_offset_sec = 4;
int end_index_sec = start_index + byte_offset_sec + UINT32_SIZE - 1;
for (int i = 0; i < UINT32_SIZE; i++)
{
modbusFrame[end_index_sec - i] = fram.Byte[i];
}
fram.Int32 = nanoseconds;
// The 32-bit nanoseconds start 8 bytes after the 64-bit value starts
int byte_offset_nanosec = 8;
int end_index_nanosec = start_index + byte_offset_nanosec + UINT32_SIZE - 1;
for (int i = 0; i < UINT32_SIZE; i++)
{
modbusFrame[end_index_nanosec - i] = fram.Byte[i];
}
// The 32-bit attoseconds start 12 bytes after the 64-bit value starts
fram.Int32 = attoseconds;
int byte_offset_attosec = 12;
int end_index_attosec = start_index + byte_offset_attosec + UINT32_SIZE - 1;
for (int i = 0; i < UINT32_SIZE; i++)
{
modbusFrame[end_index_attosec - i] = fram.Byte[i];
}
}
void modbusMaster::byteToFrame(byte value, int byteNum, byte modbusFrame[], int start_index)
{
if (byteNum == 1) modbusFrame[start_index] = value;
else modbusFrame[start_index+1] = value;
}
void modbusMaster::pointerToFrame(uint16_t value, pointerType point, endianness endian, byte modbusFrame[], int start_index)
{
leFrame fram;
fram.uInt16[0] = value;
if (endian == bigEndian)
{
modbusFrame[start_index + 1] = fram.Byte[0]<<2; // Shift the lower address bit UP two
modbusFrame[start_index + 1] |= point;
modbusFrame[start_index] = fram.Byte[1];
}
else
{
modbusFrame[start_index] = fram.Byte[0]<<2; // Shift the lower address bit UP two
modbusFrame[start_index] |= point;
modbusFrame[start_index + 1] = fram.Byte[1];
}
}
void modbusMaster::StringToFrame(String value, byte modbusFrame[], int start_index)
{
int charLength = value.length();
char charString[charLength];
value.toCharArray(charString, charLength);
int j = 0;
for (int i = 0; i < charLength; i++)
{
modbusFrame[start_index + j] = charString[i]; // converts from "char" type to "byte" type
j++;
}
}
void modbusMaster::charToFrame(char inChar[], int charLength, byte modbusFrame[], int start_index)
{
int j = 0;
for (int i = 0; i < charLength; i++)
{
modbusFrame[start_index + j] = inChar[i]; // converts from "char" type to "byte" type
j++;
}
}
//----------------------------------------------------------------------------
// LOW LEVEL FUNCTIONS
//----------------------------------------------------------------------------
// This gets data from either a holding or input register
// For a holding register readCommand = 0x03
// For an input register readCommand = 0x04
bool modbusMaster::getRegisters(byte readCommand, int16_t startRegister, int16_t numRegisters)
{
// Create an array for the command
byte command[8];
// Put in the slave id and the command
command[0] = _slaveID;
command[1] = readCommand;
// Put in the starting register
leFrame fram = {{0,}};
fram.Int16[0] = startRegister;
command[2] = fram.Byte[1];
command[3] = fram.Byte[0];
// Put in the number of registers
fram.Int16[1] = numRegisters;
command[4] = fram.Byte[3];
command[5] = fram.Byte[2];
// The size of the returned frame should be:
// # Registers X 2 bytes/register + 5 bytes of modbus RTU frame
// Try up to 10 times to get the right results
int tries = 0;
int16_t respSize = 0;
while ((respSize != (numRegisters*2 + 5) && tries < 10))
{
// Send out the command (this adds the CRC)
respSize = sendCommand(command, 8);
tries++;
delay(25);
}
if (respSize == (numRegisters*2 + 5))
return true;
else return false;
};
// This sets the value of one or more holding registers
// Modbus commands 0x06 and 0x10 (16)
// The boolean switch to "forceMultiple" will force the command 0x10 (16,
// preset multiple registers) instead of using 0x06 for a single register
bool modbusMaster::setRegisters(int16_t startRegister, int16_t numRegisters,
byte value[], bool forceMultiple)
{
// figure out how long the command will be
int commandLength;
if (numRegisters > 1 or forceMultiple) commandLength = numRegisters*2 + 9;
else commandLength = numRegisters*2 + 6;
// Create an array for the command
byte command[commandLength];
// Put in the slave id and the command
command[0] = _slaveID;
if (numRegisters > 1 or forceMultiple) command[1] = 0x10;
else command[1] = 0x06;
// Put in the starting register
leFrame fram = {{0,}};
fram.Int16[0] = startRegister;
command[2] = fram.Byte[1];
command[3] = fram.Byte[0];
// Put in the register values
// For multiple registers, need to add in how many registers and how many bytes
if (numRegisters > 1 or forceMultiple)
{
// Put in the number of registers
fram.Int16[1] = numRegisters;
command[4] = fram.Byte[3];
command[5] = fram.Byte[2];
// Put in the number of bytes to write
command[6] = numRegisters*2;
// Put in the data, allowing 7 extra spaces for the modbus frame structure
for (int i = 7; i < numRegisters*2 + 7; i++) command[i] = value[i-7];
}
// For a single register, only need the data itself
else
{
// Put in the data, allowing 4 extra spaces for the modbus frame structure
for (int i = 4; i < numRegisters*2 + 4; i++) command[i] = value[i-4];
}
// Try up to 10 times to get the right results
int tries = 0;
int16_t respSize = 0;
bool success = false;
while ((!success && tries < 10))
{
// Send out the command (this adds the CRC)
respSize = sendCommand(command, commandLength);
// The structure of the response for 0x10 should be:
// {slaveID, fxnCode, Address of 1st register, # Registers, CRC}
if ((numRegisters > 1 or forceMultiple) && respSize == 8 && int16FromFrame(bigEndian, 4) == numRegisters)
success = true;
// The structure of the response for 0x06 should be:
// {slaveID, fxnCode, Address of 1st register, Value written, CRC}
if (numRegisters == 1 && respSize == 8 && responseBuffer[4] == value[0]
&& responseBuffer[5] == value[1])
success = true;
tries++;
delay(25);
}
return success;
};
//----------------------------------------------------------------------------
// LOWEST LEVEL FUNCTION
//----------------------------------------------------------------------------
// This sends a command to the sensor bus and listens for a response
int modbusMaster::sendCommand(byte command[], int commandLength)
{
// Empty the response buffer
for (int i = 0; i < RESPONSE_BUFFER_SIZE; i++)
modbusMaster::responseBuffer[i] = 0x00;
// Add the CRC to the frame
insertCRC(command, commandLength);
// Send out the command
driverEnable();
emptySerialBuffer(_stream); // Clear any junk before sending command
_stream->write(command, commandLength);
_stream->flush();
recieverEnable();
// Print the raw send (for debugging)
_debugStream->print("Raw Request >>> ");
printFrameHex(command, commandLength);
// Wait for a response
uint32_t start = millis();
while (_stream->available() == 0 && millis() - start < modbusTimeout)
{ delay(1);}
if (_stream->available() > 0)
{
// Read the incoming bytes
int bytesRead = _stream->readBytes(responseBuffer, 135);
emptySerialBuffer(_stream);
// Print the raw response (for debugging)
_debugStream->print("Raw Response (");
_debugStream->print(bytesRead);
_debugStream->print(" bytes) <<< ");
printFrameHex(responseBuffer, bytesRead);
// Verify that the values match with the commands
if (responseBuffer[0] != _slaveID)
{
_debugStream->println("Response is not from the correct modbus slave!");
return 0;
}
// Verify that the CRC is correct
calculateCRC(responseBuffer, bytesRead);
if (crcFrame[0] != responseBuffer[bytesRead-2] || crcFrame[1] != responseBuffer[bytesRead-1])
{
_debugStream->println("CRC of response is not correct!");
return 0;
}
// Check for exception response
// An execption response sets the highest bit of the function code in the response.
if ((responseBuffer[1] & 0b10000000) == 0b10000000)
{
_debugStream->print("Exception: ");
if (responseBuffer[2] == 0x01)_debugStream->println("Illegal Function!");
if (responseBuffer[2] == 0x02)_debugStream->println("Illegal Data Address!");
if (responseBuffer[2] == 0x03)_debugStream->println("Illegal Data Value!");
if (responseBuffer[2] == 0x04)_debugStream->println("Slave Device Failure!");
if (responseBuffer[2] == 0x05)_debugStream->println("Acknowledge...");
if (responseBuffer[2] == 0x06)_debugStream->println("Slave Device Busy!");
if (responseBuffer[2] == 0x07)_debugStream->println("Negative Acknowledge!");
if (responseBuffer[2] == 0x08)_debugStream->println("Memory Parity Error!");
if (responseBuffer[2] == 0x0A)_debugStream->println("Gateway Path Unavailable!");
if (responseBuffer[2] == 0x0B)_debugStream->println("Gateway Target Device Failed to Respond!");
return 0;
}
// If everything passes, return the number of bytes
return bytesRead;
}
else
{
_debugStream->println("No response received.");
return 0;
}
}
//----------------------------------------------------------------------------
// PRIVATE HELPER FUNCTIONS
//----------------------------------------------------------------------------
// This flips the device/receive enable to DRIVER so the arduino can send text
void modbusMaster::driverEnable(void)
{
if (_enablePin >= 0)
{
digitalWrite(_enablePin, HIGH);
_debugStream->println("RS485 Driver/Master Tx Enabled");
delay(8);
}
}
// This flips the device/receive enable to RECIEVER so the sensor can send text
void modbusMaster::recieverEnable(void)
{
if (_enablePin >= 0)
{
digitalWrite(_enablePin, LOW);
_debugStream->println("RS485 Receiver/Slave Tx Enabled");
// delay(8);
}
}
// This empties the serial buffer
void modbusMaster::emptySerialBuffer(Stream *stream)
{
while (stream->available() > 0)
{
stream->read();
delay(1);
}
}
// Just a function to pretty-print the modbus hex frames
// This is purely for debugging
void modbusMaster::printFrameHex(byte modbusFrame[], int frameLength)
{
_debugStream->print("{");
for (int i = 0; i < frameLength; i++)
{
_debugStream->print("0x");
if (modbusFrame[i] < 16) _debugStream->print("0");
_debugStream->print(modbusFrame[i], HEX);
if (i < frameLength - 1) _debugStream->print(", ");
}
_debugStream->println("}");
}
// Calculates a Modbus RTC cyclical redudancy code (CRC)
// and adds it to the last two bytes of a frame
// From: https://ctlsys.com/support/how_to_compute_the_modbus_rtu_message_crc/
// and: https://stackoverflow.com/questions/19347685/calculating-modbus-rtu-crc-16
//
void modbusMaster::calculateCRC(byte modbusFrame[], int frameLength)
{
// Reset the CRC frame
modbusMaster::crcFrame[0] = {0x00};
modbusMaster::crcFrame[1] = {0x00};
uint16_t crc = 0xFFFF;
for (int pos = 0; pos < frameLength - 2; pos++)
{
crc ^= (unsigned int)modbusFrame[pos]; // XOR byte into least sig. byte of crc
for (int i = 8; i != 0; i--) { // Loop over each bit
if ((crc & 0x0001) != 0) { // If the least significant bit (LSB) is set
crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
}
else // Else least significant bit (LSB) is not set
crc >>= 1; // Just shift right
}
}
// Break into low and high bytes
byte crcLow = crc & 0xFF;
byte crcHigh = crc >> 8;
// Append the bytes to the end of the frame
crcFrame[0] = crcLow;
crcFrame[1] = crcHigh;
}
void modbusMaster::insertCRC(byte modbusFrame[], int frameLength)
{
// Calculate the CRC
calculateCRC(modbusFrame, frameLength);
// Append the bytes to the end of the frame
modbusFrame[frameLength - 2] = crcFrame[0];
modbusFrame[frameLength - 1] = crcFrame[1];
}
// This slices one array out of another
// Used for slicing one or more registers out of a returned modbus RTU frame
void modbusMaster::sliceArray(byte inputArray[], byte outputArray[],
int start_index, int numBytes, bool reverseOrder)
{
if (reverseOrder)
{
// Reverse the order of bytes to get from big-endian to little-endian
int j = numBytes - 1;
for (int i = 0; i < numBytes; i++)
{
outputArray[i] = inputArray[start_index + j];
j--;
}
}
else
{
for (int i = 0; i < numBytes; i++)
outputArray[i] = inputArray[start_index + i];
}
}
// This converts data in a modbus RTU frame into a little-endian data frame
// little-endian data frames are needed because all Arduino processors are little-endian
leFrame modbusMaster::leFrameFromFrame(int varBytes,
endianness endian,
int start_index)
{
// Set up a temporary output frame
byte outFrame[varBytes];
// Slice data from the full response frame into the temporary output frame
if (endian == bigEndian)
sliceArray(responseBuffer, outFrame, start_index, varBytes, true);
else sliceArray(responseBuffer, outFrame, start_index, varBytes, false);
// Put it into a little-endian frame (the format of all arduino processors)
leFrame fram = {{0,}};
memcpy(fram.Byte, outFrame, varBytes);
// Return the little-endian frame
return fram;
}