std::vector<SerialPortInfo> OFServiceProvider::EnumAvaliablePorts(bool rescan) { std::vector<SerialPortInfo> retList; deviceType = "serial"; devices.clear(); std::vector <std::string> prefixMatch; //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- enumerateWin32Ports(rescan); //ofLogNotice() << "ofSerial: listing devices (" << nPorts << " total)"; for (int i = 0; i < portNamesShort.size(); i++){ //NOTE: we give the short port name for both as that is what the user should pass and the short name is more friendly ofSerialDeviceInfo ifo; ifo.devicePath=portNamesShort[i].c_str(); ifo.deviceID=i; devices.push_back(ifo); } //--------------------------------------------- #endif //--------------------------------------------- //here we sort the device to have the aruino ones first. partition(devices.begin(), devices.end(), isDeviceArduino); //we are reordering the device ids. too! for(int k = 0; k < (int)devices.size(); k++){ devices[k].deviceID = k; SerialPortInfo ifo; ifo.ID=devices[k].getDeviceID(); ifo.Name=devices[k].getDeviceName().c_str(); retList.push_back(ifo); } bHaveEnumeratedDevices = true; return retList; }
//---------------------------------------------------------------- void ofSerial::buildDeviceList(){ deviceType = "serial"; devices.clear(); std::vector <std::string> prefixMatch; enumerateWin32Ports(); std::cout << "found " << nPorts << " devices" << "\r\n"; for (int i = 0; i < nPorts; i++){ //NOTE: we give the short port name for both as that is what the user should pass and the short name is more friendly devices.push_back(ofSerialDeviceInfo(std::string(portNamesShort[i]), std::string(portNamesShort[i]), i)); } //here we sort the device to have the aruino ones first. partition(devices.begin(), devices.end(), isDeviceArduino); //we are reordering the device ids. too! for(int k = 0; k < (int)devices.size(); k++){ devices[k].deviceID = k; } bHaveEnumeratedDevices = true; }
//---------------------------------------------------------------- void ofSerial::buildDeviceList(){ deviceType = "serial"; devices.clear(); std::vector <std::string> prefixMatch; #ifdef TARGET_OSX prefixMatch.push_back("cu."); prefixMatch.push_back("tty."); #endif #ifdef TARGET_LINUX prefixMatch.push_back("ttyS"); prefixMatch.push_back("ttyUSB"); prefixMatch.push_back("rfc"); #endif #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) DIR *dir; struct dirent *entry; dir = opendir("/dev"); string deviceName = ""; int deviceCount = 0; if (dir == NULL){ ofLog(OF_LOG_ERROR,"ofSerial: error listing devices in /dev"); } else { //for each device while((entry = readdir(dir)) != NULL){ deviceName = (char *)entry->d_name; //we go through the prefixes for(int k = 0; k < (int)prefixMatch.size(); k++){ //if the device name is longer than the prefix if( deviceName.size() > prefixMatch[k].size() ){ //do they match ? if( deviceName.substr(0, prefixMatch[k].size()) == prefixMatch[k].c_str() ){ devices.push_back(ofSerialDeviceInfo("/dev/"+deviceName, deviceName, deviceCount)); deviceCount++; break; } } } } closedir(dir); } #endif //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- enumerateWin32Ports(); //ofLogNotice() << "ofSerial: listing devices (" << nPorts << " total)"; for (int i = 0; i < nPorts; i++){ //NOTE: we give the short port name for both as that is what the user should pass and the short name is more friendly ofSerialDeviceInfo ifo; ifo.devicePath=portNamesShort[i]; ifo.deviceID=i; devices.push_back(ifo); } //--------------------------------------------- #endif //--------------------------------------------- //here we sort the device to have the aruino ones first. partition(devices.begin(), devices.end(), isDeviceArduino); //we are reordering the device ids. too! for(int k = 0; k < (int)devices.size(); k++){ devices[k].deviceID = k; } bHaveEnumeratedDevices = true; }
//---------------------------------------------------------------- void ofSerial::buildDeviceList(){ deviceType = "serial"; devices.clear(); vector <string> prefixMatch; #ifdef TARGET_OSX prefixMatch.push_back("cu."); prefixMatch.push_back("tty."); #endif #ifdef TARGET_LINUX #ifdef TARGET_RASPBERRY_PI prefixMatch.push_back("ttyACM"); #endif prefixMatch.push_back("ttyS"); prefixMatch.push_back("ttyUSB"); prefixMatch.push_back("rfc"); #endif #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) DIR *dir; dir = opendir("/dev"); string deviceName = ""; if(dir == nullptr){ ofLogError("ofSerial") << "buildDeviceList(): error listing devices in /dev"; } else { int deviceCount = 0; //for each device struct dirent *entry; while((entry = readdir(dir)) != nullptr){ deviceName = (char *)entry->d_name; //we go through the prefixes for(int k = 0; k < (int)prefixMatch.size(); k++){ //if the device name is longer than the prefix if(deviceName.size() > prefixMatch[k].size()){ //do they match ? if(deviceName.substr(0, prefixMatch[k].size()) == prefixMatch[k].c_str()){ devices.push_back(ofSerialDeviceInfo("/dev/"+deviceName, deviceName, deviceCount)); deviceCount++; break; } } } } closedir(dir); } #endif #ifdef TARGET_WIN32 enumerateWin32Ports(); ofLogNotice("ofSerial") << "found " << nPorts << " devices"; for(int i = 0; i < nPorts; i++){ //NOTE: we give the short port name for both as that is what the user should pass and the short name is more friendly devices.push_back(ofSerialDeviceInfo(string(portNamesShort[i]), string(portNamesShort[i]), i)); } #endif //here we sort the device to have the aruino ones first. partition(devices.begin(), devices.end(), isDeviceArduino); //we are reordering the device ids. too! for(int k = 0; k < (int)devices.size(); k++){ devices[k].deviceID = k; } bHaveEnumeratedDevices = true; }
//---------------------------------------------------------------- bool ofSerial::setup(int deviceNumber, int baud){ int deviceCount = 0; string str = ""; string device = ""; bool deviceFound = false; //--------------------------------------------- #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) //--------------------------------------------- //---------------------------------------------------- //We will find serial devices by listing the directory DIR *dir; struct dirent *entry; dir = opendir("/dev"); if (dir == NULL){ ofLog(OF_LOG_ERROR,"ofSerial: error listing devices in /dev"); } while ((entry = readdir(dir)) != NULL){ str = (char *)entry->d_name; if( str.substr(0,3) == "cu." || str.substr(0,3) == "tty" || str.substr(0,3) == "rfc" ){ if(deviceCount == deviceNumber){ device = "/dev/"+str; deviceFound = true; ofLog(OF_LOG_NOTICE,"ofSerial device %i - /dev/%s <--selected", deviceCount, str.c_str()); }else ofLog(OF_LOG_NOTICE,"ofSerial device %i - /dev/%s", deviceCount, str.c_str()); deviceCount++; } } if(deviceFound){ return setup(device, baud); }else{ ofLog(OF_LOG_ERROR,"ofSerial: could not find device %i - only %i devices found", deviceNumber, deviceCount); return false; } //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- enumerateWin32Ports(); if (deviceNumber < nPorts){ device = portNamesShort[deviceNumber]; deviceFound = true; } if(deviceFound){ return setup(device, baud); }else{ ofLog(OF_LOG_ERROR,"ofSerial: could not find device %i - only %i devices found", deviceNumber, nPorts); return false; } //--------------------------------------------- #endif //--------------------------------------------- }
//---------------------------------------------------------------- void ofSerial::enumerateDevices(){ //--------------------------------------------- #if defined( TARGET_OSX ) //--------------------------------------------- //---------------------------------------------------- //We will find serial devices by listing the directory DIR *dir; struct dirent *entry; dir = opendir("/dev"); string str = ""; string device = ""; int deviceCount = 0; if (dir == NULL){ ofLog(OF_LOG_ERROR,"ofSerial: error listing devices in /dev"); } else { printf("ofSerial: listing devices\n"); while ((entry = readdir(dir)) != NULL){ str = (char *)entry->d_name; if( str.substr(0,3) == "cu." ){ printf("device %i - %s\n", deviceCount, str.c_str()); deviceCount++; } } } //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #if defined( TARGET_LINUX ) //--------------------------------------------- //---------------------------------------------------- //We will find serial devices by listing the directory DIR *dir; struct dirent *entry; dir = opendir("/dev"); string str = ""; string device = ""; int deviceCount = 0; if (dir == NULL){ ofLog(OF_LOG_ERROR,"ofSerial: error listing devices in /dev"); } else { printf("ofSerial: listing devices\n"); while ((entry = readdir(dir)) != NULL){ str = (char *)entry->d_name; if( str.substr(0,3) == "tty" || str.substr(0,3) == "rfc" ){ printf("device %i - %s\n", deviceCount, str.c_str()); deviceCount++; } } } //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- enumerateWin32Ports(); printf("ofSerial: listing devices (%i total)\n", nPorts); for (int i = 0; i < nPorts; i++){ printf("device %i -- %s", i, portNamesFriendly[i]); } //--------------------------------------------- #endif //--------------------------------------------- }
//---------------------------------------------------------------- void ofSerial::buildDeviceList(){ deviceType = "serial"; devices.clear(); vector <string> prefixMatch; #ifdef TARGET_OSX prefixMatch.push_back("cu."); prefixMatch.push_back("tty."); #endif #ifdef TARGET_LINUX prefixMatch.push_back("ttyACM"); prefixMatch.push_back("ttyS"); prefixMatch.push_back("ttyUSB"); prefixMatch.push_back("rfc"); #endif #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) ofDirectory dir("/dev"); int deviceCount = 0; for(auto & entry: dir){ std::string deviceName = entry.path(); //we go through the prefixes for(auto & prefix: prefixMatch){ //if the device name is longer than the prefix if(deviceName.size() > prefix.size()){ //do they match ? if(deviceName.substr(0, prefix.size()) == prefix.c_str()){ devices.push_back(ofSerialDeviceInfo("/dev/"+deviceName, deviceName, deviceCount)); deviceCount++; break; } } } } #endif #ifdef TARGET_WIN32 enumerateWin32Ports(); ofLogNotice("ofSerial") << "found " << nPorts << " devices"; for(int i = 0; i < nPorts; i++){ //NOTE: we give the short port name for both as that is what the user should pass and the short name is more friendly devices.push_back(ofSerialDeviceInfo(string(portNamesShort[i]), string(portNamesShort[i]), i)); } #endif #if defined( TARGET_OSX ) //here we sort the device to have the aruino ones first. partition(devices.begin(), devices.end(), isDeviceArduino); //we are reordering the device ids. too! int k = 0; for(auto & device: devices){ device.deviceID = k++; } #endif bHaveEnumeratedDevices = true; }
//---------------------------------------------------------------- bool ofSerial::setup(int deviceNumber, int baud){ int deviceCount = 0; string str = ""; string device = ""; bool deviceFound = false; //--------------------------------------------- #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) //--------------------------------------------- //---------------------------------------------------- //We will find serial devices by listing the directory DIR *dir; struct dirent *entry; dir = opendir("/dev"); if (dir == NULL){ ofLog(OF_LOG_ERROR,"ofSerial: error listing devices in /dev"); } while ((entry = readdir(dir)) != NULL){ str = (char *)entry->d_name; #ifdef TARGET_OSX //if( str.substr(0,3) == "cu." || str.substr(0,4) == "tty." ){ if( str.find("cu.usbserial") == 0 || str.find("tty.usbserial") == 0 ){ #else if( str.substr(0,4) == "ttyS" || str.substr(0,6) == "ttyUSB" || str.substr(0,3) == "rfc" ){ #endif if(deviceCount == deviceNumber){ device = "/dev/"+str; deviceFound = true; ofLog(OF_LOG_NOTICE,"ofSerial device %i - /dev/%s <--selected", deviceCount, str.c_str()); }else ofLog(OF_LOG_NOTICE,"ofSerial device %i - /dev/%s", deviceCount, str.c_str()); deviceCount++; } } if(deviceFound){ return setup(device, baud); }else{ ofLog(OF_LOG_ERROR,"ofSerial: could not find device %i - only %i devices found", deviceNumber, deviceCount); return false; } //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- enumerateWin32Ports(); if (deviceNumber < nPorts){ device = portNamesShort[deviceNumber]; deviceFound = true; } if(deviceFound){ return setup(device, baud); }else{ ofLog(OF_LOG_ERROR,"ofSerial: could not find device %i - only %i devices found", deviceNumber, nPorts); return false; } //--------------------------------------------- #endif //--------------------------------------------- } //---------------------------------------------------------------- bool ofSerial::setup(string portName, int baud){ bInited = false; //--------------------------------------------- #if defined( TARGET_OSX ) || defined( TARGET_LINUX ) //--------------------------------------------- ofLog(OF_LOG_NOTICE,"ofSerialInit: opening port %s @ %d bps", portName.c_str(), baud); fd = open(portName.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); if(fd == -1){ ofLog(OF_LOG_ERROR,"ofSerial: unable to open port"); return false; } struct termios options; tcgetattr(fd,&oldoptions); options = oldoptions; switch(baud){ case 300: cfsetispeed(&options,B300); cfsetospeed(&options,B300); break; case 1200: cfsetispeed(&options,B1200); cfsetospeed(&options,B1200); break; case 2400: cfsetispeed(&options,B2400); cfsetospeed(&options,B2400); break; case 4800: cfsetispeed(&options,B4800); cfsetospeed(&options,B4800); break; case 9600: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); break; case 14400: cfsetispeed(&options,B14400); cfsetospeed(&options,B14400); break; case 19200: cfsetispeed(&options,B19200); cfsetospeed(&options,B19200); break; case 28800: cfsetispeed(&options,B28800); cfsetospeed(&options,B28800); break; case 38400: cfsetispeed(&options,B38400); cfsetospeed(&options,B38400); break; case 57600: cfsetispeed(&options,B57600); cfsetospeed(&options,B57600); break; case 115200: cfsetispeed(&options,B115200); cfsetospeed(&options,B115200); break; default: cfsetispeed(&options,B9600); cfsetospeed(&options,B9600); ofLog(OF_LOG_ERROR,"ofSerialInit: cannot set %i baud setting baud to 9600\n", baud); break; } options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; tcsetattr(fd,TCSANOW,&options); bInited = true; ofLog(OF_LOG_NOTICE,"sucess in opening serial connection"); return true; //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- // open the serial port: // "COM4", etc... hComm=CreateFileA(portName.c_str(),GENERIC_READ|GENERIC_WRITE,0,0, OPEN_EXISTING,0,0); if(hComm==INVALID_HANDLE_VALUE){ ofLog(OF_LOG_ERROR,"ofSerial: unable to open port"); return false; } // now try the settings: COMMCONFIG cfg; DWORD cfgSize; char buf[80]; cfgSize=sizeof(cfg); GetCommConfig(hComm,&cfg,&cfgSize); int bps = baud; sprintf(buf,"baud=%d parity=N data=8 stop=1",bps); #if (_MSC_VER) // microsoft visual studio // msvc doesn't like BuildCommDCB, //so we need to use this version: BuildCommDCBA if(!BuildCommDCBA(buf,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: unable to build comm dcb; (%s)",buf); } #else if(!BuildCommDCB(buf,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: Can't build comm dcb; %s",buf); } #endif // Set baudrate and bits etc. // Note that BuildCommDCB() clears XON/XOFF and hardware control by default if(!SetCommState(hComm,&cfg.dcb)){ ofLog(OF_LOG_ERROR,"ofSerial: Can't set comm state"); } //ofLog(OF_LOG_NOTICE,buf,"bps=%d, xio=%d/%d",cfg.dcb.BaudRate,cfg.dcb.fOutX,cfg.dcb.fInX); // Set communication timeouts (NT) COMMTIMEOUTS tOut; GetCommTimeouts(hComm,&oldTimeout); tOut = oldTimeout; // Make timeout so that: // - return immediately with buffered characters tOut.ReadIntervalTimeout=MAXDWORD; tOut.ReadTotalTimeoutMultiplier=0; tOut.ReadTotalTimeoutConstant=0; SetCommTimeouts(hComm,&tOut); bInited = true; return true; //--------------------------------------------- #endif //--------------------------------------------- }
value enumerateDevices() { char empty = 0; char* str = ∅ //--------------------------------------------- #if defined( TARGET_OSX ) //--------------------------------------------- //---------------------------------------------------- //We will find serial devices by listing the directory DIR *dir; struct dirent *entry; dir = opendir("/dev"); if (dir == NULL){ return alloc_null(); } else { while ((entry = readdir(dir)) != NULL){ char* name = entry->d_name; if (strncmp(name,"cu.",3) == 0 || strncmp(name,"tty.",4) == 0) { int len0 = strlen(str); int len1 = strlen(name); char* nstr = (char*)malloc(len0 + len1 + 7); strcpy(nstr,str); strcat(nstr,"/dev/"); strcat(nstr,name); strcat(nstr,"\n"); if (str != &empty) free(str); str = nstr; } } } //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #if defined( TARGET_LINUX ) //--------------------------------------------- //---------------------------------------------------- //We will find serial devices by listing the directory DIR *dir; struct dirent *entry; dir = opendir("/dev"); if (dir == NULL){ return alloc_null(); } else { while ((entry = readdir(dir)) != NULL){ char* name = entry->d_name; if (strncmp(name,"ttyS",4) == 0 || strncmp(name,"ttyUSB",6) == 0 || strncmp(name,"ttyACM",6) == 0 || strncmp(name,"rfc",3) == 0) { int len0 = strlen(str); int len1 = strlen(name); char* nstr = (char*)malloc(len0 + len1 + 7); strcpy(nstr,str); strcat(nstr,"/dev/"); strcat(nstr,name); strcat(nstr,"\n"); if (str != &empty ) free(str); str = nstr; } } } //--------------------------------------------- #endif //--------------------------------------------- //--------------------------------------------- #ifdef TARGET_WIN32 //--------------------------------------------- enumerateWin32Ports(); for (int i = 0; i < nPorts; i++){ char* name = portNamesShort[i]; int len0 = strlen(str); int len1 = strlen(name); char* nstr = (char*)malloc(len0 + len1 + 7); strcpy(nstr,str); strcat(nstr,name); strcat(nstr,"\n"); if (str != &empty) free(str); str = nstr; } //--------------------------------------------- #endif //--------------------------------------------- if (strlen(str) > 0) str[strlen(str)-1] = '\0'; value ret = alloc_string(str); if (str != &empty) free(str); return ret; }