diff --git a/include/CppLinuxSerial/SerialPort.hpp b/include/CppLinuxSerial/SerialPort.hpp index b942d62..3839c3c 100644 --- a/include/CppLinuxSerial/SerialPort.hpp +++ b/include/CppLinuxSerial/SerialPort.hpp @@ -12,6 +12,7 @@ #define SERIAL_PORT_SERIAL_PORT_H // System headers +#include #include // For file I/O (reading/writing to COM port) #include #include // POSIX terminal control definitions (struct termios) @@ -24,28 +25,31 @@ namespace CppLinuxSerial { /// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class -enum class BaudRates +enum class BaudRate { none, b9600, b57600 }; -//! @brief SerialPort object is used to perform rx/tx serial communication. +/// \brief SerialPort object is used to perform rx/tx serial communication. class SerialPort { public: - //! @brief Constructor + /// \brief Default constructor. SerialPort(); + /// \brief Constructor that sets up serial port with all required parameters. + SerialPort(const std::string& device, BaudRate baudRate); + //! @brief Destructor virtual ~SerialPort(); //! @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. - void SetFilePath(std::string filePath); + void SetDevice(const std::string& device); - void SetBaudRate(BaudRates baudRate); + void SetBaudRate(BaudRate baudRate); //! @brief Controls what happens when Read() is called. //! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode. @@ -61,8 +65,8 @@ class SerialPort //! @note Must call this before you can configure the COM port. void Open(); - //! @brief Sets all settings for the com port to common defaults. - void SetEverythingToCommonDefaults(); + /// \brief Configures the tty device as a serial port. + void ConfigureDeviceAsSerialPort(); //! @brief Closes the COM port. void Close(); @@ -80,9 +84,9 @@ class SerialPort void Read(std::string *str); private: - std::string filePath; + std::string device_; - BaudRates baudRate; + BaudRate baudRate_; //! @brief The file descriptor for the open file. This gets written to when Open() is called. int fileDesc; diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index e46cbf8..9828452 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -23,11 +23,13 @@ namespace mn { namespace CppLinuxSerial { SerialPort::SerialPort() : - filePath(std::string()), - baudRate(BaudRates::none), - fileDesc(0) - { - // Everything setup in initialiser list + SerialPort("", BaudRate::none) + { + } + + SerialPort::SerialPort(const std::string& device, BaudRate baudRate) { + device_ = device; + baudRate_ = baudRate; } SerialPort::~SerialPort() @@ -35,13 +37,12 @@ namespace CppLinuxSerial { } - void SerialPort::SetFilePath(std::string filePath) - { - // Save a pointer to the file path - this->filePath = filePath; + void SerialPort::SetDevice(const std::string& device) + { + device_ = device; } - void SerialPort::SetBaudRate(BaudRates baudRate) + void SerialPort::SetBaudRate(BaudRate baudRate) { // Get current termios struct @@ -49,15 +50,15 @@ namespace CppLinuxSerial { switch(baudRate) { - case BaudRates::none: + case BaudRate::none: // Error, baud rate has not been set yet - throw std::runtime_error("Baud rate for '" + this->filePath + "' cannot be set to none."); + throw std::runtime_error("Baud rate for '" + device_ + "' cannot be set to none."); break; - case BaudRates::b9600: + case BaudRate::b9600: cfsetispeed(&myTermios, B9600); cfsetospeed(&myTermios, B9600); break; - case BaudRates::b57600: + case BaudRate::b57600: cfsetispeed(&myTermios, B57600); cfsetospeed(&myTermios, B57600); break; @@ -68,15 +69,15 @@ namespace CppLinuxSerial { // Setting the baudrate must of been successful, so we can now store this // new value internally. This must be done last! - this->baudRate = baudRate; + baudRate_ = baudRate; } void SerialPort::Open() { - std::cout << "Attempting to open COM port \"" << this->filePath << "\"." << std::endl; + std::cout << "Attempting to open COM port \"" << device_ << "\"." << std::endl; - if(this->filePath.size() == 0) { + if(device_.size() == 0) { //this->sp->PrintError(SmartPrint::Ss() << "Attempted to open file when file path has not been assigned to."); //return false; @@ -88,7 +89,7 @@ namespace CppLinuxSerial { // O_RDONLY for read-only, O_WRONLY for write only, O_RDWR for both read/write access // 3rd, optional parameter is mode_t mode - this->fileDesc = open(this->filePath.c_str(), O_RDWR); + this->fileDesc = open(device_.c_str(), O_RDWR); // Check status if (this->fileDesc == -1) @@ -116,9 +117,9 @@ namespace CppLinuxSerial { this->SetTermios(settings); } - void SerialPort::SetEverythingToCommonDefaults() + void SerialPort::ConfigureDeviceAsSerialPort() { - std::cout << "Configuring COM port \"" << this->filePath << "\"." << std::endl; + std::cout << "Configuring COM port \"" << device_ << "\"." << std::endl; //================== CONFIGURE ==================// @@ -137,7 +138,7 @@ namespace CppLinuxSerial { //========================= SET UP BAUD RATES =========================// - this->SetBaudRate(BaudRates::b57600); + // this->SetBaudRate(BaudRate::b57600); /* switch(this->baudRate) @@ -301,7 +302,7 @@ namespace CppLinuxSerial { if(tcgetattr(this->fileDesc, &tty) != 0) { // Error occurred - std::cout << "Could not get terminal attributes for \"" << this->filePath << "\" - " << strerror(errno) << std::endl; + std::cout << "Could not get terminal attributes for \"" << device_ << "\" - " << strerror(errno) << std::endl; throw std::system_error(EFAULT, std::system_category()); //return false; } @@ -317,7 +318,7 @@ namespace CppLinuxSerial { if(tcsetattr(this->fileDesc, TCSANOW, &myTermios) != 0) { // Error occurred - std::cout << "Could not apply terminal attributes for \"" << this->filePath << "\" - " << strerror(errno) << std::endl; + std::cout << "Could not apply terminal attributes for \"" << device_ << "\" - " << strerror(errno) << std::endl; throw std::system_error(EFAULT, std::system_category()); } diff --git a/test/unit/BasicTests.cpp b/test/unit/BasicTests.cpp index e92a8fc..06a96e6 100644 --- a/test/unit/BasicTests.cpp +++ b/test/unit/BasicTests.cpp @@ -6,17 +6,24 @@ using namespace mn::CppLinuxSerial; namespace { - class BasicTest : public ::testing::Test { + class BasicTests : public ::testing::Test { protected: - BasicTest() { + BasicTests() { } - virtual ~BasicTest() { + virtual ~BasicTests() { } }; - TEST_F(BasicTest, SimplePacket) { + TEST_F(BasicTests, CanBeConstructed) { + SerialPort serialPort; + EXPECT_EQ(true, true); + } + + TEST_F(BasicTests, CanOpen) { + SerialPort serialPort; + serialPort.Open(); EXPECT_EQ(true, true); }