Blame view

src/ModbusAdapter.h 4.61 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"
5735b406   Peter M. Groen   Revert "Setting u...
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
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
24
       * \brief Default constructor
cadcf24a   Peter M. Groen   Setting up workin...
25
       */
46785270   Peter M. Groen   Setting up workin...
26
      explicit ModbusAdapter();
cadcf24a   Peter M. Groen   Setting up workin...
27
28
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
29
       * \brief Default destructor
cadcf24a   Peter M. Groen   Setting up workin...
30
       */
32017af3   Peter M. Groen   Setting up workin...
31
32
      virtual ~ModbusAdapter();
  
cadcf24a   Peter M. Groen   Setting up workin...
33
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
34
       * \brief /// Create a modbus connection, accepting a configuration object.
cadcf24a   Peter M. Groen   Setting up workin...
35
       */
5735b406   Peter M. Groen   Revert "Setting u...
36
      bool ModbusConnect( const ConnectionConfig &conncfg ) override;
cadcf24a   Peter M. Groen   Setting up workin...
37
38
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
39
40
       * \brief   ModbusDisconnect
       *          Disconnect from the serial bus or the TCP connection, freeing its resources
cadcf24a   Peter M. Groen   Setting up workin...
41
       */
b85a3e4a   Peter M. Groen   Setting up workin...
42
      bool ModbusDisconnect() override;
cadcf24a   Peter M. Groen   Setting up workin...
43
44
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
45
46
47
48
49
50
51
52
       * \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...
53
       */
46785270   Peter M. Groen   Setting up workin...
54
      modbusData ModbusReadData( int slaveId, int functionCode, int startAddress, int noOfItems ) override;
cadcf24a   Peter M. Groen   Setting up workin...
55
56
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
57
58
59
60
61
62
       * \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...
63
       */
46785270   Peter M. Groen   Setting up workin...
64
      modbusData ModbusReadHoldReg( int slaveId, int startAddress, int noOfItems ) override;
cadcf24a   Peter M. Groen   Setting up workin...
65
66
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
67
68
69
70
71
72
73
74
       * \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...
75
       */
46785270   Peter M. Groen   Setting up workin...
76
      void ModBusWriteData( int slaveId, int functionCode, int startAddress, int noOfItems, std::vector<int>values ) override;
cadcf24a   Peter M. Groen   Setting up workin...
77
78
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
79
80
       * \brief   Indicates if this connection is alive.
       * \return  True if alive, false if not.
cadcf24a   Peter M. Groen   Setting up workin...
81
       */
46785270   Peter M. Groen   Setting up workin...
82
      bool isConnected() const override;
cadcf24a   Peter M. Groen   Setting up workin...
83
84
  
      /*!
0df27b07   Peter M. Groen   Added doxygen com...
85
86
87
       * \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...
88
       */
5735b406   Peter M. Groen   Revert "Setting u...
89
      std::string ErrorString( int errnum ) const override;
cadcf24a   Peter M. Groen   Setting up workin...
90
91
  
  private:    // Methods
5735b406   Peter M. Groen   Revert "Setting u...
92
      bool    ModbusConnectRTU( const std::string &serialport, int baud, char parity, int dataBits, int stopBits, int RTS, int timeOut );
783ce3c5   Peter M. Groen   Setting up workin...
93
      bool    ModbusConnectTCP( const std::string &ip, int port, int timeOut = -1 );
cadcf24a   Peter M. Groen   Setting up workin...
94
  
5735b406   Peter M. Groen   Revert "Setting u...
95
96
97
      void    InitBuffers();
      void    ClearBuffers();
  
cadcf24a   Peter M. Groen   Setting up workin...
98
  private:    // Members
5735b406   Peter M. Groen   Revert "Setting u...
99
100
101
      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 )
cadcf24a   Peter M. Groen   Setting up workin...
102
  
5735b406   Peter M. Groen   Revert "Setting u...
103
104
105
      // Return value Buffers ( Room for improvement )
      uint8_t                     *m_dest;
      uint16_t                    *m_dest16;
32017af3   Peter M. Groen   Setting up workin...
106
  };