提升Asio串口问题

时间:2011-08-16 16:55:50

标签: c++ windows boost serial-port boost-asio

我在Windows系统上使用CodeBlocks并已下载Boost,编译并设置我的IDE变量和构建选项。我已成功使用其他boost库,现在我需要处理一个读取和写入串行端口的程序。

我无法得到任何我尝试编译asio串口的例子。例如,下面的代码将生成一个编译错误:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
#include <boost/thread.hpp>



int main()
{


    boost::asio::io_service io_service;
    boost::asio::serial_port port(io_service);


    return 0;

}

这是上述代码的构建日志:

Compiling: main.cpp
In file included from C:\Dev\boost_1_47_0/boost/thread/win32/thread_data.hpp:12,
                 from C:\Dev\boost_1_47_0/boost/thread/thread.hpp:15,
                 from C:\Dev\boost_1_47_0/boost/thread.hpp:13,
                 from C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp:4:
C:\Dev\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:59: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as  dllimport: attribute ignored
C:\Dev\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:69: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as  dllimport: attribute ignored
C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp: In function 'int main()':
C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp:13: error: 'serial_port' is not a member of 'boost::asio'
C:\Users\bjune\Documents\CodeBlocks\hellow\main.cpp:13: error: expected ';' before 'port'
C:\Dev\boost_1_47_0/boost/system/error_code.hpp: At global scope:

任何建议?

1 个答案:

答案 0 :(得分:3)

来自 boost / asio / serial_port_base.hpp 文件(稍微简化):

#if defined(BOOST_ASIO_HAS_IOCP) || !defined(BOOST_WINDOWS)
#  define BOOST_ASIO_HAS_SERIAL_PORT 1
#endif

只有当BOOST_ASIO_HAS_IOCP也为真时,BOOST_ASIO_HAS_SERIAL_PORT才会在Windows中生效。

然后,从 boost / asio / detail / win_iocp_io_service_fwd.hpp

#if defined(BOOST_WINDOWS)
#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0400)

// Define this to indicate that IOCP is supported on the target platform.
#  define BOOST_ASIO_HAS_IOCP 1
#endif
#endif

所以,如果我正确地遵循它,你需要将_WIN32_WINNT定义为0x0400或更高版本以启用它。