forked from smarmengol/Modbus-Master-Slave-for-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ModbusRtu.h
1438 lines (1284 loc) · 37.7 KB
/
ModbusRtu.h
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file ModbusRtu.h
* @version 1.21
* @date 2016.02.21
* @author Samuel Marco i Armengol
* @contact [email protected]
* @contribution Helium6072
*
* @description
* Arduino library for communicating with Modbus devices
* over RS232/USB/485 via RTU protocol.
*
* Further information:
* http://modbus.org/
* http://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
*
* @license
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version
* 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @defgroup setup Modbus Object Instantiation/Initialization
* @defgroup loop Modbus Object Management
* @defgroup buffer Modbus Buffer Management
* @defgroup discrete Modbus Function Codes for Discrete Coils/Inputs
* @defgroup register Modbus Function Codes for Holding/Input Registers
*
*/
#ifndef ModbusRtu_H_
#define ModbusRtu_H_
#include <inttypes.h>
#include "Arduino.h"
#include "Print.h"
#include <SoftwareSerial.h>
/**
* @struct modbus_t
* @brief
* Master query structure:
* This includes all the necessary fields to make the Master generate a Modbus query.
* A Master may keep several of these structures and send them cyclically or
* use them according to program needs.
*/
typedef struct
{
uint8_t u8id; /*!< Slave address between 1 and 247. 0 means broadcast */
uint8_t u8fct; /*!< Function code: 1, 2, 3, 4, 5, 6, 15 or 16 */
uint16_t u16RegAdd; /*!< Address of the first register to access at slave/s */
uint16_t u16CoilsNo; /*!< Number of coils or registers to access */
uint16_t *au16reg; /*!< Pointer to memory image in master */
}
modbus_t;
enum
{
RESPONSE_SIZE = 6,
EXCEPTION_SIZE = 3,
CHECKSUM_SIZE = 2
};
/**
* @enum MESSAGE
* @brief
* Indexes to telegram frame positions
*/
enum MESSAGE
{
ID = 0, //!< ID field
FUNC, //!< Function code position
ADD_HI, //!< Address high byte
ADD_LO, //!< Address low byte
NB_HI, //!< Number of coils or registers high byte
NB_LO, //!< Number of coils or registers low byte
BYTE_CNT //!< byte counter
};
/**
* @enum MB_FC
* @brief
* Modbus function codes summary.
* These are the implement function codes either for Master or for Slave.
*
* @see also fctsupported
* @see also modbus_t
*/
enum MB_FC
{
MB_FC_NONE = 0, /*!< null operator */
MB_FC_READ_COILS = 1, /*!< FCT=1 -> read coils or digital outputs */
MB_FC_READ_DISCRETE_INPUT = 2, /*!< FCT=2 -> read digital inputs */
MB_FC_READ_REGISTERS = 3, /*!< FCT=3 -> read registers or analog outputs */
MB_FC_READ_INPUT_REGISTER = 4, /*!< FCT=4 -> read analog inputs */
MB_FC_WRITE_COIL = 5, /*!< FCT=5 -> write single coil or output */
MB_FC_WRITE_REGISTER = 6, /*!< FCT=6 -> write single register */
MB_FC_WRITE_MULTIPLE_COILS = 15, /*!< FCT=15 -> write multiple coils or outputs */
MB_FC_WRITE_MULTIPLE_REGISTERS = 16 /*!< FCT=16 -> write multiple registers */
};
enum COM_STATES
{
COM_IDLE = 0,
COM_WAITING = 1
};
enum ERR_LIST
{
ERR_NOT_MASTER = -1,
ERR_POLLING = -2,
ERR_BUFF_OVERFLOW = -3,
ERR_BAD_CRC = -4,
ERR_EXCEPTION = -5
};
enum
{
NO_REPLY = 255,
EXC_FUNC_CODE = 1,
EXC_ADDR_RANGE = 2,
EXC_REGS_QUANT = 3,
EXC_EXECUTE = 4
};
const unsigned char fctsupported[] =
{
MB_FC_READ_COILS,
MB_FC_READ_DISCRETE_INPUT,
MB_FC_READ_REGISTERS,
MB_FC_READ_INPUT_REGISTER,
MB_FC_WRITE_COIL,
MB_FC_WRITE_REGISTER,
MB_FC_WRITE_MULTIPLE_COILS,
MB_FC_WRITE_MULTIPLE_REGISTERS
};
#define T35 5
#define MAX_BUFFER 64 //!< maximum size for the communication buffer in bytes
/**
* @class Modbus
* @brief
* Arduino class library for communicating with Modbus devices over
* USB/RS232/485 (via RTU protocol).
*/
class Modbus
{
private:
HardwareSerial *port; //!< Pointer to Serial class object
SoftwareSerial *softPort; //!< Pointer to SoftwareSerial class object
uint8_t u8id; //!< 0=master, 1..247=slave number
uint8_t u8serno; //!< serial port: 0-Serial, 1..3-Serial1..Serial3; 4: use software serial
uint8_t u8txenpin; //!< flow control pin: 0=USB or RS-232 mode, >0=RS-485 mode
uint8_t u8state;
uint8_t u8lastError;
uint8_t au8Buffer[MAX_BUFFER];
uint8_t u8BufferSize;
uint8_t u8lastRec;
uint16_t *au16regs;
uint16_t u16InCnt, u16OutCnt, u16errCnt;
uint16_t u16timeOut;
uint32_t u32time, u32timeOut;
uint8_t u8regsize;
void init(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin);
void init(uint8_t u8id);
void sendTxBuffer();
int8_t getRxBuffer();
uint16_t calcCRC(uint8_t u8length);
uint8_t validateAnswer();
uint8_t validateRequest();
void get_FC1();
void get_FC3();
int8_t process_FC1( uint16_t *regs, uint8_t u8size );
int8_t process_FC3( uint16_t *regs, uint8_t u8size );
int8_t process_FC5( uint16_t *regs, uint8_t u8size );
int8_t process_FC6( uint16_t *regs, uint8_t u8size );
int8_t process_FC15( uint16_t *regs, uint8_t u8size );
int8_t process_FC16( uint16_t *regs, uint8_t u8size );
void buildException( uint8_t u8exception ); // build exception message
public:
Modbus();
Modbus(uint8_t u8id, uint8_t u8serno);
Modbus(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin);
Modbus(uint8_t u8id);
void begin(long u32speed);
void begin(SoftwareSerial *sPort, long u32speed);
void begin(long u32speed, uint8_t u8config);
void begin();
void setTimeOut( uint16_t u16timeout); //!<write communication watch-dog timer
uint16_t getTimeOut(); //!<get communication watch-dog timer value
boolean getTimeOutState(); //!<get communication watch-dog timer state
int8_t query( modbus_t telegram ); //!<only for master
int8_t poll(); //!<cyclic poll for master
int8_t poll( uint16_t *regs, uint8_t u8size ); //!<cyclic poll for slave
uint16_t getInCnt(); //!<number of incoming messages
uint16_t getOutCnt(); //!<number of outcoming messages
uint16_t getErrCnt(); //!<error counter
uint8_t getID(); //!<get slave ID between 1 and 247
uint8_t getState();
uint8_t getLastError(); //!<get last error message
void setID( uint8_t u8id ); //!<write new ID for the slave
void end(); //!<finish any communication and release serial communication port
};
/* _____PUBLIC FUNCTIONS_____________________________________________________ */
/**
* @brief
* Default Constructor for Master through Serial
*
* @ingroup setup
*/
Modbus::Modbus()
{
init(0, 0, 0);
}
/**
* @brief
* Full constructor for a Master/Slave through USB/RS232C
*
* @param u8id node address 0=master, 1..247=slave
* @param u8serno serial port used 0..3
* @ingroup setup
* @overload Modbus::Modbus(uint8_t u8id, uint8_t u8serno)
* @overload Modbus::Modbus(uint8_t u8id)
* @overload Modbus::Modbus()
*/
Modbus::Modbus(uint8_t u8id, uint8_t u8serno)
{
init(u8id, u8serno, 0);
}
/**
* @brief
* Full constructor for a Master/Slave through USB/RS232C/RS485
* It needs a pin for flow control only for RS485 mode
*
* @param u8id node address 0=master, 1..247=slave
* @param u8serno serial port used 0..3
* @param u8txenpin pin for txen RS-485 (=0 means USB/RS232C mode)
* @ingroup setup
* @overload Modbus::Modbus(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
* @overload Modbus::Modbus(uint8_t u8id)
* @overload Modbus::Modbus()
*/
Modbus::Modbus(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
{
init(u8id, u8serno, u8txenpin);
}
/**
* @brief
* Constructor for a Master/Slave through USB/RS232C via software serial
* This constructor only specifies u8id (node address) and should be only
* used if you want to use software serial instead of hardware serial.
* If you use this constructor you have to begin ModBus object by
* using "void Modbus::begin(SoftwareSerial *softPort, long u32speed)".
*
* @param u8id node address 0=master, 1..247=slave
* @ingroup setup
* @overload Modbus::Modbus(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
* @overload Modbus::Modbus(uint8_t u8id, uint8_t u8serno)
* @overload Modbus::Modbus()
*/
Modbus::Modbus(uint8_t u8id)
{
init(u8id);
}
/**
* @brief
* Initialize class object.
*
* Sets up the serial port using specified baud rate.
* Call once class has been instantiated, typically within setup().
*
* @see http://arduino.cc/en/Serial/Begin#.Uy4CJ6aKlHY
* @param speed baud rate, in standard increments (300..115200)
* @ingroup setup
*/
void Modbus::begin(long u32speed)
{
switch( u8serno )
{
#if defined(UBRR1H)
case 1:
port = &Serial1;
break;
#endif
#if defined(UBRR2H)
case 2:
port = &Serial2;
break;
#endif
#if defined(UBRR3H)
case 3:
port = &Serial3;
break;
#endif
case 0:
default:
#if defined(SERIAL_PORT_HARDWARE)
port = &SERIAL_PORT_HARDWARE;
#else
port = &Serial;
#endif
break;
}
port->begin(u32speed);
if (u8txenpin > 1) // pin 0 & pin 1 are reserved for RX/TX
{
// return RS485 transceiver to transmit mode
pinMode(u8txenpin, OUTPUT);
digitalWrite(u8txenpin, LOW);
#if defined(TEENSYDUINO)
port->transmitterEnable(u8txenpin);
#endif
}
while(port->read() >= 0);
u8lastRec = u8BufferSize = 0;
u16InCnt = u16OutCnt = u16errCnt = 0;
}
/**
* @brief
* Initialize class object.
*
* Sets up the software serial port using specified baud rate and SoftwareSerial object.
* Call once class has been instantiated, typically within setup().
*
* @param speed *softPort, pointer to SoftwareSerial class object
* @param speed baud rate, in standard increments (300..115200)
* @ingroup setup
*/
void Modbus::begin(SoftwareSerial *sPort, long u32speed)
{
softPort=sPort;
softPort->begin(u32speed);
if (u8txenpin > 1) // pin 0 & pin 1 are reserved for RX/TX
{
// return RS485 transceiver to transmit mode
pinMode(u8txenpin, OUTPUT);
digitalWrite(u8txenpin, LOW);
}
while(softPort->read() >= 0);
u8lastRec = u8BufferSize = 0;
u16InCnt = u16OutCnt = u16errCnt = 0;
}
/**
* @brief
* Initialize class object.
*
* Sets up the serial port using specified baud rate.
* Call once class has been instantiated, typically within setup().
*
* @see http://arduino.cc/en/Serial/Begin#.Uy4CJ6aKlHY
* @param speed baud rate, in standard increments (300..115200)
* @param config data frame settings (data length, parity and stop bits)
* @ingroup setup
*/
void Modbus::begin(long u32speed,uint8_t u8config)
{
switch( u8serno )
{
#if defined(UBRR1H)
case 1:
port = &Serial1;
break;
#endif
#if defined(UBRR2H)
case 2:
port = &Serial2;
break;
#endif
#if defined(UBRR3H)
case 3:
port = &Serial3;
break;
#endif
case 0:
default:
#if defined(SERIAL_PORT_HARDWARE)
port = &SERIAL_PORT_HARDWARE;
#else
port = &Serial;
#endif
break;
}
port->begin(u32speed, u8config);
if (u8txenpin > 1) // pin 0 & pin 1 are reserved for RX/TX
{
// return RS485 transceiver to transmit mode
pinMode(u8txenpin, OUTPUT);
digitalWrite(u8txenpin, LOW);
}
while(port->read() >= 0);
u8lastRec = u8BufferSize = 0;
u16InCnt = u16OutCnt = u16errCnt = 0;
}
/**
* @brief
* Initialize default class object.
*
* Sets up the serial port using 19200 baud.
* Call once class has been instantiated, typically within setup().
*
* @overload Modbus::begin(uint16_t u16BaudRate)
* @ingroup setup
*/
void Modbus::begin()
{
begin(19200);
}
/**
* @brief
* Method to write a new slave ID address
*
* @param u8id new slave address between 1 and 247
* @ingroup setup
*/
void Modbus::setID( uint8_t u8id)
{
if (( u8id != 0) && (u8id <= 247))
{
this->u8id = u8id;
}
}
/**
* @brief
* Method to read current slave ID address
*
* @return u8id current slave address between 1 and 247
* @ingroup setup
*/
uint8_t Modbus::getID()
{
return this->u8id;
}
/**
* @brief
* Initialize time-out parameter
*
* Call once class has been instantiated, typically within setup().
* The time-out timer is reset each time that there is a successful communication
* between Master and Slave. It works for both.
*
* @param time-out value (ms)
* @ingroup setup
*/
void Modbus::setTimeOut( uint16_t u16timeOut)
{
this->u16timeOut = u16timeOut;
}
/**
* @brief
* Return communication Watchdog state.
* It could be usefull to reset outputs if the watchdog is fired.
*
* @return TRUE if millis() > u32timeOut
* @ingroup loop
*/
boolean Modbus::getTimeOutState()
{
return (millis() > u32timeOut);
}
/**
* @brief
* Get input messages counter value
* This can be useful to diagnose communication
*
* @return input messages counter
* @ingroup buffer
*/
uint16_t Modbus::getInCnt()
{
return u16InCnt;
}
/**
* @brief
* Get transmitted messages counter value
* This can be useful to diagnose communication
*
* @return transmitted messages counter
* @ingroup buffer
*/
uint16_t Modbus::getOutCnt()
{
return u16OutCnt;
}
/**
* @brief
* Get errors counter value
* This can be useful to diagnose communication
*
* @return errors counter
* @ingroup buffer
*/
uint16_t Modbus::getErrCnt()
{
return u16errCnt;
}
/**
* Get modbus master state
*
* @return = 0 IDLE, = 1 WAITING FOR ANSWER
* @ingroup buffer
*/
uint8_t Modbus::getState()
{
return u8state;
}
/**
* Get the last error in the protocol processor
*
* @returnreturn NO_REPLY = 255 Time-out
* @return EXC_FUNC_CODE = 1 Function code not available
* @return EXC_ADDR_RANGE = 2 Address beyond available space for Modbus registers
* @return EXC_REGS_QUANT = 3 Coils or registers number beyond the available space
* @ingroup buffer
*/
uint8_t Modbus::getLastError()
{
return u8lastError;
}
/**
* @brief
* *** Only Modbus Master ***
* Generate a query to an slave with a modbus_t telegram structure
* The Master must be in COM_IDLE mode. After it, its state would be COM_WAITING.
* This method has to be called only in loop() section.
*
* @see modbus_t
* @param modbus_t modbus telegram structure (id, fct, ...)
* @ingroup loop
* @todo finish function 15
*/
int8_t Modbus::query( modbus_t telegram )
{
uint8_t u8regsno, u8bytesno;
if (u8id!=0) return -2;
if (u8state != COM_IDLE) return -1;
if ((telegram.u8id==0) || (telegram.u8id>247)) return -3;
au16regs = telegram.au16reg;
// telegram header
au8Buffer[ ID ] = telegram.u8id;
au8Buffer[ FUNC ] = telegram.u8fct;
au8Buffer[ ADD_HI ] = highByte(telegram.u16RegAdd );
au8Buffer[ ADD_LO ] = lowByte( telegram.u16RegAdd );
switch( telegram.u8fct )
{
case MB_FC_READ_COILS:
case MB_FC_READ_DISCRETE_INPUT:
case MB_FC_READ_REGISTERS:
case MB_FC_READ_INPUT_REGISTER:
au8Buffer[ NB_HI ] = highByte(telegram.u16CoilsNo );
au8Buffer[ NB_LO ] = lowByte( telegram.u16CoilsNo );
u8BufferSize = 6;
break;
case MB_FC_WRITE_COIL:
au8Buffer[ NB_HI ] = ((au16regs[0] > 0) ? 0xff : 0);
au8Buffer[ NB_LO ] = 0;
u8BufferSize = 6;
break;
case MB_FC_WRITE_REGISTER:
au8Buffer[ NB_HI ] = highByte(au16regs[0]);
au8Buffer[ NB_LO ] = lowByte(au16regs[0]);
u8BufferSize = 6;
break;
case MB_FC_WRITE_MULTIPLE_COILS: // TODO: implement "sending coils"
u8regsno = telegram.u16CoilsNo / 16;
u8bytesno = u8regsno * 2;
if ((telegram.u16CoilsNo % 16) != 0)
{
u8bytesno++;
u8regsno++;
}
au8Buffer[ NB_HI ] = highByte(telegram.u16CoilsNo );
au8Buffer[ NB_LO ] = lowByte( telegram.u16CoilsNo );
au8Buffer[ NB_LO+1 ] = u8bytesno;
u8BufferSize = 7;
u8regsno = u8bytesno = 0; // now auxiliary registers
for (uint16_t i = 0; i < telegram.u16CoilsNo; i++)
{
}
break;
case MB_FC_WRITE_MULTIPLE_REGISTERS:
au8Buffer[ NB_HI ] = highByte(telegram.u16CoilsNo );
au8Buffer[ NB_LO ] = lowByte( telegram.u16CoilsNo );
au8Buffer[ NB_LO+1 ] = (uint8_t) ( telegram.u16CoilsNo * 2 );
u8BufferSize = 7;
for (uint16_t i=0; i< telegram.u16CoilsNo; i++)
{
au8Buffer[ u8BufferSize ] = highByte( au16regs[ i ] );
u8BufferSize++;
au8Buffer[ u8BufferSize ] = lowByte( au16regs[ i ] );
u8BufferSize++;
}
break;
}
sendTxBuffer();
u8state = COM_WAITING;
return 0;
}
/**
* @brief *** Only for Modbus Master ***
* This method checks if there is any incoming answer if pending.
* If there is no answer, it would change Master state to COM_IDLE.
* This method must be called only at loop section.
* Avoid any delay() function.
*
* Any incoming data would be redirected to au16regs pointer,
* as defined in its modbus_t query telegram.
*
* @params nothing
* @return errors counter
* @ingroup loop
*/
int8_t Modbus::poll()
{
// check if there is any incoming frame
uint8_t u8current;
if(u8serno<4)
u8current = port->available();
else
u8current = softPort->available();
if (millis() > u32timeOut)
{
u8state = COM_IDLE;
u8lastError = NO_REPLY;
u16errCnt++;
return 0;
}
if (u8current == 0) return 0;
// check T35 after frame end or still no frame end
if (u8current != u8lastRec)
{
u8lastRec = u8current;
u32time = millis() + T35;
return 0;
}
if (millis() < u32time) return 0;
// transfer Serial buffer frame to auBuffer
u8lastRec = 0;
int8_t i8state = getRxBuffer();
if (i8state < 7)
{
u8state = COM_IDLE;
u16errCnt++;
return i8state;
}
// validate message: id, CRC, FCT, exception
uint8_t u8exception = validateAnswer();
if (u8exception != 0)
{
u8state = COM_IDLE;
return u8exception;
}
// process answer
switch( au8Buffer[ FUNC ] )
{
case MB_FC_READ_COILS:
case MB_FC_READ_DISCRETE_INPUT:
// call get_FC1 to transfer the incoming message to au16regs buffer
get_FC1( );
break;
case MB_FC_READ_INPUT_REGISTER:
case MB_FC_READ_REGISTERS :
// call get_FC3 to transfer the incoming message to au16regs buffer
get_FC3( );
break;
case MB_FC_WRITE_COIL:
case MB_FC_WRITE_REGISTER :
case MB_FC_WRITE_MULTIPLE_COILS:
case MB_FC_WRITE_MULTIPLE_REGISTERS :
// nothing to do
break;
default:
break;
}
u8state = COM_IDLE;
return u8BufferSize;
}
/**
* @brief
* *** Only for Modbus Slave ***
* This method checks if there is any incoming query
* Afterwards, it would shoot a validation routine plus a register query
* Avoid any delay() function !!!!
* After a successful frame between the Master and the Slave, the time-out timer is reset.
*
* @param *regs register table for communication exchange
* @param u8size size of the register table
* @return 0 if no query, 1..4 if communication error, >4 if correct query processed
* @ingroup loop
*/
int8_t Modbus::poll( uint16_t *regs, uint8_t u8size )
{
au16regs = regs;
u8regsize = u8size;
uint8_t u8current;
// check if there is any incoming frame
if(u8serno<4)
u8current = port->available();
else
u8current = softPort->available();
if (u8current == 0) return 0;
// check T35 after frame end or still no frame end
if (u8current != u8lastRec)
{
u8lastRec = u8current;
u32time = millis() + T35;
return 0;
}
if (millis() < u32time) return 0;
u8lastRec = 0;
int8_t i8state = getRxBuffer();
u8lastError = i8state;
if (i8state < 7) return i8state;
// check slave id
if (au8Buffer[ ID ] != u8id) return 0;
// validate message: CRC, FCT, address and size
uint8_t u8exception = validateRequest();
if (u8exception > 0)
{
if (u8exception != NO_REPLY)
{
buildException( u8exception );
sendTxBuffer();
}
u8lastError = u8exception;
return u8exception;
}
u32timeOut = millis() + long(u16timeOut);
u8lastError = 0;
// process message
switch( au8Buffer[ FUNC ] )
{
case MB_FC_READ_COILS:
case MB_FC_READ_DISCRETE_INPUT:
return process_FC1( regs, u8size );
break;
case MB_FC_READ_INPUT_REGISTER:
case MB_FC_READ_REGISTERS :
return process_FC3( regs, u8size );
break;
case MB_FC_WRITE_COIL:
return process_FC5( regs, u8size );
break;
case MB_FC_WRITE_REGISTER :
return process_FC6( regs, u8size );
break;
case MB_FC_WRITE_MULTIPLE_COILS:
return process_FC15( regs, u8size );
break;
case MB_FC_WRITE_MULTIPLE_REGISTERS :
return process_FC16( regs, u8size );
break;
default:
break;
}
return i8state;
}
/* _____PRIVATE FUNCTIONS_____________________________________________________ */
void Modbus::init(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
{
this->u8id = u8id;
this->u8serno = (u8serno > 3) ? 0 : u8serno;
this->u8txenpin = u8txenpin;
this->u16timeOut = 1000;
}
void Modbus::init(uint8_t u8id)
{
this->u8id = u8id;
this->u8serno = 4;
this->u8txenpin = 0;
this->u16timeOut = 1000;
}
/**
* @brief
* This method moves Serial buffer data to the Modbus au8Buffer.
*
* @return buffer size if OK, ERR_BUFF_OVERFLOW if u8BufferSize >= MAX_BUFFER
* @ingroup buffer
*/
int8_t Modbus::getRxBuffer()
{
boolean bBuffOverflow = false;
if (u8txenpin > 1) digitalWrite( u8txenpin, LOW );
u8BufferSize = 0;
if(u8serno<4)
while ( port->available() )
{
au8Buffer[ u8BufferSize ] = port->read();
u8BufferSize ++;
if (u8BufferSize >= MAX_BUFFER) bBuffOverflow = true;
}
else
while ( softPort->available() )
{
au8Buffer[ u8BufferSize ] = softPort->read();
u8BufferSize ++;
if (u8BufferSize >= MAX_BUFFER) bBuffOverflow = true;
}
u16InCnt++;
if (bBuffOverflow)
{
u16errCnt++;
return ERR_BUFF_OVERFLOW;
}
return u8BufferSize;
}
/**
* @brief
* This method transmits au8Buffer to Serial line.
* Only if u8txenpin != 0, there is a flow handling in order to keep
* the RS485 transceiver in output state as long as the message is being sent.
* This is done with UCSRxA register.
* The CRC is appended to the buffer before starting to send it.
*
* @param nothing
* @return nothing
* @ingroup buffer
*/
void Modbus::sendTxBuffer()
{
//uint8_t i = 0; //unused
// append CRC to message
uint16_t u16crc = calcCRC( u8BufferSize );
au8Buffer[ u8BufferSize ] = u16crc >> 8;
u8BufferSize++;
au8Buffer[ u8BufferSize ] = u16crc & 0x00ff;
u8BufferSize++;
// set RS485 transceiver to transmit mode
if (u8txenpin > 1)
{
#if defined(__AVR__)
switch( u8serno )
{
#if defined(UBRR1H)
case 1:
UCSR1A=UCSR1A |(1 << TXC1);
break;
#endif
#if defined(UBRR2H)
case 2:
UCSR2A=UCSR2A |(1 << TXC2);
break;
#endif
#if defined(UBRR3H)
case 3:
UCSR3A=UCSR3A |(1 << TXC3);
break;
#endif
case 0:
default:
UCSR0A=UCSR0A |(1 << TXC0);
break;
}
digitalWrite( u8txenpin, HIGH );
#endif
}
// transfer buffer to serial line
if(u8serno<4)
port->write( au8Buffer, u8BufferSize );
else
softPort->write( au8Buffer, u8BufferSize );
// keep RS485 transceiver in transmit mode as long as sending
if (u8txenpin > 1)
{
#if defined(__AVR__)
switch( u8serno )
{
#if defined(UBRR1H)
case 1:
while (!(UCSR1A & (1 << TXC1)));
break;
#endif
#if defined(UBRR2H)
case 2:
while (!(UCSR2A & (1 << TXC2)));
break;
#endif
#if defined(UBRR3H)
case 3:
while (!(UCSR3A & (1 << TXC3)));
break;
#endif
case 0:
default:
while (!(UCSR0A & (1 << TXC0)));
break;
}
// return RS485 transceiver to receive mode
digitalWrite( u8txenpin, LOW );
#endif
}
if(u8serno<4)
while(port->read() >= 0);
else
while(softPort->read() >= 0);
u8BufferSize = 0;
// set time-out for master
u32timeOut = millis() + (unsigned long) u16timeOut;
// increase message counter
u16OutCnt++;
}
/**
* @brief