32017af3
Peter M. Groen
Setting up workin...
|
1
2
3
4
5
6
7
8
|
/*****************************************************************************
* Copyright (c) 2022 Priva b.v.
*****************************************************************************/
#pragma once
#include "ConnectionConfig.h"
#include "IModbusAdapter.h"
|
32017af3
Peter M. Groen
Setting up workin...
|
9
|
|
cadcf24a
Peter M. Groen
Setting up workin...
|
10
11
12
13
|
// std
#include <memory>
#include <variant>
#include <vector>
|
32017af3
Peter M. Groen
Setting up workin...
|
14
|
|
783ce3c5
Peter M. Groen
Setting up workin...
|
15
16
|
typedef modbus_t;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
17
18
19
20
|
/// @brief The ModbusAdapter class represents a single modbus context. Each context will
/// result in an instance of this class. It is not intended to be
/// created directly but through a factory. The factory will create
/// the object and return the pointer to its interface.
|
32017af3
Peter M. Groen
Setting up workin...
|
21
22
23
|
class ModbusAdapter : public IModbusAdapter
{
public:
|
cadcf24a
Peter M. Groen
Setting up workin...
|
24
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
25
|
* \brief Default constructor
|
cadcf24a
Peter M. Groen
Setting up workin...
|
26
|
*/
|
46785270
Peter M. Groen
Setting up workin...
|
27
|
explicit ModbusAdapter();
|
cadcf24a
Peter M. Groen
Setting up workin...
|
28
29
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
30
|
* \brief Default destructor
|
cadcf24a
Peter M. Groen
Setting up workin...
|
31
|
*/
|
32017af3
Peter M. Groen
Setting up workin...
|
32
33
|
virtual ~ModbusAdapter();
|
cadcf24a
Peter M. Groen
Setting up workin...
|
34
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
35
|
* \brief /// Create a modbus connection, accepting a configuration object.
|
cadcf24a
Peter M. Groen
Setting up workin...
|
36
|
*/
|
783ce3c5
Peter M. Groen
Setting up workin...
|
37
|
bool ModbusConnect( const ConnectionConfig &config ) override;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
38
39
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
40
41
|
* \brief ModbusDisconnect
* Disconnect from the serial bus or the TCP connection, freeing its resources
|
cadcf24a
Peter M. Groen
Setting up workin...
|
42
|
*/
|
b85a3e4a
Peter M. Groen
Setting up workin...
|
43
|
bool ModbusDisconnect() override;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
44
45
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
46
47
48
49
50
51
52
53
|
* \brief Read data from a modbus device given by its parameters.
* \param slaveId - The Id of the ModbusDevice.
* \param functionCode - The code describing the action we want to perform on the device.
* Given by an enum, provided by the modbus-stack.
* \param startAddress - Startaddres of the register we want to read.
* \param noOfItems - The number of items we expect back.
* \returns modbusData - A vector holding each register in an entry in the same order as they are received.
* Empty if no data was received.
|
cadcf24a
Peter M. Groen
Setting up workin...
|
54
|
*/
|
46785270
Peter M. Groen
Setting up workin...
|
55
|
modbusData ModbusReadData( int slaveId, int functionCode, int startAddress, int noOfItems ) override;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
56
57
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
58
59
60
61
62
63
|
* \brief Read data from the holdregisters ( or keep-registers ) of a modbus device.
* \param slaveId - The Id of the ModbusDevice.
* \param startAddress - Startaddres of the register we want to read.
* \param noOfItems - The number of items we expect back.
* \returns modbusData - A vector holding each register in an entry in the same order as they are received.
* Empty if no data was received.
|
cadcf24a
Peter M. Groen
Setting up workin...
|
64
|
*/
|
46785270
Peter M. Groen
Setting up workin...
|
65
|
modbusData ModbusReadHoldReg( int slaveId, int startAddress, int noOfItems ) override;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
66
67
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
68
69
70
71
72
73
74
75
|
* \brief Write data to the device.
* \param slaveId - The Id of the Modbus device
* \param funtionCode - The code describing the action we want to perform on the device
* given by an enum, provided by the modbus-stack.
* \param startAddress - Startaddres of the register we want to read.
* \param noOfItems - The number of items we expect to be written
* \param values - The values we want to write to the given device. Each vector-entry represents a single byte
* and they will be sent in sequence.
|
cadcf24a
Peter M. Groen
Setting up workin...
|
76
|
*/
|
46785270
Peter M. Groen
Setting up workin...
|
77
|
void ModBusWriteData( int slaveId, int functionCode, int startAddress, int noOfItems, std::vector<int>values ) override;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
78
79
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
80
81
|
* \brief Indicates if this connection is alive.
* \return True if alive, false if not.
|
cadcf24a
Peter M. Groen
Setting up workin...
|
82
|
*/
|
46785270
Peter M. Groen
Setting up workin...
|
83
|
bool isConnected() const override;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
84
85
|
/*!
|
0df27b07
Peter M. Groen
Added doxygen com...
|
86
87
88
|
* \brief returns the translated error code coming from the modbus stack.
* \param errnum - error Number coming from the stack.
* \return The translated error as a string. Empty if not resolvable.
|
cadcf24a
Peter M. Groen
Setting up workin...
|
89
|
*/
|
783ce3c5
Peter M. Groen
Setting up workin...
|
90
|
std::string ErrorString( int errorNumber ) const override;
|
cadcf24a
Peter M. Groen
Setting up workin...
|
91
92
|
private: // Methods
|
783ce3c5
Peter M. Groen
Setting up workin...
|
93
94
95
96
97
98
99
100
101
102
103
|
/*!
* Create and connect to a serial port.
*
* @param serialport - The serial port by name ( i.e. /dev/ttyUSB0 )
* @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.
*/
bool ModbusConnectRTU( const std::string &serialport, int baud, char parity, int dataBits, int stopBits, int timeOut );
|
cadcf24a
Peter M. Groen
Setting up workin...
|
104
|
|
783ce3c5
Peter M. Groen
Setting up workin...
|
105
106
107
108
109
110
111
112
|
/*!
* ConnectionConfig Constructor. Used to create a new TCP connection.
*
* @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.
*/
bool ModbusConnectTCP( const std::string &ip, int port, int timeOut = -1 );
|
cadcf24a
Peter M. Groen
Setting up workin...
|
113
114
|
private: // Members
|
783ce3c5
Peter M. Groen
Setting up workin...
|
115
116
117
118
|
ConnectionType connectionType { ConnectionType::CT_UNKNOWN }; ///> The type of connection this instance provides. Needed for administration
bool connected { false }; ///> Shows if the connection is still active.
modbus_t *modbus; ///> The actual low-level modbus instance as a raw-pointer.
/// ( unique_pointer gives an error on this struct )
|
cadcf24a
Peter M. Groen
Setting up workin...
|
119
|
|
32017af3
Peter M. Groen
Setting up workin...
|
120
|
};
|