繁体   English   中英

串口通道上的boost :: asio :: async_write问题

[英]boost::asio::async_write issue over serial channel

我有客户端服务器应用程序,流程如下所述:

客户端在Windows端,不使用升级服务器在linux端,并使用升级客户端 - 服务器通过串行通道RS485进行通信。 和服务器使用boost::asio::async_write

client --> calls command with specific command_id --> server
client <-- sends acknowledgement                  <-- server
{server process the command, meanwhile the client is blocked for response}
client <-- sends response                         <-- server

有时,即使服务器发送响应,客户端收到确认但未收到响应的情况。 当客户端发送另一个命令时,客户端稍后会收到挂起的响应。

如果我使用boost::asio::write进行串行通信,则完全没有问题。

下面是async_write的代码片段

boost::asio::async_write(serial_port, boost::asio::buffer(&v_chunk[0], v_chunk.size()),
        boost::bind(&Serial_channel::async_write_callback, this, boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));

io_serv->run();
io_serv->reset();

你使用io_service的方式不起作用。 首先,在服务事件循环停止之前, run函数不会返回。 其次,如果您只想将其用作“轮询器”,那么您应该使用poll或者poll_one (或者run_one )。

但是,如果您这样做,它与执行非异步write调用相同,并且您放弃了异步函数的好处。

引用@Joachim评论我改变了我的流程,就像下面一样,它起作用了。

boost::asio::async_write(serial_port, boost::asio::buffer(&v_chunk[0], v_chunk.size()),
        boost::bind(&Serial_channel::async_write_callback, this, boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));

io_serv->run();
usleep(1000); // 1 millisecond delay
io_serv->reset();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM