/**
 * @brief initializer for port, provided that device name is defined
 * @param serial_name -- name of the serial device to be opened
 * @param baud_rate -- bits-per-sec for the I/O
 * @return -- 1 for success 0 for failure
 */
bool UAS_serial::beginPort(const char* serial_name, uint32_t baudrate){
    int baudr;
    _baudrate = baudrate;
    _device_name = serial_name;
    
    mapBaudRate(_baudrate, &baudr); // map baudrate to termios B-- style

    _serial_id = open(_device_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if(_serial_id == -1){
      perror("unable to open comport ");
      _error = -1;
      _port_opened = 0;
      return _port_opened;
    }

    memset(&_port_settings, 0, sizeof(_port_settings));  // setting memory for port
    _port_settings.c_cflag = baudr | CS8 | CLOCAL | CREAD;
    _port_settings.c_iflag = 0;
    _port_settings.c_oflag = 0;
    _port_settings.c_lflag = 0;
    _port_settings.c_cc[VMIN] = 0;      /* block untill n bytes are received */
    _port_settings.c_cc[VTIME] = 0;     /* block untill a timer expires (n * 100 mSec.) */
    _error = tcsetattr(_serial_id, TCSANOW, &_port_settings);
    
    if(_error==-1){
      close(_serial_id);
      perror("unable to adjust portsettings ");
      _port_opened = 0;
      return _port_opened;
    }
    
    flushIO();
    _port_opened = 1;
    return _port_opened;
}
Exemple #2
0
VALUE closeIO(VALUE self)
{

    PortDescriptor *port = NULL;

    Data_Get_Struct(self, PortDescriptor, port);

    flushIO(self);
    port->status = CloseHandle(port->fd);
    port->fd     = INVALID_HANDLE_VALUE;

    rb_iv_set(self, "@open", INT2FIX(port->status));

    return INT2FIX(port->status);

}