/***************************************************************************** * Copyright (c) 2022 Priva b.v. *****************************************************************************/ #pragma once #include "ConnectionConfig.h" #include "IModbusAdapter.h" #include "modbus.h" // std #include #include #include /// @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. class ModbusAdapter : public IModbusAdapter { public: /*! * \brief ModbusAdapter */ explicit ModbusAdapter(); /*! * \brief ~ModbusAdapter */ virtual ~ModbusAdapter(); /*! * \brief ModbusConnect * \param conncfg */ bool ModbusConnect( const ConnectionConfig &conncfg ) override; /*! * \brief ModbusDisconnect */ bool ModbusDisconnect() override; /*! * \brief ModbusReadData * \param slaveId * \param startAddress * \param noOfItems */ modbusData ModbusReadData( int slaveId, int functionCode, int startAddress, int noOfItems ) override; /*! * \brief ModbusReadHoldReg * \param slaveId * \param startAddress * \param noOfItems * \return */ modbusData ModbusReadHoldReg( int slaveId, int startAddress, int noOfItems ) override; /*! * \brief ModBusWriteData * \param slaveId * \param funtionCode * \param startAddress * \param noOfItems * \param values */ void ModBusWriteData( int slaveId, int functionCode, int startAddress, int noOfItems, std::vectorvalues ) override; /*! * \brief isConnected * \return */ bool isConnected() const override; /*! * \brief ErrorString * \param errnum * \return */ std::string ErrorString( int errnum ) const override; private: // Methods bool ModbusConnectRTU( const std::string &serialport, int baud, char parity, int dataBits, int stopBits, int RTS, int timeOut ); bool ModbusConnectTCP( const std::string &ip, int port, int timeOut = -1 ); void InitBuffers(); void ClearBuffers(); private: // Members ConnectionType m_connType { ConnectionType::CT_UNKNOWN }; ///> The type of connection this instance provides. Needed for administration bool m_connected { false }; ///> Shows if the connection is still active. modbus_t *m_modbus; ///> The actual low-level modbus instance as a raw-pointer. ( unique_pointer gives an error on this struct ) // Return value Buffers ( Room for improvement ) uint8_t *m_dest; uint16_t *m_dest16; };