Skip to content

Commit d9272bf

Browse files
committed
First version of Data class, without template
1 parent 59be9c8 commit d9272bf

File tree

11 files changed

+864
-55
lines changed

11 files changed

+864
-55
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ With [Codelite](https://codelite.org/) it's easier and funny, right ?
117117

118118
![Debugging with Codelite](https://raw.githubusercontent.com/epsilonrt/piduino/master/doc/images/codelite-2.png)
119119

120-
You will find several examples in the folder `/usr/share/doc/modbuspp/examples` (or `/usr/local/share/...`)
120+
You will find several examples in the folder
121+
[/usr/share/doc/modbuspp/examples](https://github.com/epsilonrt/libmodbuspp/tree/master/examples/master) (or `/usr/local/share/...`)

examples/master/read-coils/read-coils.project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,6 @@
105105
<VirtualDirectory Name="doc">
106106
<File Name="README.md"/>
107107
</VirtualDirectory>
108+
<Dependencies Name="Debug"/>
109+
<Dependencies Name="Release"/>
108110
</CodeLite_Project>

examples/master/read-input-registers/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using namespace std;
88
using namespace Modbus;
99

10+
1011
// -----------------------------------------------------------------------------
1112
int main (int argc, char **argv) {
1213
string port ("/dev/ttyUSB1");

examples/master/read-input-registers/read-input-registers.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CurrentFileName :=
1313
CurrentFilePath :=
1414
CurrentFileFullPath :=
1515
User :=epsilonrt
16-
Date :=19/01/19
16+
Date :=20/01/19
1717
CodeLitePath :=/home/pascal/.codelite
1818
LinkerName :=/usr/bin/g++
1919
SharedObjectLinkerName :=/usr/bin/g++ -shared -fPIC

examples/master/read-input-registers/read-input-registers.project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,6 @@
100100
<VirtualDirectory Name="doc">
101101
<File Name="README.md"/>
102102
</VirtualDirectory>
103+
<Dependencies Name="Debug"/>
104+
<Dependencies Name="Release"/>
103105
</CodeLite_Project>

include/modbuspp-data.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* Copyright © 2018 Pascal JEAN, All rights reserved.
2+
* This file is part of the libmodbuspp Library.
3+
*
4+
* The libmodbuspp Library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 3 of the License, or (at your option) any later version.
8+
*
9+
* The libmodbuspp Library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with the libmodbuspp Library; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef MODBUSPP_DATA_H
19+
#define MODBUSPP_DATA_H
20+
21+
#include <string>
22+
#include <modbus.h>
23+
#include "global.h"
24+
25+
/**
26+
*
27+
*/
28+
namespace Modbus {
29+
30+
/**
31+
* @class Data
32+
* @brief
33+
*/
34+
class Data {
35+
public:
36+
enum Type {
37+
Byte, // 2 Bytes (nothing is smaller than the word on MODBUS)
38+
Word, // 2 Bytes - 1 MODBUS register
39+
LongWord, // 4 Bytes - 2 MODBUS registers
40+
LongLongWord, // 8 Bytes - 4 MODBUS registers
41+
Float, // 4 Bytes - 2 MODBUS registers
42+
Double, // 8 Bytes - 4 MODBUS registers
43+
Void = -1
44+
};
45+
46+
enum Endian { // network number ABCD
47+
EndianBigBig = 0x00, // bytes in big endian order, word in big endian order : ABCD
48+
EndianBig = EndianBigBig, // big endian order : ABCD
49+
EndianBigLittle = 0x01, // bytes in big endian order, word in little endian order : CDAB
50+
EndianLittleBig = 0x02, // bytes in little endian order, word in big endian order : BADC
51+
EndianLittleLittle = 0x03, // bytes in little endian order, word in little endian order : DCBA
52+
EndianLittle = EndianLittleLittle // little endian order : DCBA
53+
};
54+
55+
Data (Type type = Void, Endian endian = EndianBig); // default constructor
56+
Data (const Data & other);
57+
virtual ~Data();
58+
59+
Data (uint8_t v, Endian endian = EndianBig);
60+
Data (uint16_t v, Endian endian = EndianBig);
61+
Data (uint32_t v, Endian endian = EndianBig);
62+
Data (uint64_t v, Endian endian = EndianBig);
63+
Data (char v, Endian endian = EndianBig);
64+
Data (int v, Endian endian = EndianBig);
65+
Data (long v, Endian endian = EndianBig);
66+
Data (long long v, Endian endian = EndianBig);
67+
Data (float v, Endian endian = EndianBig);
68+
Data (double v, Endian endian = EndianBig);
69+
70+
void swap (Data &other);
71+
Data& operator= (const Data &other);
72+
73+
void set (uint8_t v);
74+
void set (uint16_t v);
75+
void set (uint32_t v);
76+
void set (uint64_t v);
77+
78+
void set (char v);
79+
void set (int v);
80+
void set (long v);
81+
void set (long long v);
82+
void set (float v);
83+
void set (double v);
84+
85+
void get (uint8_t & v) const;
86+
void get (uint16_t & v) const;
87+
void get (uint32_t & v) const;
88+
void get (uint64_t & v) const;
89+
90+
void get (char & v) const;
91+
void get (int & v) const;
92+
void get (long & v) const;
93+
void get (long long & v) const;
94+
void get (float & v) const;
95+
void get (double & v) const;
96+
97+
Endian endianness() const;
98+
Type type() const;
99+
void setType (Type t);
100+
size_t size() const;
101+
uint16_t * ptr();
102+
const uint16_t * ptr() const;
103+
104+
// debug purpose
105+
void print () const;
106+
static void print (const uint8_t * p, const size_t s);
107+
static void print (const uint8_t & v);
108+
static void print (const uint16_t & v);
109+
static void print (const uint32_t & v);
110+
static void print (const uint64_t & v);
111+
static void print (const char & v);
112+
static void print (const int & v);
113+
static void print (const long & v);
114+
static void print (const long long & v);
115+
static void print (const float & v);
116+
static void print (const double & v);
117+
118+
protected:
119+
class Private;
120+
Data (Private &dd);
121+
std::unique_ptr<Private> d_ptr;
122+
123+
private:
124+
PIMP_DECLARE_PRIVATE (Data)
125+
};
126+
}
127+
/**
128+
* @}
129+
*/
130+
/* ========================================================================== */
131+
#endif /* MODBUSPP_DATA_H defined */

include/modbuspp.h

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,94 @@ namespace Modbus {
5757
class Data {
5858
public:
5959
enum Type {
60-
Byte,
61-
Word,
62-
LongWord,
63-
LongLongWord,
64-
Float,
65-
Double
60+
Byte, // 2 Bytes (nothing is smaller than the word on MODBUS)
61+
Word, // 2 Bytes - 1 MODBUS register
62+
LongWord, // 4 Bytes - 2 MODBUS registers
63+
LongLongWord, // 8 Bytes - 4 MODBUS registers
64+
Float, // 4 Bytes - 2 MODBUS registers
65+
Double, // 8 Bytes - 4 MODBUS registers
66+
Void = -1
6667
};
6768

68-
enum Endian {
69-
EndianBigBig = 0x00, // bytes in big endian order, word in big endian order
70-
EndianBig = EndianBigBig,
71-
EndianBigLittle = 0x01,
72-
EndianLittleBig = 0x02,
73-
EndianLittleLittle = 0x03,
74-
EndianLittle = EndianLittleLittle
69+
enum Endian { // network number ABCD
70+
EndianBigBig = 0x00, // bytes in big endian order, word in big endian order : ABCD
71+
EndianBig = EndianBigBig, // big endian order : ABCD
72+
EndianBigLittle = 0x01, // bytes in big endian order, word in little endian order : CDAB
73+
EndianLittleBig = 0x02, // bytes in little endian order, word in big endian order : BADC
74+
EndianLittleLittle = 0x03, // bytes in little endian order, word in little endian order : DCBA
75+
EndianLittle = EndianLittleLittle // little endian order : DCBA
7576
};
7677

77-
Data (Endian bigEndian = EndianBig);
78-
Data (char i, Endian bigEndian = EndianBig);
79-
Data (int i, Endian bigEndian = EndianBig);
80-
Data (long i, Endian bigEndian = EndianBig);
81-
Data (long long i, Endian bigEndian = EndianBig);
82-
Data (uint8_t i, Endian bigEndian = EndianBig);
83-
Data (uint16_t i, Endian bigEndian = EndianBig);
84-
Data (uint32_t i, Endian bigEndian = EndianBig);
85-
Data (uint64_t i, Endian bigEndian = EndianBig);
78+
Data (Type type = Void, Endian endian = EndianBig); // default constructor
79+
Data (const Data & other);
80+
virtual ~Data();
81+
82+
Data (uint8_t v, Endian endian = EndianBig);
83+
Data (uint16_t v, Endian endian = EndianBig);
84+
Data (uint32_t v, Endian endian = EndianBig);
85+
Data (uint64_t v, Endian endian = EndianBig);
86+
Data (char v, Endian endian = EndianBig);
87+
Data (int v, Endian endian = EndianBig);
88+
Data (long v, Endian endian = EndianBig);
89+
Data (long long v, Endian endian = EndianBig);
90+
Data (float v, Endian endian = EndianBig);
91+
Data (double v, Endian endian = EndianBig);
92+
93+
void swap (Data &other);
94+
Data& operator= (const Data &other);
95+
96+
void set (uint8_t v);
97+
void set (uint16_t v);
98+
void set (uint32_t v);
99+
void set (uint64_t v);
100+
101+
void set (char v);
102+
void set (int v);
103+
void set (long v);
104+
void set (long long v);
105+
void set (float v);
106+
void set (double v);
107+
108+
void get (uint8_t & v) const;
109+
void get (uint16_t & v) const;
110+
void get (uint32_t & v) const;
111+
void get (uint64_t & v) const;
112+
113+
void get (char & v) const;
114+
void get (int & v) const;
115+
void get (long & v) const;
116+
void get (long long & v) const;
117+
void get (float & v) const;
118+
void get (double & v) const;
119+
120+
Endian endianness() const;
86121
Type type() const;
122+
void setType (Type t);
87123
size_t size() const;
124+
uint16_t * ptr();
125+
const uint16_t * ptr() const;
126+
127+
// debug purpose
128+
void print () const;
129+
static void print (const uint8_t * p, const size_t s);
130+
static void print (const uint8_t & v);
131+
static void print (const uint16_t & v);
132+
static void print (const uint32_t & v);
133+
static void print (const uint64_t & v);
134+
static void print (const char & v);
135+
static void print (const int & v);
136+
static void print (const long & v);
137+
static void print (const long long & v);
138+
static void print (const float & v);
139+
static void print (const double & v);
140+
141+
protected:
142+
class Private;
143+
Data (Private &dd);
144+
std::unique_ptr<Private> d_ptr;
145+
146+
private:
147+
PIMP_DECLARE_PRIVATE (Data)
88148
};
89149

90150
/**
@@ -160,7 +220,7 @@ namespace Modbus {
160220
* @brief MODBUS Master (Client)
161221
*/
162222
class Master : public Device {
163-
223+
164224
public:
165225
/**
166226
* @brief Constructor
@@ -209,13 +269,13 @@ namespace Modbus {
209269
int readRegistrers (int addr, uint16_t * dest, int nb = 1);
210270
/**
211271
* @brief write a single bit
212-
*
213-
* This function shall write the status of value at the address addr of
272+
*
273+
* This function shall write the status of value at the address addr of
214274
* the remote device. \n
215275
* The function uses the Modbus function code 0x05 (force single coil).
216276
* @param addr
217277
* @param value
218-
* @return
278+
* @return
219279
*/
220280
int writeCoil (int addr, bool value);
221281
/**

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ list(REMOVE_ITEM src_modbuspp ${MODBUSPP_SRC_DIR}/modbuspp.cpp)
2929
set (hdr_modbuspp
3030
${MODBUSPP_INC_DIR}/global.h
3131
${MODBUSPP_INC_DIR}/modbuspp.h
32+
${MODBUSPP_INC_DIR}/modbuspp-data.h
3233
)
3334

3435
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in

0 commit comments

Comments
 (0)