diff --git a/examples/serial_example.cc b/examples/serial_example.cc index cd06803..e81e50e 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -29,7 +29,6 @@ #include "serial/serial.h" -using std::array; using std::string; using std::exception; using std::cout; @@ -47,15 +46,16 @@ void my_sleep(unsigned long milliseconds) { void enumerate_ports() { - vector > devices_found = serial::list_ports(); + vector devices_found = serial::list_ports(); - vector >::iterator iter = devices_found.begin(); + vector::iterator iter = devices_found.begin(); while( iter != devices_found.end() ) { - array device = *iter++; + serial::PortDescription device = *iter++; - printf( "(%s, %s, %s)\n", device[0].c_str(), device[1].c_str(), device[2].c_str() ); + printf( "(%s, %s, %s)\n", device.port.c_str(), device.friendly_name.c_str(), + device.hardware_id.c_str() ); } } diff --git a/include/serial/serial.h b/include/serial/serial.h index 0bc8db1..16f52ea 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -36,7 +36,6 @@ #ifndef SERIAL_H #define SERIAL_H -#include #include #include #include @@ -696,19 +695,30 @@ public: } }; +/*! + * Structure that describes a serial device. + */ +struct PortDescription { + + /*! Address of port (this can be passed to the constructor of Serial) */ + std::string port; + + /*! Has additional information when available */ + std::string friendly_name; + + /*! Hardware ID or "n/a" if not available */ + std::string hardware_id; + +}; /* Lists the serial ports available on the system * * Returns a vector of available serial ports, each represented - * by three strings: + * by a serial::PortDescription data structure: * - * - Port (this can be passed to the constructor of Serial) - * - Pretty port (has additional information when availabe) - * - Hardware ID or "n/a" if not avaiable - * - * \return vector of triplets (port, pretty port, hw id), one for each serial port + * \return vector of serial::PortDescription. */ -std::vector > +std::vector list_ports(); } // namespace serial diff --git a/src/impl/list_ports/list_ports_linux.cc b/src/impl/list_ports/list_ports_linux.cc index b437ac8..375d6ac 100644 --- a/src/impl/list_ports/list_ports_linux.cc +++ b/src/impl/list_ports/list_ports_linux.cc @@ -22,7 +22,7 @@ #include "serial/serial.h" -using std::array; +using serial::PortDescription; using std::istringstream; using std::ifstream; using std::getline; @@ -292,10 +292,10 @@ usb_sysfs_hw_string(const string& sysfs_path) return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str() ); } -vector > +vector serial::list_ports() { - vector > results; + vector results; vector search_globs; search_globs.push_back("/dev/ttyACM*"); @@ -318,11 +318,11 @@ serial::list_ports() string hardware_id = sysfs_info[1]; - array device_entry; - device_entry[0] = device; - device_entry[1] = friendly_name; - device_entry[2] = hardware_id; - + PortDescription device_entry; + device_entry.port = device; + device_entry.friendly_name = friendly_name; + device_entry.hardware_id = hardware_id; + results.push_back( device_entry ); } diff --git a/src/impl/list_ports/list_ports_win.cc b/src/impl/list_ports/list_ports_win.cc index 1d5939e..00cdb52 100644 --- a/src/impl/list_ports/list_ports_win.cc +++ b/src/impl/list_ports/list_ports_win.cc @@ -10,9 +10,8 @@ #include #include #include -#include -using std::array; +using serial::PortDescription using std::vector; using std::string; @@ -20,7 +19,7 @@ static const DWORD port_name_max_length = 256; static const DWORD friendly_name_max_length = 256; static const DWORD hardware_id_max_length = 256; -vector > +vector serial::list_ports() { decltype( serial::list_ports() ) devices_found; @@ -114,10 +113,10 @@ serial::list_ports() else hardware_id[0] = '\0'; - array port_entry; - port_entry[0] = port_name; - port_entry[1] = friendly_name; - port_entry[2] = hardware_id; + PortDescription port_entry; + port_entry.port = port_name; + port_entry.friendly_name = friendly_name; + port_entry.hardware_id = hardware_id; devices_found.push_back(port_entry); }