Search code examples
c++boostserial-portboost-asioflow-control

boost::asio::serial_port set RTS DTS


I have a serial device and I want to control the RTS and DTR signal during the communication. Basicly the whole communication is based on this two signals. Is there a way to use the boost::asio::serial_port implementation under linux to do this. Is there any way to get the underlying structures boost uses to control both signals?

I found under boost/asio/impl/serial_port_basis.ipp

boost::system::error_code serial_port_base::flow_control::load(
const BOOST_ASIO_OPTION_STORAGE& storage, boost::system::error_code& ec)
{
#if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
...
#else
  if (storage.c_iflag & (IXOFF | IXON))
  {
    value_ = software;
  }
# if defined(_BSD_SOURCE)
  else if (storage.c_cflag & CRTSCTS)
  {
    value_ = hardware;
  }
# elif defined(__QNXNTO__)
  else if (storage.c_cflag & IHFLOW && storage.c_cflag & OHFLOW)
  {
    value_ = hardware;
  }
# endif
  else
  {
    value_ = none;
  }
#endif
  ec = boost::system::error_code();
  return ec;
}

boost also defines # define BOOST_ASIO_OPTION_STORAGE termios


Solution

  • Its pretty easy to be honest with the native_handle. That's how I solved it.

    #include <sys/ioctl.h>
    ....
    
    :::Constructor:::
    {
      //config for my device
      serialPort.open(port);
      serialPort.set_option(serial_port::baud_rate(9600));
      serialPort.set_option(serial_port::parity(serial_port::parity::even));
      serialPort.set_option(serial_port::character_size(serial_port::character_size(8)));
      serialPort.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
      serialPort.set_option(serial_port::flow_control(serial_port::flow_control::none));
      fd = serialPort.native_handle(); // fd is of typ int
    }
    
    void setRTS(bool enabled
        //int fd = serialPort.native_handle();
        int data = TIOCM_RTS;
        if (!enabled)
            ioctl(fd, TIOCMBIC, &data);        
        else
            ioctl(fd, TIOCMBIS, &data);
    }
    
    void setDTR(bool enabled)
    {
        //int fd = serialPort.native_handle();
        int data = TIOCM_DTR;
        if (!enabled)
            ioctl(fd, TIOCMBIC, &data);        // Clears the DTR pin
        else
            ioctl(fd, TIOCMBIS, &data);        // Sets the DTR pin
    }