我正在尝试创建一个将从串行端口读取和解析数据的类,但是我不断收到实例化的错误,我不知道为什么。该类接受串行端口及其io_service。我正在使用提升。我收到了很多错误,但我认为这是因为它的累积(我想,我不确定这是否正确)。这是第一个:
在/usr/include/boost/bind.hpp:22:0中包含的文件中, 来自../Sources/Magnetic Compensator Core.cpp:17:
/usr/include/boost/bind/bind.hpp:实例化'boost :: _ bi :: result_traits&,const boost :: system :: error_code&,unsigned int)>': /usr/include/boost/bind/bind_template.hpp:15:48:从'boost :: _ bi :: bind_t&,const boost :: system :: error_code&amp ;, unsigned int)实例化,boost :: _ bi :: list4 ,boost :: reference_wrapper>,boost :: arg< 1&gt ;, boost :: arg< 2> > >”
以下是该类的代码:
class mag_serial
{
bool data_available;
boost::asio::serial_port& ser_port;
boost::asio::deadline_timer timeout;
char my_buffer[1];
std::string str;
std::string st;
void read_callback(bool& data_available, boost::asio::deadline_timer& timeout, const boost::system::error_code& error, std::size_t bytes_transferred)
{
data_available = true;
if(str.length() > 1)
{
if (!(str.at(str.length() - 1) == temp))//&str.at(str.length() - 1) != "#")
{
str.append(my_buffer,bytes_transferred);
if(str.at(str.length() - 1) == quit)
{
cout << "I am quitting";
Stop();
ser_port.cancel();
ser_port.close();
return;
}
i++;
}
else if (str.at(str.length() - 1) == temp)
{
st = str;//.substr(1, str.size());
// Processing Functions
}
}
else
{
str.append(my_buffer,bytes_transferred);
if(str.at(0) == quit)
{
cout << "I am quitting";
Stop();
ser_port.cancel();
ser_port.close();
return;
}
}
ser_port.async_read_some(boost::asio::buffer(my_buffer),
boost::bind(&mag_serial::read_callback, boost::ref(data_available),
boost::ref(timeout),boost::asio::placeholders::error(),
boost::asio::placeholders::bytes_transferred()));
data_available = true;
}
void wait_callback(boost::asio::serial_port& ser_port, const boost::system::error_code& error)
{
if (error)
{
// Data was read and this timeout was cancelled
return;
}
}
public:
mag_serial(boost::asio::serial_port& ser_port, boost::asio::io_service& io_svc): ser_port(ser_port), timeout(ser_port.get_io_service()){}
void read_mag_serial_thread()
{
bool data_available = false;
ser_port.async_read_some(boost::asio::buffer(my_buffer),
boost::bind(&mag_serial::read_callback, boost::ref(data_available),
boost::ref(timeout),boost::asio::placeholders::error(),
boost::asio::placeholders::bytes_transferred()));
timeout.expires_from_now(boost::posix_time::seconds(1));
timeout.async_wait(boost::bind(&mag_serial::wait_callback, boost::ref(ser_port),boost::asio::placeholders::error()));
io_svc.run();
if(!data_available)
{
ser_port.close();
cout << "ser_port was closed";
}
}
};
答案 0 :(得分:1)
这不会编译
ser_port.async_read_some(boost::asio::buffer(my_buffer),
boost::bind(&mag_serial::read_callback, boost::ref(data_available),
boost::ref(timeout),boost::asio::placeholders::error(),
boost::asio::placeholders::bytes_transferred()));
成员函数mag_serial::read_callback
需要一个实例来绑定。
ser_port.async_read_some(
boost::asio::buffer(my_buffer),
boost::bind(
&mag_serial::read_callback,
this,
boost::ref(data_available),
boost::ref(timeout),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
这是coliru,我没有尝试修改格式。