2011-04-04 55 views

回答

8

你如何使用它取決於你想要做什麼,;-)。

的文檔在這裏找到:

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的autolinking和Boost.Regex 庫。另外, 你可以選擇建造這些 庫,並鏈接到他們

如果你不希望依賴關係到其他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)); 


    } 
} 
+0

我必須編寫必須在Windows和Unix系統上運行的代碼。 現在,當你需要它時,我把IF DEF WIN32等 如何填充這個庫來實現這個目標? – Safari 2011-04-04 12:30:17

+0

根本不需要定義win32 - 它們都已經隱藏在asio庫中。圖書館完全是跨平臺的。如果您使用boost.asio作爲您自己的dll庫的一部分,那麼您需要如果def win32去除dll導出(這在Windows中需要但不是gcc),那麼您需要這樣做的唯一原因是: - 請參閱http: //msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx – Tom 2011-04-04 12:42:40

-1

通讀以前關於SO的一些​​問題。您將瞭解使用此庫時使用的一些技巧。

+0

我必須編寫必須在Windows和Unix系統上運行的代碼。 現在,當你需要它時,我把IF DEF WIN32等 如何填充這個庫來實現這個目標? – Safari 2011-04-04 12:27:35