如何使用Boost.Asio c++?

发布于 2024-10-30 03:42:21 字数 1434 浏览 3 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2024-11-06 03:42:21

如何使用它取决于您想要做什么,;-)。

该文档位于:

http://www.boost。 org/doc/libs/1_46_1/doc/html/boost_asio.html

您会发现很多适合您需求的示例。

对于构建,您应该注意库依赖关系取决于您是在 Windows 还是 Linux 上运行。请参阅此处

http://www.boost.org/ doc/libs/1_46_1/doc/html/boost_asio/using.html

特别是:

使用 MSVC 或 Borland C++,您可能需要
添加 -DBOOST_DATE_TIME_NO_LIB 和
-DBOOST_REGEX_NO_LIB 到您的项目设置以禁用自动链接
Boost.Date_Time 和 Boost.Regex
分别是库。或者,
你可以选择构建这些
库并链接到它们

如果您不希望依赖于其他 boost 库,那么您可以从这里使用非 boost(我认为其他方面相同的 asio)库: http://think-async.com/

有关其他文档的来源,请参阅以下问题:Boost:asio 的最佳文档?

例如,要打开串行端口,您可以编写如下内容:

/** Manage serial connections.*/
class serial_manager
{

  boost::asio::io_service m_io_service;
  std::string m_name;
  const unsigned int m_baud_rate;
  const enum flow_control::type m_flow_control;
  const enum parity::type m_parity;
  const enum stop_bits::type m_stop_bits;
  const unsigned int m_char_size;
  boost::asio::serial_port  m_SerialPort;
  boost::system::error_code m_error;
public:

  /** A constructor.
   *  @param name The dvice name, for example "COM1" (windows, or "/dev/ttyS0" (linux).
   *  @param baud_rate The baud rate. 
   *  @param flow_control The flow control. Acceptable values are flow_control::none, flow_control::software, flow_control::hardware.
   *  @param parity The parity of the connection. Acceptable values are parity::none, parity::even, parity::odd.
   *  @param stop_bits The number of stop bits. Acceptable values are stop_bits::one, stop_bits::one_point_five, stop::bits::two
   *  @param char_size The number of characters in connection.
   */
  serial_manager(const std::string& name, 
         const unsigned int& baud_rate  = 19200,
         const enum flow_control::type&   flow_control  = flow_control::none,
         const enum parity::type&         parity        = parity::none,
         const enum stop_bits::type&      stop_bits     = stop_bits::one,
         const unsigned int& char_size  = 8
         ) 
;
  void open();
};

void
serial_manager::open() {
  if (!m_SerialPort.is_open()) 
    {
      m_SerialPort.open(m_name, m_error);

     if (m_error == boost::system::errc::no_such_file_or_directory ) 
     {  //for example you tried to open "COM1" on a linux machine.
        //... handle the error somehow
     }

      m_SerialPort.set_option(boost::asio::serial_port::baud_rate(m_baud_rate));
      m_SerialPort.set_option(boost::asio::serial_port::flow_control(m_flow_control));
      m_SerialPort.set_option(boost::asio::serial_port::parity(m_parity));
      m_SerialPort.set_option(boost::asio::serial_port::stop_bits(m_stop_bits));
      m_SerialPort.set_option(boost::asio::serial_port::character_size(m_char_size));


    }
 }

How you use it depends on what you want to do, ;-).

The documentation is found here:

http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio.html

You will find lots of examples that should suite your needs.

For building, you should note that the library dependancies depend upon whether you are running on windows or linux. See here

http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/using.html

In particular:

With MSVC or Borland C++ you may want
to add -DBOOST_DATE_TIME_NO_LIB and
-DBOOST_REGEX_NO_LIB to your project settings to disable autolinking of the
Boost.Date_Time and Boost.Regex
libraries respectively. Alternatively,
you may choose to build these
libraries and link to them

If you don't want the dependancies to the other boost libraries then you can use the non-boost (i think otherwise identical asio) library from here: http://think-async.com/

For sources of other documentation see this question on SO: Best documentation for Boost:asio?

As an example, to open a serial port you might write something like this:

/** Manage serial connections.*/
class serial_manager
{

  boost::asio::io_service m_io_service;
  std::string m_name;
  const unsigned int m_baud_rate;
  const enum flow_control::type m_flow_control;
  const enum parity::type m_parity;
  const enum stop_bits::type m_stop_bits;
  const unsigned int m_char_size;
  boost::asio::serial_port  m_SerialPort;
  boost::system::error_code m_error;
public:

  /** A constructor.
   *  @param name The dvice name, for example "COM1" (windows, or "/dev/ttyS0" (linux).
   *  @param baud_rate The baud rate. 
   *  @param flow_control The flow control. Acceptable values are flow_control::none, flow_control::software, flow_control::hardware.
   *  @param parity The parity of the connection. Acceptable values are parity::none, parity::even, parity::odd.
   *  @param stop_bits The number of stop bits. Acceptable values are stop_bits::one, stop_bits::one_point_five, stop::bits::two
   *  @param char_size The number of characters in connection.
   */
  serial_manager(const std::string& name, 
         const unsigned int& baud_rate  = 19200,
         const enum flow_control::type&   flow_control  = flow_control::none,
         const enum parity::type&         parity        = parity::none,
         const enum stop_bits::type&      stop_bits     = stop_bits::one,
         const unsigned int& char_size  = 8
         ) 
;
  void open();
};

void
serial_manager::open() {
  if (!m_SerialPort.is_open()) 
    {
      m_SerialPort.open(m_name, m_error);

     if (m_error == boost::system::errc::no_such_file_or_directory ) 
     {  //for example you tried to open "COM1" on a linux machine.
        //... handle the error somehow
     }

      m_SerialPort.set_option(boost::asio::serial_port::baud_rate(m_baud_rate));
      m_SerialPort.set_option(boost::asio::serial_port::flow_control(m_flow_control));
      m_SerialPort.set_option(boost::asio::serial_port::parity(m_parity));
      m_SerialPort.set_option(boost::asio::serial_port::stop_bits(m_stop_bits));
      m_SerialPort.set_option(boost::asio::serial_port::character_size(m_char_size));


    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文