我有一个串行设备,我想在通信过程中控制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/