From 5a48f672c934de8e173a1de6b262f79c43eac843 Mon Sep 17 00:00:00 2001 From: Geoffrey Hunter Date: Fri, 24 Nov 2017 13:56:25 -0800 Subject: [PATCH] Basic write/read data test between virtual serial ports is passing. --- include/CppLinuxSerial/SerialPort.hpp | 4 ++-- src/SerialPort.cpp | 30 +++++++++++------------------- test/unit/BasicTests.cpp | 6 ++++++ test/unit/CMakeLists.txt | 10 ++-------- test/unit/TestUtil.hpp | 91 ++----------------------------------------------------------------------------------------- 5 files changed, 23 insertions(+), 118 deletions(-) diff --git a/include/CppLinuxSerial/SerialPort.hpp b/include/CppLinuxSerial/SerialPort.hpp index 20d1b4b..52ab637 100644 --- a/include/CppLinuxSerial/SerialPort.hpp +++ b/include/CppLinuxSerial/SerialPort.hpp @@ -78,13 +78,13 @@ namespace mn { //! @param str Reference to an string containing the characters to write to the COM port. //! @throws {std::runtime_error} if filename has not been set. //! {std::system_error} if system write() operation fails. - void Write(std::string *str); + void Write(const std::string& data); //! @brief Use to read from the COM port. //! @param str Reference to a string that the read characters from the COM port will be saved to. //! @throws {std::runtime_error} if filename has not been set. //! {std::system_error} if system read() operation fails. - void Read(std::string *str); + void Read(std::string& data); private: diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index e0ebe67..7d635a8 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -232,21 +232,19 @@ namespace CppLinuxSerial { this->SetTermios(myTermios); } - void SerialPort::Write(std::string* str) + void SerialPort::Write(const std::string& data) { - if(this->fileDesc_ == 0) - { + if(fileDesc_ == 0) { //this->sp->PrintError(SmartPrint::Ss() << ); //return false; throw std::runtime_error("SendMsg called but file descriptor (fileDesc) was 0, indicating file has not been opened."); } - int writeResult = write(this->fileDesc_, str->c_str(), str->size()); + int writeResult = write(fileDesc_, data.c_str(), data.size()); // Check status - if (writeResult == -1) - { + if (writeResult == -1) { // Could not open COM port //this->sp->PrintError(SmartPrint::Ss() << "Unable to write to \"" << this->filePath << "\" - " << strerror(errno)); //return false; @@ -257,10 +255,9 @@ namespace CppLinuxSerial { // If code reaches here than write must of been successful } - void SerialPort::Read(std::string* str) + void SerialPort::Read(std::string& data) { - if(this->fileDesc_ == 0) - { + if(fileDesc_ == 0) { //this->sp->PrintError(SmartPrint::Ss() << "Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened."); //return false; throw std::runtime_error("Read() was called but file descriptor (fileDesc) was 0, indicating file has not been opened."); @@ -271,25 +268,20 @@ namespace CppLinuxSerial { memset (&buf, '\0', sizeof buf); // Read from file - int n = read(this->fileDesc_, &buf, sizeof(buf)); + int n = read(fileDesc_, &buf, sizeof(buf)); // Error Handling - if(n < 0) - { - // Could not open COM port - //this->sp->PrintError(SmartPrint::Ss() << "Unable to read from \"" << this->filePath << "\" - " << strerror(errno)); - //return false; - + if(n < 0) { + // Read was unsuccessful throw std::system_error(EFAULT, std::system_category()); } - if(n > 0) - { + if(n > 0) { //this->sp->PrintDebug(SmartPrint::Ss() << "\"" << n << "\" characters have been read from \"" << this->filePath << "\""); // Characters have been read buf[n] = '\0'; //printf("%s\r\n", buf); - str->append(buf); + data.append(buf); //std::cout << *str << " and size of string =" << str->size() << "\r\n"; } diff --git a/test/unit/BasicTests.cpp b/test/unit/BasicTests.cpp index d16b617..6253410 100644 --- a/test/unit/BasicTests.cpp +++ b/test/unit/BasicTests.cpp @@ -65,6 +65,12 @@ namespace { SerialPort serialPort1(device1_, BaudRate::b57600); serialPort1.Open(); + serialPort0.Write("Hello"); + + std::string readData; + serialPort1.Read(readData); + + ASSERT_EQ("Hello", readData); } } // namespace \ No newline at end of file diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index d9c403f..a84bbf1 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -19,7 +19,7 @@ file(GLOB_RECURSE CppLinuxSerialUnitTests_SRC -add_executable(CppLinuxSerialUnitTests ${CppLinuxSerialUnitTests_SRC} "run.sh") +add_executable(CppLinuxSerialUnitTests ${CppLinuxSerialUnitTests_SRC}) target_link_libraries(CppLinuxSerialUnitTests LINK_PUBLIC CppLinuxSerial ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) @@ -32,10 +32,4 @@ add_custom_target( add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests.touch - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests) - -add_custom_command(TARGET CppLinuxSerialUnitTests POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${CMAKE_CURRENT_SOURCE_DIR}/run.sh - $ - ) \ No newline at end of file + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests) \ No newline at end of file diff --git a/test/unit/TestUtil.hpp b/test/unit/TestUtil.hpp index 7f36b92..e580e04 100644 --- a/test/unit/TestUtil.hpp +++ b/test/unit/TestUtil.hpp @@ -23,82 +23,6 @@ using namespace std::literals; -#define READ 0 -#define WRITE 1 -FILE * popen2(std::string command, std::string type, int & pid) -{ - pid_t child_pid; - int fd[2]; - pipe(fd); - - if((child_pid = fork()) == -1) - { - perror("fork"); - exit(1); - } - - /* child process */ - if (child_pid == 0) - { - if (type == "r") - { - close(fd[READ]); //Close the READ end of the pipe since the child's fd is write-only - dup2(fd[WRITE], 1); //Redirect stdout to pipe - } - else - { - close(fd[WRITE]); //Close the WRITE end of the pipe since the child's fd is read-only - dup2(fd[READ], 0); //Redirect stdin to pipe - } - - setpgid(child_pid, child_pid); //Needed so negative PIDs can kill children of /bin/sh - execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL); - exit(0); - } - else - { - if (type == "r") - { - close(fd[WRITE]); //Close the WRITE end of the pipe since parent's fd is read-only - } - else - { - close(fd[READ]); //Close the READ end of the pipe since parent's fd is write-only - } - } - - pid = child_pid; - - if (type == "r") - { - return fdopen(fd[READ], "r"); - } - - return fdopen(fd[WRITE], "w"); -} - -int pclose2(FILE * fp, pid_t pid) -{ - int stat; - - fclose(fp); - while (waitpid(pid, &stat, 0) == -1) - { - if (errno != EINTR) - { - stat = -1; - break; - } - } - - return stat; -} - -struct ProcessInfo { - FILE* fp; - pid_t pid; -}; - namespace mn { namespace CppLinuxSerial { @@ -123,17 +47,6 @@ namespace mn { return result; } - void StartProcess(const std::string &cmd) { - std::array buffer; - std::string result; - int pid; - FILE * fp = popen2(cmd, "r", pid); - ProcessInfo processInfo; - processInfo.fp = fp; - processInfo.pid = pid; - processes_.push_back(processInfo); - } - void CreateVirtualSerialPortPair() { std::cout << "Creating virtual serial port pair..." << std::endl; // StartProcess("sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11"); @@ -144,6 +57,8 @@ namespace mn { // std::cout << "Finished creating virtual serial port pair." << std::endl; // std::system("./run.sh"); std::system("nohup sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11 &"); + auto pid = std::system("echo $!"); + std::cout << "pid = " << pid << std::endl; std::this_thread::sleep_for(1s); std::system("sudo chmod a+rw /dev/ttyS10"); std::system("sudo chmod a+rw /dev/ttyS11"); @@ -159,8 +74,6 @@ namespace mn { std::system("sudo pkill socat"); } - std::vector processes_; - }; } // namespace CppLinuxSerial } // namespace mn -- libgit2 0.21.4