Blame view

src/ConnectionConfig.h 3.63 KB
32017af3   Peter M. Groen   Setting up workin...
1
2
3
  #pragma once
  
  #include <string>
cadcf24a   Peter M. Groen   Setting up workin...
4
  #include <unordered_map>
32017af3   Peter M. Groen   Setting up workin...
5
  
46785270   Peter M. Groen   Setting up workin...
6
7
8
  /*!
   * \brief The ConnectionPort enum
   */
cadcf24a   Peter M. Groen   Setting up workin...
9
  enum class ConnectionPort : unsigned int
32017af3   Peter M. Groen   Setting up workin...
10
  {
cadcf24a   Peter M. Groen   Setting up workin...
11
      CP_EXTERNAL     = 0,
32017af3   Peter M. Groen   Setting up workin...
12
13
14
15
      CP_IOBUS,
      CP_TCP
  };
  
46785270   Peter M. Groen   Setting up workin...
16
17
18
  /*!
   * \brief The Parity enum
   */
cadcf24a   Peter M. Groen   Setting up workin...
19
  enum class Parity : unsigned int
32017af3   Peter M. Groen   Setting up workin...
20
  {
cadcf24a   Peter M. Groen   Setting up workin...
21
22
23
24
25
      PAR_ODD,
      PAR_EVEN,
      PAR_NONE
  };
  
46785270   Peter M. Groen   Setting up workin...
26
27
28
  /*!
   * \brief The ConnectionType enum
   */
cadcf24a   Peter M. Groen   Setting up workin...
29
30
31
32
33
  enum class ConnectionType : unsigned int
  {
      CT_SERIAL,
      CT_TCP,
      CT_UNKNOWN
32017af3   Peter M. Groen   Setting up workin...
34
35
36
37
38
  };
  
  class ConnectionConfig
  {
  public:
46785270   Peter M. Groen   Setting up workin...
39
40
41
42
43
44
45
46
47
      /*!
       * \brief ConnectionConfig
       * \param port
       * \param baud
       * \param parity
       * \param dataBits
       * \param stopBits
       * \param timeOut
       */
cadcf24a   Peter M. Groen   Setting up workin...
48
49
50
51
52
53
54
55
56
      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 )
46785270   Peter M. Groen   Setting up workin...
57
          , m_conType( ConnectionType::CT_SERIAL )
cadcf24a   Peter M. Groen   Setting up workin...
58
      {}
32017af3   Peter M. Groen   Setting up workin...
59
  
46785270   Peter M. Groen   Setting up workin...
60
61
62
63
64
65
66
      /*!
       * \brief ConnectionConfig
       * \param port
       * \param ip
       * \param portnum
       * \param timeOut
       */
32017af3   Peter M. Groen   Setting up workin...
67
      ConnectionConfig( ConnectionPort port, const std::string &ip, int portnum, int timeOut = -1  )
cadcf24a   Peter M. Groen   Setting up workin...
68
69
70
71
72
73
74
75
          : 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 )
46785270   Peter M. Groen   Setting up workin...
76
          , m_conType( ConnectionType::CT_TCP )
cadcf24a   Peter M. Groen   Setting up workin...
77
      {}
32017af3   Peter M. Groen   Setting up workin...
78
  
cadcf24a   Peter M. Groen   Setting up workin...
79
      // Getters and Setters. Implemented to avoid outside meddling on the member variables.
b85a3e4a   Peter M. Groen   Setting up workin...
80
81
82
83
84
85
      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 )
cadcf24a   Peter M. Groen   Setting up workin...
86
  
b85a3e4a   Peter M. Groen   Setting up workin...
87
88
      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
32017af3   Peter M. Groen   Setting up workin...
89
  
b85a3e4a   Peter M. Groen   Setting up workin...
90
91
      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 )
32017af3   Peter M. Groen   Setting up workin...
92
  
46785270   Peter M. Groen   Setting up workin...
93
  private:
cadcf24a   Peter M. Groen   Setting up workin...
94
  
32017af3   Peter M. Groen   Setting up workin...
95
      /// Serial connections
cadcf24a   Peter M. Groen   Setting up workin...
96
      ConnectionPort  m_port;
32017af3   Peter M. Groen   Setting up workin...
97
98
99
100
101
102
      int             m_baudRate;
      Parity          m_parity;
      int             m_dataBits;
      int             m_stopBits;
  
      /// TCP connections
cadcf24a   Peter M. Groen   Setting up workin...
103
104
      std::string     m_ipaddress;
      int             m_portnumber;
32017af3   Peter M. Groen   Setting up workin...
105
  
cadcf24a   Peter M. Groen   Setting up workin...
106
107
      /// Generic
      int             m_timeOut;
46785270   Peter M. Groen   Setting up workin...
108
109
      ConnectionType  m_conType;
  
b85a3e4a   Peter M. Groen   Setting up workin...
110
111
112
      // ============================================================
      // == Change accordingly to the devicenames on your platform ==
      // ============================================================
cadcf24a   Peter M. Groen   Setting up workin...
113
114
115
116
117
118
119
120
121
122
123
124
      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' }
      };
32017af3   Peter M. Groen   Setting up workin...
125
  };