asio_read_some 继续读取以获取所有数据
我使用boost asio async_read_some,我要读取的数据是STXdataETXlrc, 如果不是lrc我会使用async_read_until,我怎样才能继续阅读直到得到ETXlrc? 感谢所有的帮助。
/huvcbo
I use boost asio async_read_some, the data I want to read STXdataETXlrc,
if it were not for lrc I would use async_read_until, how can I continue reading until I get ETXlrc?
appreciate all the help.
/huvcbo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从非常稀疏的信息来看,我假设您正在串行通信的背景下进行交谈。
在这种情况下,最简单的开始方法可能是使用 Asio 的组合读取操作(例如
read
、read_until
等)。通过一些语法糖,您可以更富有表现力地编写:
例如:
然后您可以继续进行固定长度读取以获取校验和。更多涉及的
read_until
重载允许您编写MatchCondition
来代替,这样您就可以将其全部组合在一个调用中:https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/reference/read_until.html。这是一个使用
boost::regex
重载,显示消息以及校验和是否验证:Live On Compiler Explorer
使用
socat
进行本地测试:From the very sparse information, I'm assuming you are talking in the context of serial comms.
In that case, the simplest way to get started might be to use Asio's composed read operations (like
read
,read_until
etc).With a bit of syntactic sugar you can write more expressively:
E.g.:
You could then proceed to to a fixed-length read to get the checksum. More involved overloads of
read_until
allow you to writeMatchCondition
instead so you can combine it all in a single call: https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/reference/read_until.html.Here's an example that uses the
boost::regex
overload, that shows the message and whether the checksum verifies:Live On Compiler Explorer
Tested locally using
socat
: