gpt4 book ai didi

c++ - 串行 channel 上的 boost::asio::async_write 问题

转载 作者:行者123 更新时间:2023-11-28 03:16:48 26 4
gpt4 key购买 nike

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

客户端在windows端,不使用boost服务器在 linux 端并使用 boost客户端-服务器通过串行 channel 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 调用相同,并且您会失去异步函数的优势。

关于c++ - 串行 channel 上的 boost::asio::async_write 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16646350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com