Commit 9bfbe36664d741496812d6f2a60763f069e2276c
1 parent
4de88caa
Adding basic 'Open' unit test.
Showing
3 changed files
with
48 additions
and
36 deletions
include/CppLinuxSerial/SerialPort.hpp
| @@ -12,6 +12,7 @@ | @@ -12,6 +12,7 @@ | ||
| 12 | #define SERIAL_PORT_SERIAL_PORT_H | 12 | #define SERIAL_PORT_SERIAL_PORT_H |
| 13 | 13 | ||
| 14 | // System headers | 14 | // System headers |
| 15 | +#include <string> | ||
| 15 | #include <fstream> // For file I/O (reading/writing to COM port) | 16 | #include <fstream> // For file I/O (reading/writing to COM port) |
| 16 | #include <sstream> | 17 | #include <sstream> |
| 17 | #include <termios.h> // POSIX terminal control definitions (struct termios) | 18 | #include <termios.h> // POSIX terminal control definitions (struct termios) |
| @@ -24,28 +25,31 @@ namespace CppLinuxSerial | @@ -24,28 +25,31 @@ namespace CppLinuxSerial | ||
| 24 | { | 25 | { |
| 25 | 26 | ||
| 26 | /// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class | 27 | /// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class |
| 27 | -enum class BaudRates | 28 | +enum class BaudRate |
| 28 | { | 29 | { |
| 29 | none, | 30 | none, |
| 30 | b9600, | 31 | b9600, |
| 31 | b57600 | 32 | b57600 |
| 32 | }; | 33 | }; |
| 33 | 34 | ||
| 34 | -//! @brief SerialPort object is used to perform rx/tx serial communication. | 35 | +/// \brief SerialPort object is used to perform rx/tx serial communication. |
| 35 | class SerialPort | 36 | class SerialPort |
| 36 | { | 37 | { |
| 37 | 38 | ||
| 38 | public: | 39 | public: |
| 39 | - //! @brief Constructor | 40 | + /// \brief Default constructor. |
| 40 | SerialPort(); | 41 | SerialPort(); |
| 41 | 42 | ||
| 43 | + /// \brief Constructor that sets up serial port with all required parameters. | ||
| 44 | + SerialPort(const std::string& device, BaudRate baudRate); | ||
| 45 | + | ||
| 42 | //! @brief Destructor | 46 | //! @brief Destructor |
| 43 | virtual ~SerialPort(); | 47 | virtual ~SerialPort(); |
| 44 | 48 | ||
| 45 | //! @brief Sets the file path to use for communications. The file path must be set before Open() is called, otherwise Open() will return an error. | 49 | //! @brief Sets the file path to use for communications. The file path must be set before Open() is called, otherwise Open() will return an error. |
| 46 | - void SetFilePath(std::string filePath); | 50 | + void SetDevice(const std::string& device); |
| 47 | 51 | ||
| 48 | - void SetBaudRate(BaudRates baudRate); | 52 | + void SetBaudRate(BaudRate baudRate); |
| 49 | 53 | ||
| 50 | //! @brief Controls what happens when Read() is called. | 54 | //! @brief Controls what happens when Read() is called. |
| 51 | //! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode. | 55 | //! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode. |
| @@ -61,8 +65,8 @@ class SerialPort | @@ -61,8 +65,8 @@ class SerialPort | ||
| 61 | //! @note Must call this before you can configure the COM port. | 65 | //! @note Must call this before you can configure the COM port. |
| 62 | void Open(); | 66 | void Open(); |
| 63 | 67 | ||
| 64 | - //! @brief Sets all settings for the com port to common defaults. | ||
| 65 | - void SetEverythingToCommonDefaults(); | 68 | + /// \brief Configures the tty device as a serial port. |
| 69 | + void ConfigureDeviceAsSerialPort(); | ||
| 66 | 70 | ||
| 67 | //! @brief Closes the COM port. | 71 | //! @brief Closes the COM port. |
| 68 | void Close(); | 72 | void Close(); |
| @@ -80,9 +84,9 @@ class SerialPort | @@ -80,9 +84,9 @@ class SerialPort | ||
| 80 | void Read(std::string *str); | 84 | void Read(std::string *str); |
| 81 | 85 | ||
| 82 | private: | 86 | private: |
| 83 | - std::string filePath; | 87 | + std::string device_; |
| 84 | 88 | ||
| 85 | - BaudRates baudRate; | 89 | + BaudRate baudRate_; |
| 86 | 90 | ||
| 87 | //! @brief The file descriptor for the open file. This gets written to when Open() is called. | 91 | //! @brief The file descriptor for the open file. This gets written to when Open() is called. |
| 88 | int fileDesc; | 92 | int fileDesc; |
src/SerialPort.cpp
| @@ -23,11 +23,13 @@ namespace mn { | @@ -23,11 +23,13 @@ namespace mn { | ||
| 23 | namespace CppLinuxSerial { | 23 | namespace CppLinuxSerial { |
| 24 | 24 | ||
| 25 | SerialPort::SerialPort() : | 25 | SerialPort::SerialPort() : |
| 26 | - filePath(std::string()), | ||
| 27 | - baudRate(BaudRates::none), | ||
| 28 | - fileDesc(0) | ||
| 29 | - { | ||
| 30 | - // Everything setup in initialiser list | 26 | + SerialPort("", BaudRate::none) |
| 27 | + { | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + SerialPort::SerialPort(const std::string& device, BaudRate baudRate) { | ||
| 31 | + device_ = device; | ||
| 32 | + baudRate_ = baudRate; | ||
| 31 | } | 33 | } |
| 32 | 34 | ||
| 33 | SerialPort::~SerialPort() | 35 | SerialPort::~SerialPort() |
| @@ -35,13 +37,12 @@ namespace CppLinuxSerial { | @@ -35,13 +37,12 @@ namespace CppLinuxSerial { | ||
| 35 | 37 | ||
| 36 | } | 38 | } |
| 37 | 39 | ||
| 38 | - void SerialPort::SetFilePath(std::string filePath) | ||
| 39 | - { | ||
| 40 | - // Save a pointer to the file path | ||
| 41 | - this->filePath = filePath; | 40 | + void SerialPort::SetDevice(const std::string& device) |
| 41 | + { | ||
| 42 | + device_ = device; | ||
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | - void SerialPort::SetBaudRate(BaudRates baudRate) | 45 | + void SerialPort::SetBaudRate(BaudRate baudRate) |
| 45 | { | 46 | { |
| 46 | 47 | ||
| 47 | // Get current termios struct | 48 | // Get current termios struct |
| @@ -49,15 +50,15 @@ namespace CppLinuxSerial { | @@ -49,15 +50,15 @@ namespace CppLinuxSerial { | ||
| 49 | 50 | ||
| 50 | switch(baudRate) | 51 | switch(baudRate) |
| 51 | { | 52 | { |
| 52 | - case BaudRates::none: | 53 | + case BaudRate::none: |
| 53 | // Error, baud rate has not been set yet | 54 | // Error, baud rate has not been set yet |
| 54 | - throw std::runtime_error("Baud rate for '" + this->filePath + "' cannot be set to none."); | 55 | + throw std::runtime_error("Baud rate for '" + device_ + "' cannot be set to none."); |
| 55 | break; | 56 | break; |
| 56 | - case BaudRates::b9600: | 57 | + case BaudRate::b9600: |
| 57 | cfsetispeed(&myTermios, B9600); | 58 | cfsetispeed(&myTermios, B9600); |
| 58 | cfsetospeed(&myTermios, B9600); | 59 | cfsetospeed(&myTermios, B9600); |
| 59 | break; | 60 | break; |
| 60 | - case BaudRates::b57600: | 61 | + case BaudRate::b57600: |
| 61 | cfsetispeed(&myTermios, B57600); | 62 | cfsetispeed(&myTermios, B57600); |
| 62 | cfsetospeed(&myTermios, B57600); | 63 | cfsetospeed(&myTermios, B57600); |
| 63 | break; | 64 | break; |
| @@ -68,15 +69,15 @@ namespace CppLinuxSerial { | @@ -68,15 +69,15 @@ namespace CppLinuxSerial { | ||
| 68 | 69 | ||
| 69 | // Setting the baudrate must of been successful, so we can now store this | 70 | // Setting the baudrate must of been successful, so we can now store this |
| 70 | // new value internally. This must be done last! | 71 | // new value internally. This must be done last! |
| 71 | - this->baudRate = baudRate; | 72 | + baudRate_ = baudRate; |
| 72 | } | 73 | } |
| 73 | 74 | ||
| 74 | void SerialPort::Open() | 75 | void SerialPort::Open() |
| 75 | { | 76 | { |
| 76 | 77 | ||
| 77 | - std::cout << "Attempting to open COM port \"" << this->filePath << "\"." << std::endl; | 78 | + std::cout << "Attempting to open COM port \"" << device_ << "\"." << std::endl; |
| 78 | 79 | ||
| 79 | - if(this->filePath.size() == 0) { | 80 | + if(device_.size() == 0) { |
| 80 | //this->sp->PrintError(SmartPrint::Ss() << "Attempted to open file when file path has not been assigned to."); | 81 | //this->sp->PrintError(SmartPrint::Ss() << "Attempted to open file when file path has not been assigned to."); |
| 81 | //return false; | 82 | //return false; |
| 82 | 83 | ||
| @@ -88,7 +89,7 @@ namespace CppLinuxSerial { | @@ -88,7 +89,7 @@ namespace CppLinuxSerial { | ||
| 88 | 89 | ||
| 89 | // O_RDONLY for read-only, O_WRONLY for write only, O_RDWR for both read/write access | 90 | // O_RDONLY for read-only, O_WRONLY for write only, O_RDWR for both read/write access |
| 90 | // 3rd, optional parameter is mode_t mode | 91 | // 3rd, optional parameter is mode_t mode |
| 91 | - this->fileDesc = open(this->filePath.c_str(), O_RDWR); | 92 | + this->fileDesc = open(device_.c_str(), O_RDWR); |
| 92 | 93 | ||
| 93 | // Check status | 94 | // Check status |
| 94 | if (this->fileDesc == -1) | 95 | if (this->fileDesc == -1) |
| @@ -116,9 +117,9 @@ namespace CppLinuxSerial { | @@ -116,9 +117,9 @@ namespace CppLinuxSerial { | ||
| 116 | this->SetTermios(settings); | 117 | this->SetTermios(settings); |
| 117 | } | 118 | } |
| 118 | 119 | ||
| 119 | - void SerialPort::SetEverythingToCommonDefaults() | 120 | + void SerialPort::ConfigureDeviceAsSerialPort() |
| 120 | { | 121 | { |
| 121 | - std::cout << "Configuring COM port \"" << this->filePath << "\"." << std::endl; | 122 | + std::cout << "Configuring COM port \"" << device_ << "\"." << std::endl; |
| 122 | 123 | ||
| 123 | //================== CONFIGURE ==================// | 124 | //================== CONFIGURE ==================// |
| 124 | 125 | ||
| @@ -137,7 +138,7 @@ namespace CppLinuxSerial { | @@ -137,7 +138,7 @@ namespace CppLinuxSerial { | ||
| 137 | 138 | ||
| 138 | //========================= SET UP BAUD RATES =========================// | 139 | //========================= SET UP BAUD RATES =========================// |
| 139 | 140 | ||
| 140 | - this->SetBaudRate(BaudRates::b57600); | 141 | + // this->SetBaudRate(BaudRate::b57600); |
| 141 | 142 | ||
| 142 | /* | 143 | /* |
| 143 | switch(this->baudRate) | 144 | switch(this->baudRate) |
| @@ -301,7 +302,7 @@ namespace CppLinuxSerial { | @@ -301,7 +302,7 @@ namespace CppLinuxSerial { | ||
| 301 | if(tcgetattr(this->fileDesc, &tty) != 0) | 302 | if(tcgetattr(this->fileDesc, &tty) != 0) |
| 302 | { | 303 | { |
| 303 | // Error occurred | 304 | // Error occurred |
| 304 | - std::cout << "Could not get terminal attributes for \"" << this->filePath << "\" - " << strerror(errno) << std::endl; | 305 | + std::cout << "Could not get terminal attributes for \"" << device_ << "\" - " << strerror(errno) << std::endl; |
| 305 | throw std::system_error(EFAULT, std::system_category()); | 306 | throw std::system_error(EFAULT, std::system_category()); |
| 306 | //return false; | 307 | //return false; |
| 307 | } | 308 | } |
| @@ -317,7 +318,7 @@ namespace CppLinuxSerial { | @@ -317,7 +318,7 @@ namespace CppLinuxSerial { | ||
| 317 | if(tcsetattr(this->fileDesc, TCSANOW, &myTermios) != 0) | 318 | if(tcsetattr(this->fileDesc, TCSANOW, &myTermios) != 0) |
| 318 | { | 319 | { |
| 319 | // Error occurred | 320 | // Error occurred |
| 320 | - std::cout << "Could not apply terminal attributes for \"" << this->filePath << "\" - " << strerror(errno) << std::endl; | 321 | + std::cout << "Could not apply terminal attributes for \"" << device_ << "\" - " << strerror(errno) << std::endl; |
| 321 | throw std::system_error(EFAULT, std::system_category()); | 322 | throw std::system_error(EFAULT, std::system_category()); |
| 322 | 323 | ||
| 323 | } | 324 | } |
test/unit/BasicTests.cpp
| @@ -6,17 +6,24 @@ using namespace mn::CppLinuxSerial; | @@ -6,17 +6,24 @@ using namespace mn::CppLinuxSerial; | ||
| 6 | 6 | ||
| 7 | namespace { | 7 | namespace { |
| 8 | 8 | ||
| 9 | - class BasicTest : public ::testing::Test { | 9 | + class BasicTests : public ::testing::Test { |
| 10 | protected: | 10 | protected: |
| 11 | 11 | ||
| 12 | - BasicTest() { | 12 | + BasicTests() { |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | - virtual ~BasicTest() { | 15 | + virtual ~BasicTests() { |
| 16 | } | 16 | } |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | - TEST_F(BasicTest, SimplePacket) { | 19 | + TEST_F(BasicTests, CanBeConstructed) { |
| 20 | + SerialPort serialPort; | ||
| 21 | + EXPECT_EQ(true, true); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + TEST_F(BasicTests, CanOpen) { | ||
| 25 | + SerialPort serialPort; | ||
| 26 | + serialPort.Open(); | ||
| 20 | EXPECT_EQ(true, true); | 27 | EXPECT_EQ(true, true); |
| 21 | } | 28 | } |
| 22 | 29 |