Commit dd70ec81 authored by Erik Strand's avatar Erik Strand

Updates for linux

And testing my new USB-equipped board (currently virtual serial).
parent 22d6fd49
...@@ -26,8 +26,10 @@ endif() ...@@ -26,8 +26,10 @@ endif()
add_library(serialport SHARED IMPORTED) add_library(serialport SHARED IMPORTED)
# You can define two import-locations: one for debug and one for release. # 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) #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) set_target_properties(serialport PROPERTIES IMPORTED_LOCATION /usr/lib/x86_64-linux-gnu/libserialport.so.0.1.0)
#target_include_directories(serialport INTERFACE /usr/local/Cellar/libserialport/0.1.1/include)
target_include_directories(serialport INTERFACE /usr/include)
add_executable(serial_cpp main.cpp) add_executable(serial_cpp main.cpp)
target_link_libraries(serial_cpp serialport) target_link_libraries(serial_cpp serialport)
......
...@@ -22,41 +22,64 @@ int main() { ...@@ -22,41 +22,64 @@ int main() {
std::cout << "Searching for serial ports.\n"; std::cout << "Searching for serial ports.\n";
list_ports(); list_ports();
const char* desired_port = "/dev/cu.usbserial-AC01YB5P"; //const char* desired_port = "/dev/cu.usbserial-AC01YB5P";
//const char* desired_port = "/dev/ttyUSB0";
const char* desired_port = "/dev/ttyACM0";
constexpr uint32_t baud_rate = 115200; constexpr uint32_t baud_rate = 115200;
constexpr uint32_t byte_buffer_size = 512; constexpr uint32_t byte_buffer_size = 512;
char byte_buffer[byte_buffer_size]; char byte_buffer[byte_buffer_size];
struct sp_port *port; struct sp_port *port;
std::cout << "Opening port " << desired_port << '\n'; std::cout << "Opening port " << desired_port << '\n';
sp_return error = sp_get_port_by_name(desired_port,&port); sp_return error = sp_get_port_by_name(desired_port, &port);
if (error == SP_OK) { if (error == SP_OK) {
error = sp_open(port, SP_MODE_READ); error = sp_open(port, SP_MODE_READ);
if (error == SP_OK) { if (error == SP_OK) {
sp_set_baudrate(port, baud_rate); sp_set_baudrate(port, baud_rate);
while (true) { while (true) {
int bytes_waiting = sp_input_waiting(port); int bytes_waiting = sp_input_waiting(port);
if (bytes_waiting > 0) { if (bytes_waiting > 0) {
std::cout << bytes_waiting << " bytes in the buffer: "; std::cout << bytes_waiting << " bytes in the buffer: ";
int byte_num = 0; int byte_num = 0;
byte_num = sp_nonblocking_read(port, byte_buffer, byte_buffer_size); byte_num = sp_nonblocking_read(port, byte_buffer, byte_buffer_size);
bool button_pressed = false; //bool button_pressed = false;
for (decltype(byte_num) i = 0; i < byte_num; ++i){ for (decltype(byte_num) i = 0; i < byte_num; ++i){
if (byte_buffer[i] == '1') { //if (byte_buffer[i] == '1') {
button_pressed = true; // button_pressed = true;
//}
std::cout << byte_buffer[i];
} }
std::cout << byte_buffer[i]; std::cout << '\n';
} /*
if (button_pressed) { if (button_pressed) {
std::cout << " the button is pressed!"; std::cout << " the button is pressed!";
}
std::cout << std::endl;
*/
} }
std::cout << std::endl;
} }
} // Execution can never make it here, but I'll close the port on principle.
// Execution can never make it here, but I'll close the port on principle. sp_close(port);
sp_close(port);
} else { } else {
std::cout << "Error opening serial device\n"; std::cout << "Error opening serial device\n";
switch(error) {
case SP_ERR_ARG:
std::cout << "Bad arguments\n";
break;
case SP_ERR_FAIL:
std::cout << "OS reported failure\n";
std::cout << sp_last_error_code() << '\n';
std::cout << sp_last_error_message() << '\n';
break;
case SP_ERR_SUPP:
std::cout << "Not supported\n";
break;
case SP_ERR_MEM:
std::cout << "OOM\n";
break;
default:
std::cout << "No idea\n";
}
} }
} else { } else {
std::cout << "Error finding serial device\n"; std::cout << "Error finding serial device\n";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment