Blame view

src/IModbusAdapter.h 3.53 KB
32017af3   Peter M. Groen   Setting up workin...
1
2
3
4
5
6
  /****************************************************************************
   * Copyright (c) 2022 Priva b.v.
   ****************************************************************************/
  
  #pragma once
  
cadcf24a   Peter M. Groen   Setting up workin...
7
8
9
10
11
12
13
14
15
16
17
  // std
  #include <string>
  #include <variant>
  #include <vector>
  
  using modbusData = std::vector<std::variant<uint8_t, uint16_t>>;
  
  // Forward Declaration
  class ConnectionConfig;
  
  /*!
527f96d4   Peter M. Groen   Setting up workin...
18
   * The IModbusAdapter class provides an abstract way of using
0df27b07   Peter M. Groen   Added doxygen com...
19
20
   *          the modbus stack. Implemented as a pure virtual, it cannot be instantiated.
   *          This represents a unique connection to either a serial bus or a TCP connection.
cadcf24a   Peter M. Groen   Setting up workin...
21
   */
32017af3   Peter M. Groen   Setting up workin...
22
23
  class IModbusAdapter
  {
cadcf24a   Peter M. Groen   Setting up workin...
24
25
  
  public:
0df27b07   Peter M. Groen   Added doxygen com...
26
27
      /// Default DTor.
      virtual ~IModbusAdapter() = default;
cadcf24a   Peter M. Groen   Setting up workin...
28
  
0df27b07   Peter M. Groen   Added doxygen com...
29
      /// Create a modbus connection, accepting a configuration object.
b85a3e4a   Peter M. Groen   Setting up workin...
30
      virtual bool ModbusConnect( const ConnectionConfig &conncfg ) = 0;
cadcf24a   Peter M. Groen   Setting up workin...
31
32
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
33
       * ModbusDisconnect
0df27b07   Peter M. Groen   Added doxygen com...
34
       *          Disconnect from the serial bus or the TCP connection, freeing its resources
cadcf24a   Peter M. Groen   Setting up workin...
35
       */
b85a3e4a   Peter M. Groen   Setting up workin...
36
      virtual bool ModbusDisconnect() = 0;
cadcf24a   Peter M. Groen   Setting up workin...
37
38
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
39
40
41
       * 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...
42
       *                            Given by an enum, provided by the modbus-stack.
527f96d4   Peter M. Groen   Setting up workin...
43
44
45
       * @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...
46
       *                            Empty if no data was received.
cadcf24a   Peter M. Groen   Setting up workin...
47
       */
46785270   Peter M. Groen   Setting up workin...
48
      virtual modbusData ModbusReadData( int slaveId, int functionCode, int startAddress, int noOfItems ) = 0;
cadcf24a   Peter M. Groen   Setting up workin...
49
50
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
51
52
53
54
55
       * 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...
56
       *                            Empty if no data was received.
cadcf24a   Peter M. Groen   Setting up workin...
57
58
59
60
       */
      virtual modbusData ModbusReadHoldReg( int slaveId, int startAddress, int noOfItems ) = 0;
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
61
62
63
       * 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...
64
       *                            given by an enum, provided by the modbus-stack.
527f96d4   Peter M. Groen   Setting up workin...
65
66
67
       * @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...
68
       *                            and they will be sent in sequence.
cadcf24a   Peter M. Groen   Setting up workin...
69
       */
46785270   Peter M. Groen   Setting up workin...
70
      virtual void ModBusWriteData( int slaveId, int functionCode, int startAddress, int noOfItems, std::vector<int>values ) = 0;
cadcf24a   Peter M. Groen   Setting up workin...
71
72
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
73
74
       * Indicates if this connection is alive.
       * @return  True if alive, false if not.
cadcf24a   Peter M. Groen   Setting up workin...
75
76
77
78
       */
      virtual bool isConnected() const = 0;
  
      /*!
527f96d4   Peter M. Groen   Setting up workin...
79
80
81
       * 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...
82
83
       */
      virtual std::string ErrorString( int errnum ) const = 0;
32017af3   Peter M. Groen   Setting up workin...
84
  };