ConnectionConfig.h 6.25 KB
/****************************************************************************
 * Copyright (c) 2022 Priva b.v.
 ****************************************************************************/
#pragma once

#include <string>
#include <unordered_map>

/*!
 * \brief   The ConnectionPort enum.
 *          CP_EXTERNAL - the first serial port
 *          CP_IOBUS    - The second serial port through RJ45 connector
 *          CP_TCP      - TCP Connections
 */
enum class ConnectionPort : unsigned int
{
    CP_EXTERNAL     = 0,
    CP_IOBUS,
    CP_TCP
};

/*!
 * \brief   The Parity enum.
 *          Used in a serial port context
 */
enum class Parity : unsigned int
{
    PAR_ODD,
    PAR_EVEN,
    PAR_NONE
};

/*!
 * \brief   The ConnectionType enum.
 *          Added for convenience, to distinguish between types.
 */
enum class ConnectionType : unsigned int
{
    CT_SERIAL,
    CT_TCP,
    CT_UNKNOWN
};

/*!
 * \brief The ConnectionConfig class holds all the information we need to establish a proper connection.
 *        It can be created by a configuration object and passed on to the ModBus stack.
 *        By using this class, all the connectioninfo is within its context for convenience.
 *
 *        Data can be accessed by their resp. getter-methods to avoid internal data-exposure
 *        according to the basic principles of object oriented programming. ( Abstraction, Encapsulation, Inheritance, and Polymorphism )
 */
class ConnectionConfig
{
public:
    /*!
     * \brief ConnectionConfig Constructor. Used to create a new serial connection.
     * \param port      - The portname given by its enum.
     * \param baud      - The port speed in a serial port context ( default = 115200 )
     * \param parity    - The parity. ( Default : None, no parity )
     * \param dataBits  - The number of databits. RTU uses 8 (0 - 255), ASCII uses 7 (0 - 127). Default is RTU
     * \param stopBits  - The number of stopbits used to detect the end of the frame. ( Default = 1 )
     * \param timeOut   - Timeout in .1 secs. See the termios documentation for deviations on this.
     */
    ConnectionConfig( ConnectionPort port, int baud = 115200, Parity parity = Parity::PAR_NONE, int dataBits = 8, int stopBits = 1, int timeOut = -1 )
        : m_port( port )
        , m_baudRate( baud )
        , m_parity( parity )
        , m_dataBits( dataBits )
        , m_stopBits( stopBits )
        , m_ipaddress()
        , m_portnumber( -1 )
        , m_timeOut( timeOut )
        , m_conType( ConnectionType::CT_SERIAL )
    {}

    /*!
     * \brief ConnectionConfig Constructor. Used to create a new TCP connection.
     * \param port      - The portname given by its enaum. ( Should be CP_TCP )
     * \param ip        - The ip address of the ModBus device we want to connect to.
     * \param portnum   - The portnumber the ModBus device is using
     * \param timeOut   - Timeout in which a modbus device should respond.
     */
    ConnectionConfig( ConnectionPort port, const std::string &ip, int portnum, int timeOut = -1  )
        : m_port( port )
        , m_baudRate( -1 )
        , m_parity( Parity::PAR_NONE )
        , m_dataBits( -1 )
        , m_stopBits( -1 )
        , m_ipaddress( ip )
        , m_portnumber( portnum )
        , m_timeOut( timeOut )
        , m_conType( ConnectionType::CT_TCP )
    {}

    // Getters and Setters. Implemented to avoid outside meddling on the member variables.
    std::string getPort() const { return m_portMap.at(m_port); }    ///< Get the translated portName.
    ConnectionPort getPortEnum() const { return m_port; }           ///< Get the portname Enum
    int getBaudRate() const { return m_baudRate; }                  ///< Get the given baudrate as int.
    char getParity() const { return m_parityMap.at(m_parity); }     ///< Get the translated parity.
    Parity getParityEnum() const { return m_parity; }               ///< Get the parity Enum
    int getDataBits() const { return m_dataBits; }                  ///< Get the number of databits ( 7 for ASCII, 8 for RTU )
    int getStopBits() const { return m_stopBits; }                  ///< Get the number of stopBits. ( de-facto = 1 )

    std::string getIpAddress() const { return m_ipaddress; }        ///< Get the ip-address as string
    int getTcpPort() const { return m_portnumber; }                 ///< Get the tcp portnumber as int

    int getTimeOut() const { return m_timeOut; }                    ///< Get the timeout as a multitude of 0.1 sec.
    ConnectionType getType() const { return m_conType; }            ///< Get the connection type ( Serial, TCP or Unknown )

private:

    /// Serial connections
    ConnectionPort  m_port;                                         ///< Member variable holding the portName Enum
    int             m_baudRate;                                     ///< Member variable holding the Serial port Baudrate
    Parity          m_parity;                                       ///< Member variable holding the Serial port Parity
    int             m_dataBits;                                     ///< Member variable holding the number of databits
    int             m_stopBits;                                     ///< Member variable holding the number of stopbits

    /// TCP connections
    std::string     m_ipaddress;                                    ///< Member variable holding the ip-address of the TCP-connection
    int             m_portnumber;                                   ///< Member variable holding the portnumber of the TCP-connection

    /// Generic
    int             m_timeOut;                                      ///< Member variable holding the timeOut value
    ConnectionType  m_conType;                                      ///< Member variable holding the connection type.

    /// Translation tables for portnames and parity.
    // ============================================================
    // == Change accordingly to the devicenames on your platform ==
    // ============================================================
    std::unordered_map<ConnectionPort, std::string> m_portMap =
    {
        { ConnectionPort::CP_EXTERNAL, "/dev/ttyUSB0" },
        { ConnectionPort::CP_IOBUS, "/dev/ttyUSB1" }
    };

    std::unordered_map<Parity, char> m_parityMap =
    {
        { Parity::PAR_EVEN, 'E' },
        { Parity::PAR_ODD, 'O' },
        { Parity::PAR_NONE, 'N' }
    };
};