Commit 22d6fd49 authored by Erik Strand's avatar Erik Strand

Echo characters from the serial device

parent b8400081
...@@ -22,6 +22,46 @@ int main() { ...@@ -22,6 +22,46 @@ 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";
constexpr uint32_t baud_rate = 115200;
constexpr uint32_t byte_buffer_size = 512;
char byte_buffer[byte_buffer_size];
struct sp_port *port;
std::cout << "Opening port " << desired_port << '\n';
sp_return error = sp_get_port_by_name(desired_port,&port);
if (error == SP_OK) {
error = sp_open(port, SP_MODE_READ);
if (error == SP_OK) {
sp_set_baudrate(port, baud_rate);
while (true) {
int bytes_waiting = sp_input_waiting(port);
if (bytes_waiting > 0) {
std::cout << bytes_waiting << " bytes in the buffer: ";
int byte_num = 0;
byte_num = sp_nonblocking_read(port, byte_buffer, byte_buffer_size);
bool button_pressed = false;
for (decltype(byte_num) i = 0; i < byte_num; ++i){
if (byte_buffer[i] == '1') {
button_pressed = true;
}
std::cout << byte_buffer[i];
}
if (button_pressed) {
std::cout << " the button is pressed!";
}
std::cout << std::endl;
}
}
// Execution can never make it here, but I'll close the port on principle.
sp_close(port);
} else {
std::cout << "Error opening serial device\n";
}
} else {
std::cout << "Error finding serial device\n";
}
return 0; return 0;
} }
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