我正在编写代码以使用串行连接连接到仪器并发送一些命令。到目前为止,这是我的代码:
#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个错误,所有这些都在其first
部分的boost的buffer_sequence_adapter.hpp中。库代码当然看起来很好,所以我不确定为什么它不会编译。一些错误如下:
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
感谢任何帮助!
答案 0 :(得分:6)
你只需要将文字包装起来告诉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实际上并没有尝试为特定参数实例化模板。