Blame view

src/ModbusAdapter.h 2.91 KB
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"
cadcf24a   Peter M. Groen   Setting up workin...
9
  #include "modbus.h"
32017af3   Peter M. Groen   Setting up workin...
10
  
cadcf24a   Peter M. Groen   Setting up workin...
11
12
13
14
  // std
  #include <memory>
  #include <variant>
  #include <vector>
32017af3   Peter M. Groen   Setting up workin...
15
  
cadcf24a   Peter M. Groen   Setting up workin...
16
17
18
19
  /// @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...
20
21
22
  class ModbusAdapter : public IModbusAdapter
  {
  public:
cadcf24a   Peter M. Groen   Setting up workin...
23
24
25
      /*!
       * \brief ModbusAdapter
       */
46785270   Peter M. Groen   Setting up workin...
26
      explicit ModbusAdapter();
cadcf24a   Peter M. Groen   Setting up workin...
27
28
29
30
  
      /*!
       * \brief ~ModbusAdapter
       */
32017af3   Peter M. Groen   Setting up workin...
31
32
      virtual ~ModbusAdapter();
  
cadcf24a   Peter M. Groen   Setting up workin...
33
34
35
36
      /*!
       * \brief ModbusConnect
       * \param conncfg
       */
b85a3e4a   Peter M. Groen   Setting up workin...
37
      bool ModbusConnect( const ConnectionConfig &conncfg ) override;
cadcf24a   Peter M. Groen   Setting up workin...
38
39
40
41
  
      /*!
       * \brief ModbusDisconnect
       */
b85a3e4a   Peter M. Groen   Setting up workin...
42
      bool ModbusDisconnect() override;
cadcf24a   Peter M. Groen   Setting up workin...
43
44
45
46
47
48
49
  
      /*!
       * \brief ModbusReadData
       * \param slaveId
       * \param startAddress
       * \param noOfItems
       */
46785270   Peter M. Groen   Setting up workin...
50
      modbusData ModbusReadData( int slaveId, int functionCode, int startAddress, int noOfItems ) override;
cadcf24a   Peter M. Groen   Setting up workin...
51
52
53
54
55
56
57
58
  
      /*!
       * \brief ModbusReadHoldReg
       * \param slaveId
       * \param startAddress
       * \param noOfItems
       * \return
       */
46785270   Peter M. Groen   Setting up workin...
59
      modbusData ModbusReadHoldReg( int slaveId, int startAddress, int noOfItems ) override;
cadcf24a   Peter M. Groen   Setting up workin...
60
61
62
63
64
65
66
67
68
  
      /*!
       * \brief ModBusWriteData
       * \param slaveId
       * \param funtionCode
       * \param startAddress
       * \param noOfItems
       * \param values
       */
46785270   Peter M. Groen   Setting up workin...
69
      void ModBusWriteData( int slaveId, int functionCode, int startAddress, int noOfItems, std::vector<int>values ) override;
cadcf24a   Peter M. Groen   Setting up workin...
70
71
72
73
74
  
      /*!
       * \brief isConnected
       * \return
       */
46785270   Peter M. Groen   Setting up workin...
75
      bool isConnected() const override;
cadcf24a   Peter M. Groen   Setting up workin...
76
77
78
79
80
81
  
      /*!
       * \brief ErrorString
       * \param errnum
       * \return
       */
46785270   Peter M. Groen   Setting up workin...
82
      std::string ErrorString( int errnum ) const override;
cadcf24a   Peter M. Groen   Setting up workin...
83
84
  
  private:    // Methods
b85a3e4a   Peter M. Groen   Setting up workin...
85
86
      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 );
cadcf24a   Peter M. Groen   Setting up workin...
87
88
89
90
91
92
93
94
95
96
97
98
  
      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;
32017af3   Peter M. Groen   Setting up workin...
99
  };