#pragma once #include #include /*! * \brief The ConnectionPort enum */ enum class ConnectionPort : unsigned int { CP_EXTERNAL = 0, CP_IOBUS, CP_TCP }; /*! * \brief The Parity enum */ enum class Parity : unsigned int { PAR_ODD, PAR_EVEN, PAR_NONE }; /*! * \brief The ConnectionType enum */ enum class ConnectionType : unsigned int { CT_SERIAL, CT_TCP, CT_UNKNOWN }; class ConnectionConfig { public: /*! * \brief ConnectionConfig * \param port * \param baud * \param parity * \param dataBits * \param stopBits * \param timeOut */ 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 * \param port * \param ip * \param portnum * \param timeOut */ 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. 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; int m_baudRate; Parity m_parity; int m_dataBits; int m_stopBits; /// TCP connections std::string m_ipaddress; int m_portnumber; /// Generic int m_timeOut; ConnectionType m_conType; // ============================================================ // == Change accordingly to the devicenames on your platform == // ============================================================ std::unordered_map m_portMap = { { ConnectionPort::CP_EXTERNAL, "/dev/ttyUSB0" }, { ConnectionPort::CP_IOBUS, "/dev/ttyUSB1" } }; std::unordered_map m_parityMap = { { Parity::PAR_EVEN, 'E' }, { Parity::PAR_ODD, 'O' }, { Parity::PAR_NONE, 'N' } }; };