• <small id='jjqa1'></small><noframes id='jjqa1'>

    <legend id='jjqa1'><style id='jjqa1'><dir id='jjqa1'><q id='jjqa1'></q></dir></style></legend>
      <bdo id='jjqa1'></bdo><ul id='jjqa1'></ul>

      <i id='jjqa1'><tr id='jjqa1'><dt id='jjqa1'><q id='jjqa1'><span id='jjqa1'><b id='jjqa1'><form id='jjqa1'><ins id='jjqa1'></ins><ul id='jjqa1'></ul><sub id='jjqa1'></sub></form><legend id='jjqa1'></legend><bdo id='jjqa1'><pre id='jjqa1'><center id='jjqa1'></center></pre></bdo></b><th id='jjqa1'></th></span></q></dt></tr></i><div id='jjqa1'><tfoot id='jjqa1'></tfoot><dl id='jjqa1'><fieldset id='jjqa1'></fieldset></dl></div>

      1. <tfoot id='jjqa1'></tfoot>

      2. 异步读取串行端口升压

        时间:2024-08-24

          <i id='85pRx'><tr id='85pRx'><dt id='85pRx'><q id='85pRx'><span id='85pRx'><b id='85pRx'><form id='85pRx'><ins id='85pRx'></ins><ul id='85pRx'></ul><sub id='85pRx'></sub></form><legend id='85pRx'></legend><bdo id='85pRx'><pre id='85pRx'><center id='85pRx'></center></pre></bdo></b><th id='85pRx'></th></span></q></dt></tr></i><div id='85pRx'><tfoot id='85pRx'></tfoot><dl id='85pRx'><fieldset id='85pRx'></fieldset></dl></div>
            <bdo id='85pRx'></bdo><ul id='85pRx'></ul>
            <legend id='85pRx'><style id='85pRx'><dir id='85pRx'><q id='85pRx'></q></dir></style></legend><tfoot id='85pRx'></tfoot>
            • <small id='85pRx'></small><noframes id='85pRx'>

                    <tbody id='85pRx'></tbody>

                1. 本文介绍了异步读取串行端口升压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从串行端口异步读取几个字节.目前我正在使用的代码(来源是 我该如何执行使用 asio 进行非阻塞读取?),只允许我读取 1 个字节然后退出.如何获取它以使代码继续读取,直到串行端口中没有更多传入数据?谢谢:)

                  I am trying to read several bytes asynchronously from a serial port. Currently the code I am using (source is How do I perform a nonblocking read using asio?), only allows me to read 1 byte and then it exits. How do I get it so that the code keeps reading until there is no more incoming data in the serial port? Thanks :)

                      void foo()
                  {
                      boost::asio::io_service io_svc;
                      boost::asio::serial_port ser_port(io_svc, "/dev/ttyS0");
                      boost::asio::deadline_timer timeout(io_svc);
                      unsigned char my_buffer[2];
                      bool data_available = false;
                  
                      ser_port.async_read_some(boost::asio::buffer(my_buffer),
                              boost::bind(&read_callback, boost::ref(data_available),
                                      boost::ref(timeout),boost::asio::placeholders::error(),
                                      boost::asio::placeholders::bytes_transferred()));
                      timeout.expires_from_now(boost::posix_time::seconds(30));
                      timeout.async_wait(boost::bind(&wait_callback, boost::ref(ser_port),boost::asio::placeholders::error()));
                  
                      io_svc.run();
                  
                      io_svc.
                  
                      if(!data_available)
                      {
                          ser_port.close();
                          cout << "ser_port was closed";
                      }
                  
                  }
                  
                  void read_callback(bool& data_available, boost::asio::deadline_timer& timeout, const boost::system::error_code& error, std::size_t bytes_transferred)
                  {
                      if (error || !bytes_transferred)
                      {
                          // No data was read!
                          data_available = false;
                          return;
                      }
                  
                      timeout.cancel();
                      data_available = true;
                  }
                  
                  void wait_callback(boost::asio::serial_port& ser_port, const boost::system::error_code& error)
                  {
                      if (error)
                      {
                          // Data was read and this timeout was cancelled
                  
                          return;
                      }
                  
                      ser_port.cancel();
                  }
                  

                  推荐答案

                  在您的 read_callback 中,您取消计时器并且不开始新的读取.
                  那么您希望 ASIO 做什么.您刚刚取消了所有处理程序,就像文档状态一样,run 方法将在没有处理程序时返回.

                  In your read_callback you cancel your timer and do not start a new read.
                  So what do you expect ASIO to do. You just canceled all handlers and like the documentation states, the run method will return when no handlers are left.

                  因此,如果您想获得比收到的一个字节更多的数据,您可以做两件事:
                  首先只需再次调用 asio 以从端口读取更多信息.

                  So if you want to have more data than just the one byte you receive you can do two things:
                  First just issue another call to asio to read more from the port.

                  void read_callback(bool& data_available, boost::asio::deadline_timer& timeout, const  boost::system::error_code& error, std::size_t bytes_transferred)
                  {
                      if (error || !bytes_transferred)
                      {
                          // No data was read!
                          data_available = false;
                          return;
                      }
                      // do something with the data you just read
                      ser_port.async_read_some(boost::asio::buffer(my_buffer),
                          boost::bind(&read_callback, boost::ref(data_available),
                                  boost::ref(timeout),boost::asio::placeholders::error(),
                                  boost::asio::placeholders::bytes_transferred()));
                      //restart your timer
                      data_available = true;
                  }
                  

                  或者您可以添加使用 transfer_at_least 以获取至少您需要的数据量.

                  or you can add use transfer_at_least to get at least the amount of data you need.

                  这篇关于异步读取串行端口升压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 Java 获取我的电脑中可用串行端口的列表? 下一篇:如何使用python执行java程序同时考虑输入和输出

                  相关文章

                2. <small id='NQy8y'></small><noframes id='NQy8y'>

                  <tfoot id='NQy8y'></tfoot>
                3. <i id='NQy8y'><tr id='NQy8y'><dt id='NQy8y'><q id='NQy8y'><span id='NQy8y'><b id='NQy8y'><form id='NQy8y'><ins id='NQy8y'></ins><ul id='NQy8y'></ul><sub id='NQy8y'></sub></form><legend id='NQy8y'></legend><bdo id='NQy8y'><pre id='NQy8y'><center id='NQy8y'></center></pre></bdo></b><th id='NQy8y'></th></span></q></dt></tr></i><div id='NQy8y'><tfoot id='NQy8y'></tfoot><dl id='NQy8y'><fieldset id='NQy8y'></fieldset></dl></div>

                  1. <legend id='NQy8y'><style id='NQy8y'><dir id='NQy8y'><q id='NQy8y'></q></dir></style></legend>

                        <bdo id='NQy8y'></bdo><ul id='NQy8y'></ul>