From 9f89596e85423d0d83121b0426d3b71e7b31766d Mon Sep 17 00:00:00 2001 From: William Woodall Date: Wed, 16 Apr 2014 15:07:36 -0700 Subject: [PATCH] return a vector > from list_ports instead of a vector > --- examples/serial_example.cc | 11 +++--- include/serial/serial.h | 15 ++++++++- src/impl/list_ports/list_ports_linux.cc | 45 +++++++++++++------------ src/impl/list_ports/list_ports_win.cc | 24 +++++++------ 4 files changed, 57 insertions(+), 38 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 34689cf..cd06803 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -2,12 +2,12 @@ * This example expects the serial port has a loopback on it. * * Alternatively, you could use an Arduino: - * + * *
  *  void setup() {
  *    Serial.begin();
  *  }
- * 
+ *
  *  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 > 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() )
 	{
-		vector device = *iter++;
+		array device = *iter++;
 
 		printf( "(%s, %s, %s)\n", device[0].c_str(), device[1].c_str(), device[2].c_str() );
 	}
diff --git a/include/serial/serial.h b/include/serial/serial.h
index d70440a..0bc8db1 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -36,6 +36,7 @@
 #ifndef SERIAL_H
 #define SERIAL_H
 
+#include 
 #include 
 #include 
 #include 
@@ -695,7 +696,19 @@ public:
   }
 };
 
-std::vector >
+
+/* 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 >
 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 c8b4872..b437ac8 100644
--- a/src/impl/list_ports/list_ports_linux.cc
+++ b/src/impl/list_ports/list_ports_linux.cc
@@ -1,7 +1,7 @@
-/* 
+/*
  * Copyright (c) 2014 Craig Lilley 
  * 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 
+vector
 glob(const vector& patterns)
 {
     vector 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::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 >
 serial::list_ports()
 {
-    vector > results;
+    vector > results;
 
     vector 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 devices_found = glob( search_globs );
 
     vector::iterator iter = devices_found.begin();
@@ -315,10 +318,10 @@ serial::list_ports()
 
         string hardware_id = sysfs_info[1];
 
-        vector device_entry;
-        device_entry.push_back( device );
-        device_entry.push_back( friendly_name );
-        device_entry.push_back( hardware_id );
+        array device_entry;
+        device_entry[0] = device;
+        device_entry[1] = friendly_name;
+        device_entry[2] = 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 d599794..1d5939e 100644
--- a/src/impl/list_ports/list_ports_win.cc
+++ b/src/impl/list_ports/list_ports_win.cc
@@ -1,16 +1,18 @@
-/* 
+/*
  * Copyright (c) 2014 Craig Lilley 
  * 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 
 #include 
 #include 
 #include 
+#include 
 
+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 >
 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 port_entry;
-		port_entry.push_back(port_name);
-		port_entry.push_back(friendly_name); 
-		port_entry.push_back(hardware_id);
+		array port_entry;
+		port_entry[0] = port_name;
+		port_entry[1] = friendly_name;
+		port_entry[2] = hardware_id;
 
 		devices_found.push_back(port_entry);
 	}