diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f8564b..5ed05b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [v2.4.0] - 2022-02-12 + +- Added `Available()` method to return number of bytes ready to be read from the receive buffer (thanks lotricekCZ). +- Added CMake option for shared library (thanks lotricekCZ). + ## [v2.3.0] - 2021-12-23 - Added support for setting the num. data bits. diff --git a/include/CppLinuxSerial/SerialPort.hpp b/include/CppLinuxSerial/SerialPort.hpp index 7f4078f..62eb88e 100644 --- a/include/CppLinuxSerial/SerialPort.hpp +++ b/include/CppLinuxSerial/SerialPort.hpp @@ -168,10 +168,12 @@ namespace mn { /// \throws CppLinuxSerial::Exception if state != OPEN. void ReadBinary(std::vector& data); - private: + /// \brief Use to get number of bytes available in receive buffer. + /// \returns The number of bytes available in the receive buffer (ready to be read). + /// \throws CppLinuxSerial::Exception if state != OPEN. + int32_t Available(); - /// \brief Returns a populated termios structure for the passed in file descriptor. - // termios GetTermios(); + private: /// \brief Configures the tty device as a serial port. /// \warning Device must be open (valid file descriptor) when this is called. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 43e5b94..83bdfde 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,7 +6,16 @@ file(GLOB_RECURSE CppLinuxSerial_SRC file(GLOB_RECURSE CppLinuxSerial_HEADERS "${CMAKE_SOURCE_DIR}/include/*.hpp") -add_library(CppLinuxSerial ${CppLinuxSerial_SRC} ${CppLinuxSerial_HEADERS}) +option(SERIAL_BUILD_SHARED_LIBS "Build CppLinuxSerial shared library" OFF) + +if (SERIAL_BUILD_SHARED_LIBS) + set(LibType SHARED) +else() + set(LibType STATIC) +endif() + +add_library(CppLinuxSerial ${LibType} ${CppLinuxSerial_SRC} ${CppLinuxSerial_HEADERS}) + target_include_directories(CppLinuxSerial PUBLIC "$" "$") diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index 33242c9..bf168e8 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -629,6 +629,15 @@ namespace CppLinuxSerial { THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called while state == OPEN."); timeout_ms_ = timeout_ms; } + + int32_t SerialPort::Available() { + if(state_ != State::OPEN) + THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first."); + int32_t ret = 0; + ioctl(fileDesc_, FIONREAD, &ret); + return ret; + + } } // namespace CppLinuxSerial } // namespace mn