c++ - boost::asio::serial_port 设置 RTS DTS

标签 c++ boost serial-port boost-asio flow-control

我有一个串行设备,我想在通信过程中控制RTS和DTR信号。基本上整个通信都是基于这两个信号。有没有办法在linux下使用boost::asio::serial_port实现来做到这一点。有没有办法让底层结构 boost 用于控制这两个信号?

我在 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 还定义了 #define BOOST_ASIO_OPTION_STORAGE termios

最佳答案

诚实地对待native_handle 是很容易的。 我就是这样解决的。

#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
}

关于c++ - boost::asio::serial_port 设置 RTS DTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618678/

相关文章:

c++ - 更改 linux 套接字文件权限

c++ - 如何使用 Visual Studio 2022(预览版 3)编译 Boost 库

c++ - QSerialPort - 等待来自发件人的全部数据

linux - 无法连接到 USB 串行端口(GSM 调制解调器)

c++ - CMake + UWP 资源和 source_group

c++ - qt写入结束文件

c++ - 链表 - 模板 - 指针

c++ - 负数的 lexical_cast 在不同的机器上表现不同

c++ - 将 C++ unordered_map 序列化到缓冲区 (char*)

python - 使用 Python,如何在 Linux 上创建虚拟串口?