1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

return a vector<array<string, 3> > from list_ports

instead of a vector<vector<string> >
This commit is contained in:
William Woodall 2014-04-16 15:07:36 -07:00 committed by Craig Lilley
parent 72604cec0e
commit 9f89596e85
4 changed files with 57 additions and 38 deletions

View File

@ -29,6 +29,7 @@
#include "serial/serial.h"
using std::array;
using std::string;
using std::exception;
using std::cout;
@ -46,13 +47,13 @@ void my_sleep(unsigned long milliseconds) {
void enumerate_ports()
{
vector<vector<string> > devices_found = serial::list_ports();
vector<array<string, 3> > devices_found = serial::list_ports();
vector<vector<string> >::iterator iter = devices_found.begin();
vector<array<string, 3> >::iterator iter = devices_found.begin();
while( iter != devices_found.end() )
{
vector<string> device = *iter++;
array<string, 3> device = *iter++;
printf( "(%s, %s, %s)\n", device[0].c_str(), device[1].c_str(), device[2].c_str() );
}

View File

@ -36,6 +36,7 @@
#ifndef SERIAL_H
#define SERIAL_H
#include <array>
#include <limits>
#include <vector>
#include <string>
@ -695,7 +696,19 @@ public:
}
};
std::vector<std::vector<std::string> >
/* Lists the serial ports available on the system
*
* Returns a vector of available serial ports, each represented
* by three strings:
*
* - 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
*/
std::vector<std::array<std::string, 3> >
list_ports();
} // namespace serial

View File

@ -22,6 +22,7 @@
#include "serial/serial.h"
using std::array;
using std::istringstream;
using std::ifstream;
using std::getline;
@ -291,15 +292,17 @@ 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<string> >
vector<array<string, 3> >
serial::list_ports()
{
vector<vector<string> > results;
vector<array<string, 3> > results;
vector<string> search_globs;
search_globs.push_back("/dev/ttyACM*");
search_globs.push_back("/dev/ttyS*");
search_globs.push_back("/dev/ttyUSB*");
search_globs.push_back("/dev/tty.*");
search_globs.push_back("/dev/cu.*");
vector<string> devices_found = glob( search_globs );
@ -315,10 +318,10 @@ serial::list_ports()
string hardware_id = sysfs_info[1];
vector<string> device_entry;
device_entry.push_back( device );
device_entry.push_back( friendly_name );
device_entry.push_back( hardware_id );
array<string, 3> device_entry;
device_entry[0] = device;
device_entry[1] = friendly_name;
device_entry[2] = hardware_id;
results.push_back( device_entry );

View File

@ -10,7 +10,9 @@
#include <SetupAPI.h>
#include <devguid.h>
#include <cstring>
#include <array>
using std::array;
using std::vector;
using std::string;
@ -18,7 +20,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<string>>
vector<array<string, 3> >
serial::list_ports()
{
decltype( serial::list_ports() ) devices_found;
@ -112,10 +114,10 @@ serial::list_ports()
else
hardware_id[0] = '\0';
vector<string> port_entry;
port_entry.push_back(port_name);
port_entry.push_back(friendly_name);
port_entry.push_back(hardware_id);
array<string, 3> port_entry;
port_entry[0] = port_name;
port_entry[1] = friendly_name;
port_entry[2] = hardware_id;
devices_found.push_back(port_entry);
}