apstring::apstring(const apstring & str) //description: copy constructor //postcondition: copy of str has been constructed { myLength = str.length(); myCapacity = myLength + 1; myCstring = new char[myCapacity]; strcpy(myCstring,str.myCstring); }
//Purpose: Send command to Khepera and return response. //Preconditions: Commands are in the form of a capital letter, // followed by any parameters needed, ending with // a carriage return and line feed. //Postconditions: Responses are in the form of a lowercase letter // corresponding to the capital letter command, then // any data it needs to transmit and a carriage return. apstring Serial::Talk(apstring send, int size) { if (VERBOSE) cout << "Serial::Entering Serial Talk" << endl; apstring receive = ""; char cSend[send.length()+1]; int i; for (i=0; i<send.length(); i++) cSend[i] = send[i]; cSend[i] = '\0'; if (VERBOSE) cout << "El Comando enviado al puerto es "<< cSend << endl; int tries = 0; do { //if write succeeds, read response if (write(SERIAL_ID,cSend,send.length()) == send.length()) { receive = Readline(size); if (VERBOSE){ cout << "Serial::receive="<< receive << endl; cout << "Serial::Talk done" << endl; } return receive; } tries ++; } while (tries < DEFAULT_RETRY); //if reach this point, talk failed if (VERBOSE) cout << "Serial::Talk Timeout" << endl; //perror("Serial::Readline()"); return receive; }//end Talk(apstring)
//Purpose: Open connection to Khepera on port passed // as a parameter e.g., "ttya". //Preconditions: Port name is defined //Postconditions: Connection open to given port. Returns true if successful, // false otherwise. bool Serial::Open(apstring portname) { if (VERBOSE) cout << "Serial::Entering Serial Open" << endl; int i; char buffer[64]; //if (VERBOSE) //cout << "Serial::buffer="; // convert apstring to a c string for (i=0; i<portname.length(); i++){ //if (VERBOSE) //cout << portname[i]; buffer[i] = portname[i]; } buffer[i] = '\0'; SERIAL_ID = open(buffer,O_RDWR|O_EXCL); //cerr << "Serial ID: " << SERIAL_ID << endl; if (SERIAL_ID == -1) { if (VERBOSE) cout << "Serial::Serial Port Failed to Open --> Serial ID" << endl; return false; } if (!Configure()) return false; if (!Drain()) return false; if (Talk("G,0,0\n") == "") { if (VERBOSE) cout << "Serial::Serial Port Failed to Open --> unable to talk" << endl; return false; } if (VERBOSE) cout << "Serial::Serial Port Opened" << endl; return true; }//end Open(apstring)