From b84000816fd9fa89918880c78c25dd3e2c2e72e8 Mon Sep 17 00:00:00 2001 From: Erik Strand <erik.strand@cba.mit.edu> Date: Wed, 28 Nov 2018 00:14:43 -0500 Subject: [PATCH] Add working serial port enumeration test --- CMakeLists.txt | 37 +++++++++++++++++++++++++++++++++++++ main.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c32fb3c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required (VERSION 3.1) + +# Meta information about the project +set(META_PROJECT_NAME "serial_cpp") +set(META_PROJECT_DESCRIPTION "Interfacing with a microcontroller over a serial connection") +project (htmaa_shelves LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic") +if(CMAKE_COMPILER_IS_GNUCXX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1") +endif() + +# Let the compiler decide what to inline at link time. +cmake_policy(SET CMP0069 NEW) +include(CheckIPOSupported) +check_ipo_supported(RESULT ipo_supported OUTPUT error) +if (ipo_supported) + message(STATUS "IPO / LTO enabled") +else() + message(STATUS "IPO / LTO not supported: <${error}>") +endif() + +add_library(serialport SHARED IMPORTED) +# You can define two import-locations: one for debug and one for release. +set_target_properties(serialport PROPERTIES IMPORTED_LOCATION /usr/local/Cellar/libserialport/0.1.1/lib/libserialport.dylib) +target_include_directories(serialport INTERFACE /usr/local/Cellar/libserialport/0.1.1/include) + +add_executable(serial_cpp main.cpp) +target_link_libraries(serial_cpp serialport) +if (ipo_supported) + set_property(TARGET serial_cpp PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) +endif() + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..47c77be --- /dev/null +++ b/main.cpp @@ -0,0 +1,27 @@ +// Use of libserialport based on https://gist.github.com/Nixes/78e401234e66aa131547d7b78135271c + +#include <iostream> +#include <libserialport.h> + +void list_ports() { + int32_t i; + struct sp_port **ports; + + sp_return error = sp_list_ports(&ports); + if (error == SP_OK) { + for (i = 0; ports[i]; i++) { + std::cout << "Found port: " << sp_get_port_name(ports[i]) << '\n'; + } + sp_free_port_list(ports); + } else { + std::cout << "No serial devices detected\n"; + } +} + +int main() { + std::cout << "Searching for serial ports.\n"; + list_ports(); + + return 0; +} + -- GitLab