Skip to content
Snippets Groups Projects
Commit b8400081 authored by Erik Strand's avatar Erik Strand
Browse files

Add working serial port enumeration test

parent 24628e01
No related branches found
No related tags found
No related merge requests found
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()
main.cpp 0 → 100644
// 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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment