1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 03:34:53 +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

@ -2,12 +2,12 @@
* This example expects the serial port has a loopback on it.
*
* Alternatively, you could use an Arduino:
*
*
* <pre>
* void setup() {
* Serial.begin(<insert your baudrate here>);
* }
*
*
* void loop() {
* if (Serial.available()) {
* Serial.write(Serial.read());
@ -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

@ -1,7 +1,7 @@
/*
/*
* Copyright (c) 2014 Craig Lilley <cralilley@gmail.com>
* This software is made available under the terms of the MIT licence.
* A copy of the licence can be obtained from:
* A copy of the licence can be obtained from:
* http://opensource.org/licenses/MIT
*/
@ -22,6 +22,7 @@
#include "serial/serial.h"
using std::array;
using std::istringstream;
using std::ifstream;
using std::getline;
@ -41,32 +42,32 @@ static string read_line(const string& file);
static string usb_sysfs_hw_string(const string& sysfs_path);
static string format(const char* format, ...);
vector<string>
vector<string>
glob(const vector<string>& patterns)
{
vector<string> paths_found;
if(patterns.size() == 0)
return paths_found;
glob_t glob_results;
int glob_retval = glob(patterns[0].c_str(), 0, NULL, &glob_results);
vector<string>::const_iterator iter = patterns.begin();
while(++iter != patterns.end())
{
glob_retval = glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results);
}
for(int path_index = 0; path_index < glob_results.gl_pathc; path_index++)
{
paths_found.push_back(glob_results.gl_pathv[path_index]);
}
globfree(&glob_results);
return paths_found;
}
@ -122,7 +123,7 @@ realpath(const string& path)
return result;
}
string
string
usb_sysfs_friendly_name(const string& sys_usb_path)
{
unsigned int device_number = 0;
@ -131,7 +132,7 @@ usb_sysfs_friendly_name(const string& sys_usb_path)
string manufacturer = read_line( sys_usb_path + "/manufacturer" );
string product = read_line( sys_usb_path + "/product" );
string product = read_line( sys_usb_path + "/product" );
string serial = read_line( sys_usb_path + "/serial" );
@ -231,7 +232,7 @@ format(const char* format, ...)
unsigned int loop_count = 0;
while(!done)
{
{
va_start(ap, format);
int return_value = vsnprintf(buffer, buffer_size_bytes, format, ap);
@ -291,16 +292,18 @@ 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 );
vector<string>::iterator iter = devices_found.begin();
@ -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

@ -1,16 +1,18 @@
/*
/*
* Copyright (c) 2014 Craig Lilley <cralilley@gmail.com>
* This software is made available under the terms of the MIT licence.
* A copy of the licence can be obtained from:
* A copy of the licence can be obtained from:
* http://opensource.org/licenses/MIT
*/
#include "serial/serial.h"
#include <windows.h>
#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;
@ -41,8 +43,8 @@ serial::list_ports()
// Get port name
HKEY hkey = SetupDiOpenDevRegKey(
device_info_set,
&device_info_data,
device_info_set,
&device_info_data,
DICS_FLAG_GLOBAL,
0,
DIREG_DEV,
@ -73,7 +75,7 @@ serial::list_ports()
if(strstr(port_name, "LPT") != NULL)
continue;
// Get port friendly name
char friendly_name[friendly_name_max_length];
@ -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);
}