From 9c29783bfc17aef37b4cde583204ca6103e8b8c6 Mon Sep 17 00:00:00 2001 From: Geoffrey Hunter Date: Fri, 24 Nov 2017 14:35:03 -0800 Subject: [PATCH] Added baud rate configure unit tests. --- include/CppLinuxSerial/SerialPort.hpp | 12 +++++++----- src/SerialPort.cpp | 104 ++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------- test/unit/BasicTests.cpp | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 76 deletions(-) diff --git a/include/CppLinuxSerial/SerialPort.hpp b/include/CppLinuxSerial/SerialPort.hpp index 52ab637..1d92102 100644 --- a/include/CppLinuxSerial/SerialPort.hpp +++ b/include/CppLinuxSerial/SerialPort.hpp @@ -22,11 +22,13 @@ namespace mn { namespace CppLinuxSerial { -/// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class + /// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class enum class BaudRate { - none, - b9600, - b57600 + B_9600, + B_38400, + B_57600, + B_115200, + CUSTOM }; enum class State { @@ -69,7 +71,7 @@ namespace mn { /// \brief Configures the tty device as a serial port. /// \warning Device must be open (valid file descriptor) when this is called. - void ConfigureDeviceAsSerialPort(); + void ConfigureTermios(); //! @brief Closes the COM port. void Close(); diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index 7d635a8..9d8fa8a 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -41,37 +41,13 @@ namespace CppLinuxSerial { void SerialPort::SetDevice(const std::string& device) { device_ = device; - ConfigureDeviceAsSerialPort(); + ConfigureTermios(); } void SerialPort::SetBaudRate(BaudRate baudRate) { - - // Get current termios struct - termios myTermios = this->GetTermios(); - - switch(baudRate) - { - case BaudRate::none: - // Error, baud rate has not been set yet - throw std::runtime_error("Baud rate for '" + device_ + "' cannot be set to none."); - break; - case BaudRate::b9600: - cfsetispeed(&myTermios, B9600); - cfsetospeed(&myTermios, B9600); - break; - case BaudRate::b57600: - cfsetispeed(&myTermios, B57600); - cfsetospeed(&myTermios, B57600); - break; - } - - // Save back to file - this->SetTermios(myTermios); - - // Setting the baudrate must of been successful, so we can now store this - // new value internally. This must be done last! baudRate_ = baudRate; + ConfigureTermios(); } void SerialPort::Open() @@ -102,7 +78,7 @@ namespace CppLinuxSerial { throw std::runtime_error("Could not open device " + device_ + ". Is the device name correct and do you have read/write permission?"); } - ConfigureDeviceAsSerialPort(); + ConfigureTermios(); std::cout << "COM port opened successfully." << std::endl; @@ -120,45 +96,13 @@ namespace CppLinuxSerial { this->SetTermios(settings); } - void SerialPort::ConfigureDeviceAsSerialPort() + void SerialPort::ConfigureTermios() { std::cout << "Configuring COM port \"" << device_ << "\"." << std::endl; //================== CONFIGURE ==================// - termios tty = this->GetTermios(); - /*struct termios tty; - memset(&tty, 0, sizeof(tty)); - - // Get current settings (will be stored in termios structure) - if(tcgetattr(this->fileDesc, &tty) != 0) - { - // Error occurred - this->sp->PrintError(SmartPrint::Ss() << "Could not get terminal attributes for \"" << this->filePath << "\" - " << strerror(errno)); - //return false; - return; - }*/ - - //========================= SET UP BAUD RATES =========================// - - // this->SetBaudRate(BaudRate::b57600); - - /* - switch(this->baudRate) - { - case BaudRates::none: - // Error, baud rate has not been set yet - this->sp->PrintError(SmartPrint::Ss() << "Baud rate for \"" << this->filePath << "\" has not been set."); - return; - case BaudRates::b9600: - cfsetispeed(&tty, B9600); - cfsetospeed(&tty, B9600); - break; - case BaudRates::b57600: - cfsetispeed(&tty, B57600); - cfsetospeed(&tty, B57600); - break; - }*/ + termios tty = GetTermios(); //================= (.c_cflag) ===============// @@ -170,6 +114,32 @@ namespace CppLinuxSerial { tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1) + //===================== BAUD RATE =================// + + switch(baudRate_) { + case BaudRate::B_9600: + cfsetispeed(&tty, B9600); + cfsetospeed(&tty, B9600); + break; + case BaudRate::B_38400: + cfsetispeed(&tty, B38400); + cfsetospeed(&tty, B38400); + break; + case BaudRate::B_57600: + cfsetispeed(&tty, B57600); + cfsetospeed(&tty, B57600); + break; + case BaudRate::B_115200: + cfsetispeed(&tty, B115200); + cfsetospeed(&tty, B115200); + break; + case BaudRate::CUSTOM: + // See https://gist.github.com/kennethryerson/f7d1abcf2633b7c03cf0 + throw std::runtime_error("Custom baud rate not yet supported."); + default: + throw std::runtime_error(std::string() + "baudRate passed to " + __PRETTY_FUNCTION__ + " unrecognized."); + } + //===================== (.c_oflag) =================// tty.c_oflag = 0; // No remapping, no delays @@ -180,7 +150,7 @@ namespace CppLinuxSerial { // c_cc[WMIN] sets the number of characters to block (wait) for when read() is called. // Set to 0 if you don't want read to block. Only meaningful when port set to non-canonical mode //tty.c_cc[VMIN] = 1; - this->SetNumCharsToWait(1); + SetNumCharsToWait(1); // c_cc[VTIME] sets the inter-character timer, in units of 0.1s. // Only meaningful when port is set to non-canonical mode @@ -219,21 +189,19 @@ namespace CppLinuxSerial { }*/ } - void SerialPort::SetNumCharsToWait(uint32_t numCharsToWait) - { + void SerialPort::SetNumCharsToWait(uint32_t numCharsToWait) { // Get current termios struct - termios myTermios = this->GetTermios(); + termios myTermios = GetTermios(); // Save the number of characters to wait for // to the control register myTermios.c_cc[VMIN] = numCharsToWait; // Save termios back - this->SetTermios(myTermios); + SetTermios(myTermios); } - void SerialPort::Write(const std::string& data) - { + void SerialPort::Write(const std::string& data) { if(fileDesc_ == 0) { //this->sp->PrintError(SmartPrint::Ss() << ); //return false; diff --git a/test/unit/BasicTests.cpp b/test/unit/BasicTests.cpp index 6253410..4319183 100644 --- a/test/unit/BasicTests.cpp +++ b/test/unit/BasicTests.cpp @@ -53,16 +53,43 @@ namespace { EXPECT_EQ(true, true); } + TEST_F(BasicTests, BaudRateSetCorrectly) { + SerialPort serialPort0(device0_, BaudRate::B_57600); + serialPort0.Open(); + auto retVal = TestUtil::Exec("stty -a -F " + device0_); + EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud")); + + serialPort0.SetBaudRate(BaudRate::B_115200); + retVal = TestUtil::Exec("stty -a -F " + device0_); + EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud")); + } + TEST_F(BasicTests, CanOpen) { - SerialPort serialPort0(device0_, BaudRate::b57600); + SerialPort serialPort0(device0_, BaudRate::B_57600); serialPort0.Open(); } TEST_F(BasicTests, ReadWrite) { - SerialPort serialPort0(device0_, BaudRate::b57600); + SerialPort serialPort0(device0_, BaudRate::B_57600); + serialPort0.Open(); + + SerialPort serialPort1(device1_, BaudRate::B_57600); + serialPort1.Open(); + + serialPort0.Write("Hello"); + + std::string readData; + serialPort1.Read(readData); + + ASSERT_EQ("Hello", readData); + } + + + TEST_F(BasicTests, ReadWriteDiffBaudRates) { + SerialPort serialPort0(device0_, BaudRate::B_9600); serialPort0.Open(); - SerialPort serialPort1(device1_, BaudRate::b57600); + SerialPort serialPort1(device1_, BaudRate::B_57600); serialPort1.Open(); serialPort0.Write("Hello"); -- libgit2 0.21.4