libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Modbus.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * Homegear is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * Homegear is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with Homegear. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef LIBHOMEGEAR_BASE_MODBUS_H
32 #define LIBHOMEGEAR_BASE_MODBUS_H
33 
34 #include "../Exception.h"
35 #include "TcpSocket.h"
36 
37 namespace BaseLib
38 {
39 
45 class ModbusException : public Exception
46 {
47 public:
48  ModbusException(const std::string& message) : Exception(message) {}
49  ModbusException(const std::string& message, uint8_t code, std::vector<char> packet) : Exception(message), _code(code), _packet(std::move(packet)) {}
50 
51  uint8_t getCode() const { return _code; }
52  std::vector<char> getPacket() const { return _packet; }
53 private:
54  uint8_t _code = 0;
55  std::vector<char> _packet;
56 };
57 
64 {
65 public:
66  ModbusServerBusyException(std::string message) : ModbusException(message) {}
67  ModbusServerBusyException(std::string message, uint8_t code, std::vector<char> packet) : ModbusException(message, code, packet) {}
68 };
69 
121 class Modbus
122 {
123 public:
124  struct ModbusInfo
125  {
126  std::string hostname;
127  int32_t port = 502;
128  bool useSsl = false;
129  bool keepAlive = true;
130  std::string certFile;
131  std::string certData;
132  std::string keyFile;
133  std::shared_ptr<Security::SecureVector<uint8_t>> keyData;
134  bool verifyCertificate = true;
135  std::string caFile; //For client certificate verification
136  std::string caData; //For client certificate verification
137  uint32_t timeout = 5000;
138  };
139 
140  struct DeviceInfo
141  {
142  std::string vendorName;
143  std::string productCode;
144  std::string majorMinorRevision;
145  std::string vendorUrl;
146  std::string productName;
147  std::string modelName;
148  std::string userApplicationName;
149  std::map<uint8_t, std::vector<uint8_t>> objects;
150  };
151 
152  Modbus(BaseLib::SharedObjects* baseLib, ModbusInfo& serverInfo);
153 
157  virtual ~Modbus();
158 
162  void setSlaveId(uint8_t value) { _slaveId = value; }
163 
167  void setDebug(bool value) { _debug = value; }
168 
173  void connect();
174 
178  void disconnect();
179 
184  bool isConnected() { return _socket && _socket->connected(); }
185 
199  void readCoils(uint16_t startingAddress, std::vector<uint8_t>& buffer, uint16_t coilCount);
200 
214  void readDiscreteInputs(uint16_t startingAddress, std::vector<uint8_t>& buffer, uint16_t inputCount);
215 
229  void readHoldingRegisters(uint16_t startingAddress, std::vector<uint16_t>& buffer, uint16_t registerCount);
230 
244  void readInputRegisters(uint16_t startingAddress, std::vector<uint16_t>& buffer, uint16_t registerCount);
245 
257  void writeSingleCoil(uint16_t address, bool value);
258 
270  void writeSingleRegister(uint16_t address, uint16_t value);
271 
284  void writeMultipleCoils(uint16_t startAddress, const std::vector<uint8_t>& values, uint16_t coilCount);
285 
298  void writeMultipleRegisters(uint16_t startAddress, const std::vector<uint16_t>& values, uint16_t registerCount);
299 
315  void readWriteMultipleRegisters(uint16_t readStartAddress, std::vector<uint16_t>& readBuffer, uint16_t readRegisterCount, uint16_t writeStartAddress, const std::vector<uint16_t>& writeValues, uint16_t writeRegisterCount);
316 
326  DeviceInfo readDeviceIdentification();
327 private:
328  static const uint8_t _reverseByteMask[256];
329 
333  uint8_t _slaveId = 0xFF;
334 
338  bool _debug = false;
339 
343  bool _keepAlive = true;
344 
348  BaseLib::SharedObjects* _bl = nullptr;
349 
355  std::mutex _socketMutex;
356 
360  std::unique_ptr<TcpSocket> _socket;
361 
365  std::string _hostname;
366 
370  int32_t _port = 502;
371 
375  std::unique_ptr<std::vector<char>> _readBuffer;
376 
380  uint16_t _transactionId = 0;
381 
388  void insertHeader(std::vector<char>& packet, uint8_t functionCode, uint16_t payloadSize);
389 
390  std::vector<char> getResponse(std::vector<char>& packet);
391 };
392 
393 }
394 
395 #endif
Definition: Modbus.h:140
std::map< uint8_t, std::vector< uint8_t > > objects
Definition: Modbus.h:149
std::string vendorName
Definition: Modbus.h:142
std::string certData
Definition: Modbus.h:131
std::string userApplicationName
Definition: Modbus.h:148
ModbusException(const std::string &message)
Definition: Modbus.h:48
This is the base library main class.
Definition: BaseLib.h:95
void setDebug(bool value)
Enables or disables debug mode (disabled by default).
Definition: Modbus.h:167
std::string majorMinorRevision
Definition: Modbus.h:144
std::shared_ptr< Security::SecureVector< uint8_t > > keyData
Definition: Modbus.h:133
Exception class for the Modbus server.
Definition: Modbus.h:45
Definition: BaseLib.cpp:34
Exception class thrown when the Modbus server is busy.
Definition: Modbus.h:63
ModbusServerBusyException(std::string message)
Definition: Modbus.h:66
bool isConnected()
Checks if the socket is connected.
Definition: Modbus.h:184
std::string caFile
Definition: Modbus.h:135
std::string vendorUrl
Definition: Modbus.h:145
PVariable value
Definition: UiElements.h:217
std::string modelName
Definition: Modbus.h:147
std::string caData
Definition: Modbus.h:136
std::string productCode
Definition: Modbus.h:143
ModbusServerBusyException(std::string message, uint8_t code, std::vector< char > packet)
Definition: Modbus.h:67
ModbusException(const std::string &message, uint8_t code, std::vector< char > packet)
Definition: Modbus.h:49
std::string certFile
Definition: Modbus.h:130
This class provides a Modbus client.
Definition: Modbus.h:121
std::string productName
Definition: Modbus.h:146
std::string hostname
Definition: Modbus.h:126
Base class for all exceptions defined in Homegear.
Definition: Exception.h:41
std::string keyFile
Definition: Modbus.h:132
std::vector< char > getPacket() const
Definition: Modbus.h:52
void setSlaveId(uint8_t value)
Sets the slave ID.
Definition: Modbus.h:162
Definition: Modbus.h:124
uint8_t getCode() const
Definition: Modbus.h:51