mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
Refactored list_ports to return a vector of serial::PortDescription.
This commit is contained in:
parent
9f89596e85
commit
b8479822f7
@ -29,7 +29,6 @@
|
|||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
|
||||||
using std::array;
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::exception;
|
using std::exception;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
@ -47,15 +46,16 @@ void my_sleep(unsigned long milliseconds) {
|
|||||||
|
|
||||||
void enumerate_ports()
|
void enumerate_ports()
|
||||||
{
|
{
|
||||||
vector<array<string, 3> > devices_found = serial::list_ports();
|
vector<serial::PortDescription> devices_found = serial::list_ports();
|
||||||
|
|
||||||
vector<array<string, 3> >::iterator iter = devices_found.begin();
|
vector<serial::PortDescription>::iterator iter = devices_found.begin();
|
||||||
|
|
||||||
while( iter != devices_found.end() )
|
while( iter != devices_found.end() )
|
||||||
{
|
{
|
||||||
array<string, 3> 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() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -36,7 +36,6 @@
|
|||||||
#ifndef SERIAL_H
|
#ifndef SERIAL_H
|
||||||
#define SERIAL_H
|
#define SERIAL_H
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -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
|
/* Lists the serial ports available on the system
|
||||||
*
|
*
|
||||||
* Returns a vector of available serial ports, each represented
|
* 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)
|
* \return vector of serial::PortDescription.
|
||||||
* - 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> >
|
std::vector<PortDescription>
|
||||||
list_ports();
|
list_ports();
|
||||||
|
|
||||||
} // namespace serial
|
} // namespace serial
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
|
||||||
using std::array;
|
using serial::PortDescription;
|
||||||
using std::istringstream;
|
using std::istringstream;
|
||||||
using std::ifstream;
|
using std::ifstream;
|
||||||
using std::getline;
|
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() );
|
return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<array<string, 3> >
|
vector<PortDescription>
|
||||||
serial::list_ports()
|
serial::list_ports()
|
||||||
{
|
{
|
||||||
vector<array<string, 3> > results;
|
vector<PortDescription> results;
|
||||||
|
|
||||||
vector<string> search_globs;
|
vector<string> search_globs;
|
||||||
search_globs.push_back("/dev/ttyACM*");
|
search_globs.push_back("/dev/ttyACM*");
|
||||||
@ -318,10 +318,10 @@ serial::list_ports()
|
|||||||
|
|
||||||
string hardware_id = sysfs_info[1];
|
string hardware_id = sysfs_info[1];
|
||||||
|
|
||||||
array<string, 3> device_entry;
|
PortDescription device_entry;
|
||||||
device_entry[0] = device;
|
device_entry.port = device;
|
||||||
device_entry[1] = friendly_name;
|
device_entry.friendly_name = friendly_name;
|
||||||
device_entry[2] = hardware_id;
|
device_entry.hardware_id = hardware_id;
|
||||||
|
|
||||||
results.push_back( device_entry );
|
results.push_back( device_entry );
|
||||||
|
|
||||||
|
|||||||
@ -10,9 +10,8 @@
|
|||||||
#include <SetupAPI.h>
|
#include <SetupAPI.h>
|
||||||
#include <devguid.h>
|
#include <devguid.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <array>
|
|
||||||
|
|
||||||
using std::array;
|
using serial::PortDescription
|
||||||
using std::vector;
|
using std::vector;
|
||||||
using std::string;
|
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 friendly_name_max_length = 256;
|
||||||
static const DWORD hardware_id_max_length = 256;
|
static const DWORD hardware_id_max_length = 256;
|
||||||
|
|
||||||
vector<array<string, 3> >
|
vector<PortDescription>
|
||||||
serial::list_ports()
|
serial::list_ports()
|
||||||
{
|
{
|
||||||
decltype( serial::list_ports() ) devices_found;
|
decltype( serial::list_ports() ) devices_found;
|
||||||
@ -114,10 +113,10 @@ serial::list_ports()
|
|||||||
else
|
else
|
||||||
hardware_id[0] = '\0';
|
hardware_id[0] = '\0';
|
||||||
|
|
||||||
array<string, 3> port_entry;
|
PortDescription port_entry;
|
||||||
port_entry[0] = port_name;
|
port_entry.port = port_name;
|
||||||
port_entry[1] = friendly_name;
|
port_entry.friendly_name = friendly_name;
|
||||||
port_entry[2] = hardware_id;
|
port_entry.hardware_id = hardware_id;
|
||||||
|
|
||||||
devices_found.push_back(port_entry);
|
devices_found.push_back(port_entry);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user