【发布时间】:2011-07-29 01:33:09
【问题描述】:
我会尝试使用该库在多个平台上使用套接字 Boost.Asio c++。 我在这里下载了最新版本:
http://sourceforge.net/projects/boost/files/boost/1.46.1/
但是现在我在我的代码中使用什么? 我有编译吗?包括就够了吗? 可以告诉我步骤吗?
【问题讨论】:
标签: c++ boost boost-asio
我会尝试使用该库在多个平台上使用套接字 Boost.Asio c++。 我在这里下载了最新版本:
http://sourceforge.net/projects/boost/files/boost/1.46.1/
但是现在我在我的代码中使用什么? 我有编译吗?包括就够了吗? 可以告诉我步骤吗?
【问题讨论】:
标签: c++ boost boost-asio
你如何使用它取决于你想做什么,;-)。
文档可在此处找到:
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/
有关其他文档的来源,请参阅 SO:Best documentation for 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));
}
}
【讨论】:
通读之前关于 SO 的一些 Boost.Asio 问题。您将对使用此库时使用的一些技术有一个很好的了解。
【讨论】: