ofSerial additions

While this was originally posted way back in another thread http://forum.openframeworks.cc/t/i-want-ofserialread-back-please/320/0, I thought I would open up the discussion here in Extend as it seems to be the more appropriate forum section.

JROGE has implemented the following code to enable not only printing of serial device lists, but allowing access to the list. This is extremely useful for end-user applications where users must select a serial device to connect to via the GUI, and/or whenever your serial-device order changes.

When this was originally posted in 2007, Zach had said they were considering adding this functionality into the ofSerial class, upon further discussion with Theo. I don’t mind making the additions in my local copy of oF myself for a project I am working on, but I do think this functionality can be extremely useful for others, and would like to also maintain as much compatibility with the current release of oF as possible.

Any thoughts guys?

Cheers

  
  
Code:  
//----------------------------------------------------------------  
list<string> ofSerial::listDevices(){  
  
    list<string> deviceList;  
  
   //---------------------------------------------  
   #if defined( TARGET_OSX ) || defined( TARGET_LINUX )  
   //---------------------------------------------  
  
      //----------------------------------------------------  
      //We will find serial devices by listing the directory  
  
      DIR *dir;  
      struct dirent *entry;  
      dir = opendir("/dev");  
      string str         = "";  
      string device      = "";  
  
  
      if (dir == NULL){  
         printf("ofSerial: error listing devices in /dev\n");  
      } else {  
  
         while ((entry = readdir(dir)) != NULL){  
            str = (char *)entry->d_name;  
            if( str.substr(0,3) == "cu." ){  
               //printf("device %i -- %s\n", count, str.c_str());  
               char str[500];  
                    sprintf(str,"device %d -- %s",count,str.c_str());  
               deviceList.push_back(string(str));  
               count++;  
            }  
         }  
      }  
  
   //---------------------------------------------  
    #endif  
    //---------------------------------------------  
  
   //---------------------------------------------  
   #ifdef TARGET_WIN32  
   //---------------------------------------------  
  
      enumerateWin32Ports();  
      //printf("ofSerial: listing devices (%i total)\n", nPorts);  
      for (int i = 0; i < nPorts; i++){  
            char str[500];  
            sprintf(str,"device %d -- %s",i,portNamesFriendly[i]);  
            deviceList.push_back(string(str));  
         //printf("device %i -- %s\n", i, portNamesFriendly[i]);  
      }  
  
   //---------------------------------------------  
    #endif  
    //---------------------------------------------  
  
    return deviceList;  
}  
  
  
void ofSerial::enumerateDevices(){  
    list<string> devices = listDevices();  
    printf("ofSerial: listing devices (%i totally)\n",devices.size());  
  
    for(list<string>::iterator it = devices.begin(); it != devices.end(); it++)  
    {  
            cout << (*it) << "\n";  
    }  
}