Blame view

src/ModbusAdapter.h 4.54 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
  
527f96d4   Peter M. Groen   Setting up workin...
16
17
18
19
20
  
  /// @class  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
      /*!
527f96d4   Peter M. Groen   Setting up workin...
25
       * 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
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
30
       * 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
      /*!
527f96d4   Peter M. Groen   Setting up workin...
35
       * Create a modbus connection, accepting a configuration object.
cadcf24a   Peter M. Groen   Setting up workin...
36
       */
527f96d4   Peter M. Groen   Setting up workin...
37
      bool ModbusConnect( const ConnectionConfig &config ) override;
cadcf24a   Peter M. Groen   Setting up workin...
38
39
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
40
       * ModbusDisconnect
0df27b07   Peter M. Groen   Added doxygen com...
41
       *          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
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
46
47
48
       * 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.
0df27b07   Peter M. Groen   Added doxygen com...
49
       *                            Given by an enum, provided by the modbus-stack.
527f96d4   Peter M. Groen   Setting up workin...
50
51
52
       * @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.
0df27b07   Peter M. Groen   Added doxygen com...
53
       *                            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
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
58
59
60
61
62
       * 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.
0df27b07   Peter M. Groen   Added doxygen com...
63
       *                            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
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
68
69
70
       * 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
0df27b07   Peter M. Groen   Added doxygen com...
71
       *                            given by an enum, provided by the modbus-stack.
527f96d4   Peter M. Groen   Setting up workin...
72
73
74
       * @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
0df27b07   Peter M. Groen   Added doxygen com...
75
       *                            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
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
80
81
       * Indicates if this connection is alive.
       * @returns  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
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
86
87
88
       * 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
       */
5735b406   Peter M. Groen   Revert "Setting u...
90
      std::string ErrorString( int errnum ) const override;
cadcf24a   Peter M. Groen   Setting up workin...
91
92
  
  private:    // Methods
5735b406   Peter M. Groen   Revert "Setting u...
93
      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...
94
      bool    ModbusConnectTCP( const std::string &ip, int port, int timeOut = -1 );
cadcf24a   Peter M. Groen   Setting up workin...
95
  
5735b406   Peter M. Groen   Revert "Setting u...
96
97
98
      void    InitBuffers();
      void    ClearBuffers();
  
cadcf24a   Peter M. Groen   Setting up workin...
99
  private:    // Members
527f96d4   Peter M. Groen   Setting up workin...
100
101
102
      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...
103
  
5735b406   Peter M. Groen   Revert "Setting u...
104
      // Return value Buffers ( Room for improvement )
527f96d4   Peter M. Groen   Setting up workin...
105
106
      uint8_t                    *m_dest;
      uint16_t                   *m_dest16;
32017af3   Peter M. Groen   Setting up workin...
107
  };