Commit 34780daf9808893df633f8d018338f4db15cc258
1 parent
ad1770f8
Added InWaiting and option for shared library (CMake)
- InWaiting is an equivalent of Serial.available() (On Arduino), used to get number of bytes available in buffer and if it is relevant to read from it - Sometimes during usage it bothered me that this lib is STATIC, so I made this option. (Haven't tested, but compiled just fine)
Showing
3 changed files
with
22 additions
and
1 deletions
include/CppLinuxSerial/SerialPort.hpp
| ... | ... | @@ -168,6 +168,9 @@ namespace mn { |
| 168 | 168 | /// \throws CppLinuxSerial::Exception if state != OPEN. |
| 169 | 169 | void ReadBinary(std::vector<uint8_t>& data); |
| 170 | 170 | |
| 171 | + /// \brief Use to get number of bytes available in buffer | |
| 172 | + /// \throws CppLinuxSerial::Exception if state != OPEN. | |
| 173 | + int32_t InWaiting(); | |
| 171 | 174 | private: |
| 172 | 175 | |
| 173 | 176 | /// \brief Returns a populated termios structure for the passed in file descriptor. | ... | ... |
src/CMakeLists.txt
| ... | ... | @@ -6,7 +6,16 @@ file(GLOB_RECURSE CppLinuxSerial_SRC |
| 6 | 6 | file(GLOB_RECURSE CppLinuxSerial_HEADERS |
| 7 | 7 | "${CMAKE_SOURCE_DIR}/include/*.hpp") |
| 8 | 8 | |
| 9 | -add_library(CppLinuxSerial ${CppLinuxSerial_SRC} ${CppLinuxSerial_HEADERS}) | |
| 9 | +option(SERIAL_BUILD_SHARED_LIBS "Build CppLinuxSerial shared library" OFF) | |
| 10 | + | |
| 11 | +if (SERIAL_BUILD_SHARED_LIBS) | |
| 12 | + set(LibType SHARED) | |
| 13 | +else() | |
| 14 | + set(LibType STATIC) | |
| 15 | +endif() | |
| 16 | + | |
| 17 | +add_library(CppLinuxSerial ${LibType} ${CppLinuxSerial_SRC} ${CppLinuxSerial_HEADERS}) | |
| 18 | + | |
| 10 | 19 | |
| 11 | 20 | target_include_directories(CppLinuxSerial PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>" |
| 12 | 21 | "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>") | ... | ... |
src/SerialPort.cpp
| ... | ... | @@ -629,6 +629,15 @@ namespace CppLinuxSerial { |
| 629 | 629 | THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called while state == OPEN."); |
| 630 | 630 | timeout_ms_ = timeout_ms; |
| 631 | 631 | } |
| 632 | + | |
| 633 | + int32_t SerialPort::InWaiting() { | |
| 634 | + if(state_ != State::OPEN) | |
| 635 | + THROW_EXCEPT(std::string() + __PRETTY_FUNCTION__ + " called but state != OPEN. Please call Open() first."); | |
| 636 | + int32_t ret = 0; | |
| 637 | + ioctl(fileDesc_, FIONREAD, &ret); | |
| 638 | + return ret; | |
| 639 | + | |
| 640 | + } | |
| 632 | 641 | |
| 633 | 642 | } // namespace CppLinuxSerial |
| 634 | 643 | } // namespace mn | ... | ... |