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:
有什么建议吗?
I'm using CodeBlocks on a windows systems and have downloaded Boost, complied and setup my IDE variables and build options. I have been successfully using other boost libraries and now I need to work on a program that reads and writes to a serial port.
I can not get any example that I try to compile for asio serial port. The following, for example will generate a compile error which follows the code:
#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;
}
This is the build log for the above code:
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:
Any advice ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 boost/asio/serial_port_base.hpp 文件(有点简化):
因此,仅当 BOOST_ASIO_HAS_IOCP 也为 true 时,BOOST_ASIO_HAS_SERIAL_PORT 在 Windows 中才为 true。
然后,来自boost/asio/detail/win_iocp_io_service_fwd.hpp:
所以,如果我没理解错的话,你需要将 _WIN32_WINNT 定义为 0x0400 或以上才能启用它。
From the boost/asio/serial_port_base.hpp file (a bit simplified):
So BOOST_ASIO_HAS_SERIAL_PORT is true in Windows only if BOOST_ASIO_HAS_IOCP is also true.
Then, from boost/asio/detail/win_iocp_io_service_fwd.hpp:
So, if I'm following it right, you need to define _WIN32_WINNT to 0x0400 or above to enable it.