Tools/Firmware/DynamixelGateway/Host/SerialDevice.cc
//set the terminal baud rate. Argument can be either an integer (ex. 115200) or speed_t (ex. B115200)
set the terminal baud rate. Argument can be either an integer (ex.
set the terminal baud rate . Argument can be either an integer ( ex .
int SerialDevice::SetBaudRate(const int speed)
{
  speed_t tempBaud;
  if (!_connected)
  {
#ifdef SERIAL_DEVICE_DEBUG
    std::cout << "SerialDevice::FlushInputBuffer: Error: not connected to the device" << std::endl;
#endif
    return -1;
  }
  if (_SpeedToBaud(speed,tempBaud))
  {
#ifdef SERIAL_DEVICE_DEBUG
    std::cout<<"SerialDevice::SetBaudRate: Error: bad baud rate" << std::endl;
#endif
    return -1;
  }
  if( tcgetattr( _fd, &_newterm ) < 0 )
  {
#ifdef SERIAL_DEVICE_DEBUG
    std::cout<<"SerialDevice::SetBaudRate: Error: Unable to get serial port attributes" << std::endl;
#endif
    return -1;
  }
  cfmakeraw( &_newterm );
  if (cfsetispeed( &_newterm, tempBaud ) < 0 )
  {
#ifdef SERIAL_DEVICE_DEBUG
    std::cout<<"SerialDevice::SetBaudRate: Error: Unable to set baud rate" <<std::endl;
#endif
    return -1;
  }
  if (cfsetospeed( &_newterm, tempBaud ) < 0 )
  {
#ifdef SERIAL_DEVICE_DEBUG
    std::cout<<"SerialDevice::SetBaudRate: Error: Unable to set baud rate" <<std::endl;
#endif
    return -1;
  }
  if( tcsetattr( _fd, TCSAFLUSH, &_newterm ) < 0 )
  {
#ifdef SERIAL_DEVICE_DEBUG
    std::cout<<"SerialDevice::SetBaudRate: Error: Unable to set serial port attributes" <<std::endl;
#endif
    return -1;
  }
  tcflush(_fd, TCIOFLUSH);
  _baud=tempBaud;
  return 0;
}
int SerialDevice :: SetBaudRate ( const int speed ) { speed_t tempBaud ; if ( ! _connected ) { #ifdef SERIAL_DEVICE_DEBUG std :: cout << " " << std :: endl ; #endif return -1 ; } if ( _SpeedToBaud ( speed , tempBaud ) ) { #ifdef SERIAL_DEVICE_DEBUG std :: cout << " " << std :: endl ; #endif return -1 ; } if ( tcgetattr ( _fd , & _newterm ) < 0 ) { #ifdef SERIAL_DEVICE_DEBUG std :: cout << " " << std :: endl ; #endif return -1 ; } cfmakeraw ( & _newterm ) ; if ( cfsetispeed ( & _newterm , tempBaud ) < 0 ) { #ifdef SERIAL_DEVICE_DEBUG std :: cout << " " << std :: endl ; #endif return -1 ; } if ( cfsetospeed ( & _newterm , tempBaud ) < 0 ) { #ifdef SERIAL_DEVICE_DEBUG std :: cout << " " << std :: endl ; #endif return -1 ; } if ( tcsetattr ( _fd , TCSAFLUSH , & _newterm ) < 0 ) { #ifdef SERIAL_DEVICE_DEBUG std :: cout << " " << std :: endl ; #endif return -1 ; } tcflush ( _fd , TCIOFLUSH ) ; _baud = tempBaud ; return 0 ; }
set the terminal baud rate.
set the terminal baud rate .
//check whether we are connected to the device //convert the integer speed value to speed_t if needed //get current port settings //cfmakeraw initializes the port to standard configuration. Use this! //set input baud rate //set output baud rate //set new attributes  //make sure queue is empty //save the baud rate value