1919#define MODBUSPP_DATA_H
2020
2121#include < cstdio> // printf
22- #include < cstring> // mencpy ...
22+ #include < cstring> // memcpy ...
2323#include < array>
2424#include < type_traits>
2525#include " modbuspp-swap.h"
2626
27- #ifndef __DOXYGEN__
28- #endif /* __DOXYGEN__ not defined */
29-
30- /* *
31- *
32- */
3327namespace Modbus {
3428
35- enum Endian { // network number ABCD
36- EndianBigBig = 0x00 , // bytes in big endian order, word in big endian order : ABCD
37- EndianBig = EndianBigBig, // big endian order : ABCD
38- EndianBigLittle = 0x01 , // bytes in big endian order, word in little endian order : CDAB
39- EndianLittleBig = 0x02 , // bytes in little endian order, word in big endian order : BADC
40- EndianLittleLittle = 0x03 , // bytes in little endian order, word in little endian order : DCBA
41- EndianLittle = EndianLittleLittle // little endian order : DCBA
29+ /* *
30+ * @enum Endian
31+ * @brief Sequential order in which bytes are arranged
32+ */
33+ enum Endian {
34+ EndianBigBig = 0x00 , // /< Bytes in big endian order, word in big endian order : ABCD
35+ EndianBig = EndianBigBig, // /< Big endian order : ABCD
36+ EndianBigLittle = 0x01 , // /< Bytes in big endian order, word in little endian order : CDAB
37+ EndianLittleBig = 0x02 , // /< Bytes in little endian order, word in big endian order : BADC
38+ EndianLittleLittle = 0x03 , // /< Bytes in little endian order, word in little endian order : DCBA
39+ EndianLittle = EndianLittleLittle // /< Little endian order : DCBA
4240 };
4341
44-
4542 class Device ;
4643 class Master ;
4744
4845 /* *
4946 * @class Data
5047 * @author Pascal JEAN, aka epsilonrt
5148 * @copyright GNU Lesser General Public License
52- * @brief
49+ * @brief Arithmetic data in multiple 16-bit Modbus registers
50+ *
51+ * Data is a template class for storing, transmitting, and receiving
52+ * arithmetic data in multiple 16-bit Modbus registers.
53+ *
54+ * @param T is a type of arithmetic data (int, float ...) of a size greater
55+ * than or equal to 2.
56+ * @param e is the order of bytes and words in the data model used by the
57+ * user's Modbus network. By default it is the big endian order for bytes
58+ * and words that is used.
5359 */
54- template <typename T, Endian e = EndianBigBig >
60+ template <typename T, Endian e = EndianBig >
5561 class Data {
5662 public:
5763 static_assert ( (sizeof (T) >= 2 && (sizeof (T) % 2 ) == 0 ), " Bad Data typename !" );
58- static_assert (std::is_arithmetic<T>::value, " Arithmetic type required. " );
64+ static_assert (std::is_arithmetic<T>::value, " Arithmetic type required ! " );
5965
66+ /* *
67+ * @brief Default constructor
68+ *
69+ * The default value of T is 0.
70+ */
6071 Data () : m_endian (e) {
6172 m_value = 0 ;
6273 updateRegisters ();
6374 }
6475
76+ /* *
77+ * @brief Constructor from a value of T
78+ */
6579 Data (const T& t) : m_value (t), m_endian (e) {
6680 updateRegisters ();
6781 }
6882
83+ /* *
84+ * @brief Overload of the reference operator on the T value
85+ */
6986 operator T&() {
7087 return m_value;
7188 }
7289
90+ /* *
91+ * @brief Overload of the reference operator on the T value
92+ */
7393 operator T&() const {
7494 return m_value;
7595 }
7696
97+ /* *
98+ * @brief Access to the T value
99+ */
77100 T& value () {
78101 return m_value;
79102 }
80103
104+ /* *
105+ * @brief Access to the T value
106+ */
81107 const T& value () const {
82108 return m_value;
83109 }
84110
111+ /* *
112+ * @brief Overload of the pointer operator on the T value
113+ */
85114 T* operator &() {
86115 return & m_value;
87116 }
88117
118+ /* *
119+ * @brief Overload of the pointer operator on the T value
120+ */
89121 const T* operator &() const {
90122 return & m_value;
91123 }
92124
125+ /* *
126+ * @brief Overload of the assignment operator from a T value
127+ */
93128 T& operator = (const T& t) {
94129 m_value = t;
95130 updateRegisters ();
96131 return m_value;
97132 }
98133
134+ /* *
135+ * @brief Return the bytes and words endianness
136+ */
99137 Endian endianness () const {
100138 return m_endian;
101139 }
102140
141+ /* *
142+ * @brief Number of bytes of type T
143+ */
103144 std::size_t size () const {
104145 return sizeof (m_value);
105146 }
106147
148+ /* *
149+ * @brief Array of Modbus registers corresponding to the T value
150+ */
107151 std::array < uint16_t , sizeof (T) / 2 > & registers () {
108152 return m_registers;
109153 }
110154
155+ /* *
156+ * @brief Array of Modbus registers corresponding to the T value
157+ */
111158 const std::array < uint16_t , sizeof (T) / 2 > & registers () const {
112159 return m_registers;
113160 }
114161
115-
162+ /* *
163+ * @brief Swap bytes and words of a T value \b v
164+ *
165+ * The order used is \b endianness().
166+ */
116167 void swap (T & v) {
117168
118169 switch (m_endian) { // net value: ABCDEFGH
@@ -130,7 +181,11 @@ namespace Modbus {
130181 }
131182 }
132183
133- // debug purpose
184+ /* *
185+ * @brief Prints the hexadecimal values of a byte array
186+ *
187+ * For debugging purpose.
188+ */
134189 static void print (const uint8_t * p, const size_t s) {
135190 std::printf (" 0x" );
136191 for (std::size_t i = 0 ; i < s; i++) {
@@ -139,10 +194,20 @@ namespace Modbus {
139194 std::printf (" \n " );
140195 }
141196
197+ /* *
198+ * @brief Prints the hexadecimal values of T value
199+ *
200+ * For debugging purpose.
201+ */
142202 static void print (const T & v) {
143203 print ( (const uint8_t *) &v, sizeof (v));
144204 }
145205
206+ /* *
207+ * @brief Prints the hexadecimal values of the current T value
208+ *
209+ * For debugging purpose.
210+ */
146211 void print () {
147212 updateRegisters ();
148213 print ( (const uint8_t *) m_registers.data (), size ());
@@ -151,8 +216,9 @@ namespace Modbus {
151216 friend class Device ;
152217 friend class Master ;
153218
154- protected:
219+ protected:
155220
221+ #ifndef __DOXYGEN__
156222 // update MODBUS registers from data value
157223 // to call before writing in the MODBUS registers
158224 void updateRegisters () {
@@ -178,15 +244,13 @@ namespace Modbus {
178244 swap (v);
179245 m_value = ntoh (v);
180246 }
247+ #endif /* __DOXYGEN__ not defined */
181248
182249 private:
183250 T m_value;
184251 Endian m_endian;
185252 std::array < uint16_t , sizeof (T) / 2 > m_registers;
186253 };
187254}
188- /* *
189- * @}
190- */
191255/* ========================================================================== */
192256#endif /* MODBUSPP_DATA_H defined */
0 commit comments