int PosixSerial::readSerial(char *buf, size_t &size) { if(!initialized_) { if( initializePort() == CROI_ERROR ) { return CROI_ERROR; } } //non blocking read fcntl(fd_, F_SETFL, FNDELAY); //int origflags = fcntl(fd_, F_GETFL, 0); //fcntl(fd_, F_SETFL, origflags & ~O_NONBLOCK); size = read(fd_, buf, MAX_READ); fcntl(fd_, F_SETFL, 0); // fcntl(fd_, F_SETFL, origflags | O_NONBLOCK); if(size == -1){ return CROI_ERROR; } return CROI_SUCCESS; }
int PosixSerial::readSerial(std::string & buf) { if(!initialized_) { if( initializePort() == CROI_ERROR ) { return CROI_ERROR; } } //non blocking read fcntl(fd_, F_SETFL, FNDELAY); //int origflags = fcntl(fd_, F_GETFL, 0); //fcntl(fd_, F_SETFL, origflags & ~O_NONBLOCK); int size = read(fd_, readBuf_, MAX_READ); fcntl(fd_, F_SETFL, 0); // fcntl(fd_, F_SETFL, origflags | O_NONBLOCK); if(size == -1){ return CROI_ERROR; } //debug for(int i = 0; i < size; ++i) { printf("tuosta: %i\n",readBuf_[i]); } std::cout << std::endl; std::cout << size << std::endl; buf = std::string(readBuf_, readBuf_ + size); return CROI_SUCCESS; }
void SerialInterface::pair() { #ifndef _WIN32 // look for a matching gyro setup DIR *devDir; struct dirent *entry; int matchStatus; regex_t regex; // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb*, // and (possibly just Ubuntu) Linux, where it shows up as /dev/ttyACM* if((devDir = opendir("/dev"))) { while((entry = readdir(devDir))) { #ifdef __APPLE__ regcomp(®ex, "tty\\.usb", REG_EXTENDED|REG_NOSUB); #else regcomp(®ex, "ttyACM", REG_EXTENDED|REG_NOSUB); #endif matchStatus = regexec(®ex, entry->d_name, (size_t) 0, NULL, 0); if (matchStatus == 0) { char *serialPortname = new char[100]; sprintf(serialPortname, "/dev/%s", entry->d_name); initializePort(serialPortname); delete [] serialPortname; } regfree(®ex); } closedir(devDir); } #endif }
int PosixSerial::writeSerial(const std::string buf) { if (!initialized_) { if( initializePort() == CROI_ERROR ) { return CROI_ERROR; } } int n = write(fd_, buf.c_str(), buf.size()); std::cout << "sent bytes: " << n << std::endl; if(n != static_cast<int>(buf.size())) { return CROI_ERROR; } return CROI_SUCCESS; }
int PosixSerial::writeSerial(char *buf, size_t size) { if (!initialized_) { if( initializePort() == CROI_ERROR ) { return CROI_ERROR; } } int n = write(fd_, buf, size); if(n != size) { return CROI_ERROR; } std::cout << buf << std::endl; return CROI_SUCCESS; }
bool UdpWakeOnLanWaker::initialize () { if ( !initializePacket () ) { dprintf ( D_ALWAYS, "UdpWakeOnLanWaker::initialize: " "Failed to initialize magic WOL packet\n" ); return false; } if ( !initializePort () ) { dprintf ( D_ALWAYS, "UdpWakeOnLanWaker::initialize: " "Failed to initialize port number\n" ); return false; } if ( !initializeBroadcastAddress () ) { dprintf ( D_ALWAYS, "UdpWakeOnLanWaker::initialize: " "Failed to initialize broadcast address\n" ); return false; } /* if we get here then we are fine */ return true; }