我正在编写代码以使用串行连接连接到仪器并发送一些命令。到目前为止,这是我的代码:
#include <boost/asio/basic_serial_port.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
#define PORT "COM3"
#define BAUD 9600
#define DATABITS 8
#define PARITY none
#define STOP_BITS one
#define FLOW_CONTROL none
int main()
{
using namespace boost;
//Create the serial connection to the scope
asio::io_service io;
asio::basic_serial_port<asio::serial_port_service> scope(io);
//Open the connection and configure it
scope.open(PORT);
scope.set_option(asio::serial_port_base::baud_rate(BAUD));
scope.set_option(asio::serial_port_base::flow_control(asio::serial_port_base::flow_control::FLOW_CONTROL));
scope.set_option(asio::serial_port_base::parity(asio::serial_port_base::parity::PARITY));
scope.set_option(asio::serial_port_base::stop_bits(asio::serial_port_base::stop_bits::STOP_BITS));
scope.set_option(asio::serial_port_base::character_size(DATABITS));
//Open the connection
//Send some test commands
scope.write_some("MOVE X Y \n");
//Close the port
scope.close();
return 0;
}
它在 visual studio 中看起来不错,但在编译时给我 16 个错误,所有错误都在 boost 的 buffer_sequence_adapter.hpp 的 first
部分。库代码当然看起来不错,所以我不确定为什么它不能编译。部分错误如下:
Error 1 error C2825: 'Buffers': must be a class or namespace when followed by '::' c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 156 1 ConsoleApplication1
Error 2 error C2039: 'const_iterator' : is not a member of '`global namespace'' c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 156 1 ConsoleApplication1
Error 3 error C2146: syntax error : missing ';' before identifier 'iter' c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 156 1 ConsoleApplication1
Error 4 error C2734: 'const_iterator' : const object must be initialized if not extern c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 156 1 ConsoleApplication1
Error 5 error C2065: 'iter' : undeclared identifier c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 156 1 ConsoleApplication1
Error 6 error C2228: left of '.begin' must have class/struct/union c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 156 1 ConsoleApplication1
Error 7 error C2825: 'Buffers': must be a class or namespace when followed by '::' c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 157 1 ConsoleApplication1
Error 8 error C2039: 'const_iterator' : is not a member of '`global namespace'' c:\program files\boost_1_55_0\boost\asio\detail\buffer_sequence_adapter.hpp 157 1 ConsoleApplication1
感谢任何帮助!
最佳答案
你只需要包装文字来告诉 Asio 你想如何解释缓冲区,例如
scope.write_some(boost::asio::buffer("MOVE X Y \n"));
注意,以这种方式,缓冲区也包含尾随的 NUL 字符。如果你想避免这种情况,你可以
const char* message = "MOVE X Y \n";
scope.write_some(boost::asio::buffer(message, strlen(message)));
背景:“它在 VS 中看起来没问题”的原因(您可能是指 Intellisense 在编译前没有诊断错误)是因为(至少有几个重载)write_some
是函数模板,而 Intellisense 实际上并不尝试为特定参数实例化模板。
关于c++ - boost buffer_sequence_adapter 中的奇怪编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24020677/