Modbus-Arduino
Modbus library for Arduino
Modbus.h
1 /*
2  Modbus.h - Header for Modbus Base Library
3  Copyright (C) 2014 AndrĂ© Sarmento Barbosa
4  Copyright (C) 2023 Pascal JEAN aka epsilonrt
5 */
6 #include "Arduino.h"
7 
8 #pragma once
9 
10 #define MAX_REGS 32 // Unused !
11 #define MAX_FRAME 128 // Unused !
12 //#define USE_HOLDING_REGISTERS_ONLY
13 
14 // Function Codes
15 enum {
16  MB_FC_READ_COILS = 0x01,
17  MB_FC_READ_INPUT_STAT = 0x02,
18  MB_FC_READ_REGS = 0x03,
19  MB_FC_READ_INPUT_REGS = 0x04,
20  MB_FC_WRITE_COIL = 0x05,
21  MB_FC_WRITE_REG = 0x06,
22  MB_FC_WRITE_COILS = 0x0F,
23  MB_FC_WRITE_REGS = 0x10,
24  MB_FC_REPORT_SERVER_ID = 0x11,
25 };
26 
27 // Exception Codes
28 enum {
29  MB_EX_ILLEGAL_FUNCTION = 0x01,
30  MB_EX_ILLEGAL_ADDRESS = 0x02,
31  MB_EX_ILLEGAL_VALUE = 0x03,
32  MB_EX_SLAVE_FAILURE = 0x04,
33 };
34 
35 // Reply Types
36 enum {
37  MB_REPLY_OFF = 0x01,
38  MB_REPLY_ECHO = 0x02,
39  MB_REPLY_NORMAL = 0x03,
40 };
41 
42 #ifndef __DOXYGEN__
43 struct TExtData {
44  word min;
45  word max;
46 };
47 struct TRegister {
48  word address;
49  word value;
50  TExtData *edata;
51  TRegister *next;
52  enum {
53  CoilOffset = 1,
54  IstsOffset = 10001,
55  IregOffset = 30001,
56  HregOffset = 40001
57  };
58 };
59 #endif
60 
65 class Modbus {
66  #ifndef __DOXYGEN__
67  private:
68  TRegister *_regs_head;
69  TRegister *_regs_last;
70  char *_additional_data;
71 
72  void readRegisters (const byte fcode, const word startreg, const word numregs);
73  void writeSingleRegister (const word reg, const word value);
74  void writeMultipleRegisters (const byte *frame, const word startreg, const word numoutputs, const byte bytecount);
75  void exceptionResponse (const byte fcode, const byte excode);
76 
77  #ifndef USE_HOLDING_REGISTERS_ONLY
78  void readBits (const byte fcode, const word startreg, const word numregs);
79  void writeSingleCoil (const word reg, const word status);
80  void writeMultipleCoils (const byte *frame, const word startreg, const word numoutputs, const byte bytecount);
81  #endif
82 
83  TRegister *searchRegister (word addr);
84 
85  void addReg (word address, word value = 0);
86  bool setReg (word address, word value);
87  word reg (word address);
88  bool setRegBounds (word address, word min, word max);
89  bool regOutOfBounds (word address, word value);
90 
91  // @deprecated
92  inline bool Reg (word address, word value) {
93  return setReg (address, value);
94  }
95  // @deprecated
96  inline word Reg (word address) {
97  return reg (address);
98  }
99 
100  protected:
101  byte *_frame;
102  byte _len;
103  byte _reply;
104  Print *_debug;
105  void receivePDU (byte *frame);
106  void reportServerId();
107  void debugMessage (bool reply = false);
108  #endif
109 
110  public:
115 
121  inline void addHreg (word offset, word value = 0) {
122  this->addReg (offset + TRegister::HregOffset, value);
123  }
131  inline bool setHreg (word offset, word value) {
132  return setReg (offset + TRegister::HregOffset, value);
133  }
139  inline word hreg (word offset) {
140  return reg (offset + TRegister::HregOffset);
141  }
150  inline bool setHregBounds (word offset, word min, word max) {
151  return setRegBounds (offset + TRegister::HregOffset, min, max);
152  }
160  inline bool hregOutOfBounds (word offset, word value) {
161  return regOutOfBounds (offset + TRegister::HregOffset, value);
162  }
163  ;
172  inline bool Hreg (word offset, word value) {
173  return setHreg (offset, value);
174  }
181  inline word Hreg (word offset) {
182  return hreg (offset);
183  }
184 
188  inline void setDebug (Print &print) {
189  _debug = &print;
190  _debug->println ("Modbus: debug enabled....");
191  }
192 
196  inline bool isDebug () {
197  return _debug != nullptr;
198  }
199 
204  inline void debug (const char *msg) {
205  if (_debug) {
206  _debug->println (msg);
207  }
208  }
209 
210  #ifndef USE_HOLDING_REGISTERS_ONLY
211 
217  void addCoil (word offset, bool value = false) {
218  this->addReg (offset + TRegister::CoilOffset, value ? 0xFF00 : 0x0000);
219  }
225  inline void addIsts (word offset, bool value = false) {
226  this->addReg (offset + TRegister::IstsOffset, value ? 0xFF00 : 0x0000);
227  }
233  inline void addIreg (word offset, word value = 0) {
234  this->addReg (offset + TRegister::IregOffset, value);
235  }
243  inline bool setCoil (word offset, bool value) {
244  return setReg (offset + TRegister::CoilOffset, value ? 0xFF00 : 0x0000);
245  }
253  inline bool setIsts (word offset, bool value) {
254  return setReg (offset + TRegister::IstsOffset, value ? 0xFF00 : 0x0000);
255  }
263  inline bool setIreg (word offset, word value) {
264  return setReg (offset + TRegister::IregOffset, value);
265  }
271  inline bool coil (word offset) {
272  return (reg (offset + TRegister::CoilOffset) == 0xFF00);
273  }
279  inline bool ists (word offset) {
280  return (reg (offset + TRegister::IstsOffset) == 0xFF00);
281  }
287  inline word ireg (word offset) {
288  return reg (offset + TRegister::IregOffset);
289  }
298  inline bool Coil (word offset, bool value) {
299  return setCoil (offset, value);
300  }
309  inline bool Ists (word offset, bool value) {
310  return setIsts (offset, value);
311  }
320  inline bool Ireg (word offset, word value) {
321  return setIreg (offset, value);
322  }
329  inline bool Coil (word offset) {
330  return coil (offset);
331  }
338  inline bool Ists (word offset) {
339  return ists (offset);
340  }
347  inline word Ireg (word offset) {
348  return ireg (offset);
349  }
355  int setAdditionalServerData (const char data[]);
356  #endif
357 };
Modbus base class.
Definition: Modbus.h:65
Modbus()
Default constructor.
void debug(const char *msg)
Print a debug message, only if debug mode is enabled.
Definition: Modbus.h:204
bool Coil(word offset)
Return the value of a coil.
Definition: Modbus.h:329
word ireg(word offset)
Return the value of an input register.
Definition: Modbus.h:287
bool hregOutOfBounds(word offset, word value)
Checks if the value is outside the bounds of a holging register.
Definition: Modbus.h:160
bool Ists(word offset, bool value)
Change the value of a discrete input This value will be returned when bus read,.
Definition: Modbus.h:309
bool coil(word offset)
Return the value of a coil.
Definition: Modbus.h:271
bool Coil(word offset, bool value)
Change the value of a coil This value will be returned when bus read, the master can also modify it.
Definition: Modbus.h:298
void addHreg(word offset, word value=0)
Add a holding register to the list.
Definition: Modbus.h:121
bool Ists(word offset)
Return the value of a discrete input.
Definition: Modbus.h:338
word Hreg(word offset)
Return the value of a holding register.
Definition: Modbus.h:181
void addIreg(word offset, word value=0)
Add a input register.
Definition: Modbus.h:233
bool setIreg(word offset, word value)
Change the value of an input register This value will be returned when bus read.
Definition: Modbus.h:263
int setAdditionalServerData(const char data[])
Sets additional Data for Report Server ID function.
void addIsts(word offset, bool value=false)
Add a discrete input.
Definition: Modbus.h:225
word Ireg(word offset)
Return the value of an input register.
Definition: Modbus.h:347
bool setCoil(word offset, bool value)
Change the value of a coil This value will be returned when bus read, the master can also modify it.
Definition: Modbus.h:243
bool setHreg(word offset, word value)
Change the value of a holding register This value will be returned when bus read, the master can also...
Definition: Modbus.h:131
bool Ireg(word offset, word value)
Change the value of an input register This value will be returned when bus read.
Definition: Modbus.h:320
bool isDebug()
Returns true if debug mode is enabled.
Definition: Modbus.h:196
word hreg(word offset)
Return the value of a holding register.
Definition: Modbus.h:139
bool setIsts(word offset, bool value)
Change the value of a discrete input This value will be returned when bus read,.
Definition: Modbus.h:253
void addCoil(word offset, bool value=false)
Add a coil.
Definition: Modbus.h:217
bool Hreg(word offset, word value)
Change the value of a holding register This value will be returned when bus read, the master can also...
Definition: Modbus.h:172
bool setHregBounds(word offset, word min, word max)
Sets the bounds of a holding register.
Definition: Modbus.h:150
bool ists(word offset)
Return the value of a discrete input.
Definition: Modbus.h:279
void setDebug(Print &print)
Enable debug mode.
Definition: Modbus.h:188