2

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

lexXxel
  • 111
  • 2
  • 14
  • 1
    You can stop looking, Boost only wraps handshake control by the driver. Controlling them yourself requires platform-specific fallback code. – Hans Passant Jun 03 '15 at 11:47

2 Answers2

3

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
}
lollo
  • 2,289
  • 16
  • 20
lexXxel
  • 111
  • 2
  • 14
0

Doesn`t flow_control help? You may use serial_port::native_handle() and use native OS functions

Renat
  • 83
  • 5
  • and try to look on ioctl i maybe it what you need on linux – Renat Jun 03 '15 at 11:42
  • No I have to set them to 0 and 1 depending on the data I want to send to the device. Is it possible to use ioctl and the native_handle() from boost? – lexXxel Jun 03 '15 at 11:50
  • you may doing something like this int setDTR(unsigned short level) { int status; if (ioctl(fd, TIOCMGET, &status) == -1) { perror("setDTR(): TIOCMGET"); return 0; } if (level) status |= TIOCM_DTR; else status &= ~TIOCM_DTR; if (ioctl(fd, TIOCMSET, &status) == -1) { perror("setDTR: TIOCMSET"); return 0; } return 1; } where fd is asio::serial.nativeHandle() – Renat Jun 04 '15 at 03:03