0df27b07
Peter M. Groen
Added doxygen com...
|
1
2
3
|
/****************************************************************************
* Copyright (c) 2022 Priva b.v.
****************************************************************************/
|
32017af3
Peter M. Groen
Setting up workin...
|
4
5
6
|
#pragma once
#include <string>
|
cadcf24a
Peter M. Groen
Setting up workin...
|
7
|
#include <unordered_map>
|
32017af3
Peter M. Groen
Setting up workin...
|
8
|
|
46785270
Peter M. Groen
Setting up workin...
|
9
|
/*!
|
527f96d4
Peter M. Groen
Setting up workin...
|
10
|
* The ConnectionPort enum.
|
0df27b07
Peter M. Groen
Added doxygen com...
|
11
12
13
|
* CP_EXTERNAL - the first serial port
* CP_IOBUS - The second serial port through RJ45 connector
* CP_TCP - TCP Connections
|
46785270
Peter M. Groen
Setting up workin...
|
14
|
*/
|
cadcf24a
Peter M. Groen
Setting up workin...
|
15
|
enum class ConnectionPort : unsigned int
|
32017af3
Peter M. Groen
Setting up workin...
|
16
|
{
|
cadcf24a
Peter M. Groen
Setting up workin...
|
17
|
CP_EXTERNAL = 0,
|
32017af3
Peter M. Groen
Setting up workin...
|
18
19
20
21
|
CP_IOBUS,
CP_TCP
};
|
46785270
Peter M. Groen
Setting up workin...
|
22
|
/*!
|
527f96d4
Peter M. Groen
Setting up workin...
|
23
|
* The Parity enum.
|
0df27b07
Peter M. Groen
Added doxygen com...
|
24
|
* Used in a serial port context
|
46785270
Peter M. Groen
Setting up workin...
|
25
|
*/
|
cadcf24a
Peter M. Groen
Setting up workin...
|
26
|
enum class Parity : unsigned int
|
32017af3
Peter M. Groen
Setting up workin...
|
27
|
{
|
cadcf24a
Peter M. Groen
Setting up workin...
|
28
29
30
31
32
|
PAR_ODD,
PAR_EVEN,
PAR_NONE
};
|
46785270
Peter M. Groen
Setting up workin...
|
33
|
/*!
|
527f96d4
Peter M. Groen
Setting up workin...
|
34
|
* The ConnectionType enum.
|
0df27b07
Peter M. Groen
Added doxygen com...
|
35
|
* Added for convenience, to distinguish between types.
|
46785270
Peter M. Groen
Setting up workin...
|
36
|
*/
|
cadcf24a
Peter M. Groen
Setting up workin...
|
37
38
39
40
41
|
enum class ConnectionType : unsigned int
{
CT_SERIAL,
CT_TCP,
CT_UNKNOWN
|
32017af3
Peter M. Groen
Setting up workin...
|
42
43
|
};
|
0df27b07
Peter M. Groen
Added doxygen com...
|
44
|
/*!
|
527f96d4
Peter M. Groen
Setting up workin...
|
45
|
* The ConnectionConfig class holds all the information we need to establish a proper connection.
|
0df27b07
Peter M. Groen
Added doxygen com...
|
46
47
48
49
50
51
|
* It can be created by a configuration object and passed on to the ModBus stack.
* By using this class, all the connectioninfo is within its context for convenience.
*
* Data can be accessed by their resp. getter-methods to avoid internal data-exposure
* according to the basic principles of object oriented programming. ( Abstraction, Encapsulation, Inheritance, and Polymorphism )
*/
|
32017af3
Peter M. Groen
Setting up workin...
|
52
53
54
|
class ConnectionConfig
{
public:
|
46785270
Peter M. Groen
Setting up workin...
|
55
|
/*!
|
527f96d4
Peter M. Groen
Setting up workin...
|
56
57
58
59
60
61
62
|
* ConnectionConfig Constructor. Used to create a new serial connection.
* @param port - The portname given by its enum.
* @param baud - The port speed in a serial port context ( default = 115200 )
* @param parity - The parity. ( Default : None, no parity )
* @param dataBits - The number of databits. RTU uses 8 (0 - 255), ASCII uses 7 (0 - 127). Default is RTU
* @param stopBits - The number of stopbits used to detect the end of the frame. ( Default = 1 )
* @param timeOut - Timeout in .1 secs. See the termios documentation for deviations on this.
|
46785270
Peter M. Groen
Setting up workin...
|
63
|
*/
|
cadcf24a
Peter M. Groen
Setting up workin...
|
64
|
ConnectionConfig( ConnectionPort port, int baud = 115200, Parity parity = Parity::PAR_NONE, int dataBits = 8, int stopBits = 1, int timeOut = -1 )
|
527f96d4
Peter M. Groen
Setting up workin...
|
65
66
67
68
69
70
71
72
73
|
: port( port )
, baudRate( baud )
, parity( parity )
, dataBits( dataBits )
, stopBits( stopBits )
, ipaddress()
, portnumber( -1 )
, timeOut( timeOut )
, connectionType( ConnectionType::CT_SERIAL )
|
cadcf24a
Peter M. Groen
Setting up workin...
|
74
|
{}
|
32017af3
Peter M. Groen
Setting up workin...
|
75
|
|
46785270
Peter M. Groen
Setting up workin...
|
76
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
77
|
* \brief ConnectionConfig Constructor. Used to create a new TCP connection.
|
527f96d4
Peter M. Groen
Setting up workin...
|
78
79
80
81
|
* @param port - The portname given by its enaum. ( Should be CP_TCP )
* @param ip - The ip address of the ModBus device we want to connect to.
* @param portnum - The portnumber the ModBus device is using
* @param timeOut - Timeout in which a modbus device should respond.
|
46785270
Peter M. Groen
Setting up workin...
|
82
|
*/
|
32017af3
Peter M. Groen
Setting up workin...
|
83
|
ConnectionConfig( ConnectionPort port, const std::string &ip, int portnum, int timeOut = -1 )
|
527f96d4
Peter M. Groen
Setting up workin...
|
84
85
86
87
88
89
90
91
92
|
: port( port )
, baudRate( -1 )
, parity( Parity::PAR_NONE )
, dataBits( -1 )
, stopBits( -1 )
, ipaddress( ip )
, portnumber( portnum )
, timeOut( timeOut )
, connectionType( ConnectionType::CT_TCP )
|
cadcf24a
Peter M. Groen
Setting up workin...
|
93
|
{}
|
32017af3
Peter M. Groen
Setting up workin...
|
94
|
|
cadcf24a
Peter M. Groen
Setting up workin...
|
95
|
// Getters and Setters. Implemented to avoid outside meddling on the member variables.
|
527f96d4
Peter M. Groen
Setting up workin...
|
96
97
98
99
100
101
102
|
std::string getPort() const { return portMap.at(port); } ///< Get the translated portName.
ConnectionPort getPortEnum() const { return port; } ///< Get the portname Enum
int getBaudRate() const { return baudRate; } ///< Get the given baudrate as int.
char getParity() const { return parityMap.at( parity ); } ///< Get the translated parity.
Parity getParityEnum() const { return parity; } ///< Get the parity Enum
int getDataBits() const { return dataBits; } ///< Get the number of databits ( 7 for ASCII, 8 for RTU )
int getStopBits() const { return stopBits; } ///< Get the number of stopBits. ( de-facto = 1 )
|
cadcf24a
Peter M. Groen
Setting up workin...
|
103
|
|
527f96d4
Peter M. Groen
Setting up workin...
|
104
105
|
std::string getIpAddress() const { return ipaddress; } ///< Get the ip-address as string
int getTcpPort() const { return portnumber; } ///< Get the tcp portnumber as int
|
32017af3
Peter M. Groen
Setting up workin...
|
106
|
|
527f96d4
Peter M. Groen
Setting up workin...
|
107
108
|
int getTimeOut() const { return timeOut; } ///< Get the timeout as a multitude of 0.1 sec.
ConnectionType getType() const { return connectionType; } ///< Get the connection type ( Serial, TCP or Unknown )
|
32017af3
Peter M. Groen
Setting up workin...
|
109
|
|
46785270
Peter M. Groen
Setting up workin...
|
110
|
private:
|
cadcf24a
Peter M. Groen
Setting up workin...
|
111
|
|
32017af3
Peter M. Groen
Setting up workin...
|
112
|
/// Serial connections
|
527f96d4
Peter M. Groen
Setting up workin...
|
113
114
115
116
117
|
ConnectionPort port; ///< Member variable holding the portName Enum
int baudRate; ///< Member variable holding the Serial port Baudrate
Parity parity; ///< Member variable holding the Serial port Parity
int dataBits; ///< Member variable holding the number of databits
int stopBits; ///< Member variable holding the number of stopbits
|
32017af3
Peter M. Groen
Setting up workin...
|
118
119
|
/// TCP connections
|
527f96d4
Peter M. Groen
Setting up workin...
|
120
121
|
std::string ipaddress; ///< Member variable holding the ip-address of the TCP-connection
int portnumber; ///< Member variable holding the portnumber of the TCP-connection
|
32017af3
Peter M. Groen
Setting up workin...
|
122
|
|
cadcf24a
Peter M. Groen
Setting up workin...
|
123
|
/// Generic
|
527f96d4
Peter M. Groen
Setting up workin...
|
124
125
|
int timeOut; ///< Member variable holding the timeOut value
ConnectionType connectionType; ///< Member variable holding the connection type.
|
46785270
Peter M. Groen
Setting up workin...
|
126
|
|
0df27b07
Peter M. Groen
Added doxygen com...
|
127
|
/// Translation tables for portnames and parity.
|
b85a3e4a
Peter M. Groen
Setting up workin...
|
128
129
130
|
// ============================================================
// == Change accordingly to the devicenames on your platform ==
// ============================================================
|
527f96d4
Peter M. Groen
Setting up workin...
|
131
|
std::unordered_map<ConnectionPort, std::string> portMap =
|
cadcf24a
Peter M. Groen
Setting up workin...
|
132
133
134
135
136
|
{
{ ConnectionPort::CP_EXTERNAL, "/dev/ttyUSB0" },
{ ConnectionPort::CP_IOBUS, "/dev/ttyUSB1" }
};
|
527f96d4
Peter M. Groen
Setting up workin...
|
137
|
std::unordered_map<Parity, char> parityMap =
|
cadcf24a
Peter M. Groen
Setting up workin...
|
138
139
140
141
142
|
{
{ Parity::PAR_EVEN, 'E' },
{ Parity::PAR_ODD, 'O' },
{ Parity::PAR_NONE, 'N' }
};
|
32017af3
Peter M. Groen
Setting up workin...
|
143
|
};
|