【发布时间】:2017-03-20 16:39:37
【问题描述】:
我遇到了其他人似乎遇到过的问题,但这里似乎没有被问到。我有一个非常简单的应用程序,现在只是使用 boost asio 打开和关闭一个串行端口(只是在构建更大的应用程序之前尝试做一些简单的调试。调用驻留在基于 qt 的 gui . 现在的串口通讯是在窗口构造函数中设置好的:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(getData()));
timer->start(1000);
std::string comm = "/dev/ttyUSB0";
boost::asio::io_service io;
port = std::shared_ptr<boost::asio::serial_port>(new boost::asio::serial_port(io, comm));
std::cout << "Port has been successfully opened..." << std::endl;
boost::asio::serial_port_base::baud_rate baud(19200);
port->set_option(baud);
std::cout << "Baud rate set to 19200..." << std::endl;
getData();
}
getData 将包含对串行端口的定期调用,但现在它只是请求在 gui 上用当前时间填充时间戳。
端口在析构函数中关闭
MainWindow::~MainWindow()
{
if (port->is_open()) port->close();
delete ui;
}
当gui关闭时,析构函数被调用,但系统只是在端口关闭时挂起。看来它卡在了 boost 的 posix_mutex.hpp lock 方法中。
我看过另一篇关于此的帖子 (https://cpc110.blogspot.com/2017/03/boost-asio-can-not-close-serial-port.html),但似乎没有答案。有人对此有什么想法吗?
我在 Ubuntu 16.04 LTS 上使用 boost 1.58 和 Qt 5.8。
【问题讨论】:
标签: c++ qt boost serial-port