main.cpp 3.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// 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();

Erik Strand's avatar
Erik Strand committed
25 26 27
    //const char* desired_port = "/dev/cu.usbserial-AC01YB5P";
    //const char* desired_port = "/dev/ttyUSB0";
    const char* desired_port = "/dev/ttyACM0";
28 29 30 31 32 33
    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';
Erik Strand's avatar
Erik Strand committed
34
    sp_return error = sp_get_port_by_name(desired_port, &port);
35
    if (error == SP_OK) {
Erik Strand's avatar
Erik Strand committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        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];
51
                    }
Erik Strand's avatar
Erik Strand committed
52 53 54 55 56 57 58
                    std::cout << '\n';
                    /*
                    if (button_pressed) {
                        std::cout << "   the button is pressed!";
                    }
                    std::cout << std::endl;
                    */
59 60
                }
            }
Erik Strand's avatar
Erik Strand committed
61 62
            // Execution can never make it here, but I'll close the port on principle.
            sp_close(port);
63 64
        } else {
            std::cout << "Error opening serial device\n";
Erik Strand's avatar
Erik Strand committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            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";
            }
83 84 85 86 87
        }
    } else {
        std::cout << "Error finding serial device\n";
    }

88 89 90
    return 0;
}