diff --git a/src/modbusbase.h b/src/modbusbase.h index 9b517c4..50ee36d 100644 --- a/src/modbusbase.h +++ b/src/modbusbase.h @@ -212,10 +212,10 @@ private: // Members (Giggity!) bool m_connected{}; uint32_t m_msg_id{}; int m_slaveId{}; - bool m_error{}; - int m_error_number{}; - std::string m_error_message; - unsigned int m_max_message_length; + bool m_error{false}; + int m_error_number{0}; + std::string m_error_message = ""; + unsigned int m_max_message_length = 254; }; diff --git a/src/modbusrtu.cpp b/src/modbusrtu.cpp index 69aa03e..42a264b 100644 --- a/src/modbusrtu.cpp +++ b/src/modbusrtu.cpp @@ -17,15 +17,15 @@ using namespace osdev::components::modbus; -ModbusRtu::ModbusRtu( const ConnectionConfig &conf ) - : m_conConfig( conf ) +ModbusRtu::ModbusRtu(const ConnectionConfig &conf) + : m_conConfig(conf) , m_socket( -1 ) { } bool ModbusRtu::Connect() { - m_socket = open( m_conConfig.getPortName().c_str(), O_RDWR); + m_socket = open(m_conConfig.getPortName().c_str(), O_RDWR); if(m_socket == -1) { return false; @@ -36,16 +36,16 @@ bool ModbusRtu::Connect() // Read in existing settings, and handle any error if( tcgetattr(m_socket, &l_tty) != 0 ) { - // Replace later on with a logger line. + //TODO: Replace later on with a logger line. return false; } /* Control modes */ - l_tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common) - l_tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common) + l_tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity + l_tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication l_tty.c_cflag &= ~CSIZE; // Clear all bits that set the data field - l_tty.c_cflag |= CS8; // 8 bits per byte (most common) - l_tty.c_cflag &= ~CRTSCTS; // Disable RTS / CTS hardware flow control (most common) + l_tty.c_cflag |= CS8; // 8 bits per byte + l_tty.c_cflag &= ~CRTSCTS; // Disable RTS / CTS hardware flow control l_tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1) /* local modes */ diff --git a/src/modbusrtu.h b/src/modbusrtu.h index 00cfc6c..6b06758 100644 --- a/src/modbusrtu.h +++ b/src/modbusrtu.h @@ -18,22 +18,10 @@ public: explicit ModbusRtu( const ConnectionConfig &conf ); virtual ~ModbusRtu() {} + /*! Implementation of ModbusBase */ virtual bool Connect() override; virtual bool Close() override; - - /*! - * \brief modbusSend - * \param to_send - * \param length - * \return - */ virtual int modbusSend(uint8_t *to_send, size_t length) override; - - /*! - * \brief modbusReceive - * \param buffer - * \return - */ virtual int modbusReceive(uint8_t *buffer) override; private: diff --git a/tests/connectionconfigtest.cpp b/tests/connectionconfigtest.cpp index 1c96be9..0a46ee4 100644 --- a/tests/connectionconfigtest.cpp +++ b/tests/connectionconfigtest.cpp @@ -23,5 +23,11 @@ TEST(ConnectionConfigTest, SerialPortConfig) oConfig.setPortName( "/dev/ttyUSB0" ); // Test all parameters - + EXPECT_EQ(oConfig.getBaudRate(), B9600); + EXPECT_EQ(oConfig.getConnectionType(), ConnectionConfig::E_CONNECTIONTYPE::SERIAL); + EXPECT_EQ(oConfig.getDataBits(), 8); + EXPECT_EQ(oConfig.getStopBits(), 1); + EXPECT_EQ(oConfig.getFrameTimeout(), 10); + EXPECT_EQ(oConfig.getParity(), ConnectionConfig::E_PARITY::NONE); + EXPECT_EQ(oConfig.getPortName(), "/dev/ttyUSB0"); }