boost::asio::async_write issue over serial channel
boost::asio::async_write issue over serial channel
我有一个客户端服务器应用程序,流程如下所示:
客户端在Windows端,不使用boost服务器在Linux端,使用boost客户端-服务器通过串行通道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
串行通信,根本没有问题。
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();
相关文章:
- 为什么 Serial.println(<char[]>);返回随机字符?
- clang C++17 std::vector over align type 使用 -mavx 编译时元素 SIGSE
- Arduino Uno - serial.read to a struct of bits
- Arduino - 如何从 serial.read() 馈送结构?
- Arduino在通过Serial读取utf8时表现奇怪
- 如何在 Arduino 延迟期间在 void loop() 中使用 serial.available()?
- Serial.print in arduino 不写变量
- Array.at(index) over Array[index]
- C++ Iterating Over va_list
- 在 [over.ass]/2 的例子中,为什么 *bptr = dobj2;调用 D::operator=(const B&)?
- [over.unary]/2 中注释中提到的隐藏的编译示例
- 我们在哪里可以使用std::barrier over std::latch
- Serial.Read() code
- dynamic_cast over共享库在使用clang编译的Android上的Qt中失败
- Windows 和 Linux 中的 USB Serial 之间的区别
- 在Arduino Radiohead中使用serial.read()与const char *
- 使用 Serial.Read 输入在 Arduino 上设置 LCD 光标
- Arduino Serial在哪里创建
- 为什么选择 (;;){} over while(1).
- boost::asio::async_write issue over serial channel