起初,我不是专业的c ++编程,我的问题可能非常简单。
我正在尝试使用boost asio来访问串口并使用shared_ptr来防止内存泄漏。我搜索了一些示例代码,但对我来说,其中很多都不起作用。
我的代码如下:
SerialPortConnection.cpp:
#include "SerialPortConnection.h"
SerialPortConnection::SerialPortConnection( std::string port_name_init, int baud_rate_init,
boost::asio::serial_port_base::character_size charSize_init,
boost::asio::serial_port_base::stop_bits stopBit_init,
boost::asio::serial_port_base::parity pariy_init,
boost::asio::serial_port_base::flow_control flowControl_init) :
port_name(port_name_init),
baud_rate(baud_rate_init),
charSize(charSize_init),
stopBit(stopBit_init),
pariy(pariy_init),
flowControl(flowControl_init)
{
port = boost::shared_ptr<serial_port_ptr>( new boost::asio::serial_port );
}
SerialPortConnection::~SerialPortConnection()
{
//dtor
}
SerialPortConnection.h:
#ifndef SERIALPORTCONNECTION_H
#define SERIALPORTCONNECTION_H
#include <iostream>
#include <string>
#include <memory>
#include <boost/asio.hpp>
#include <boost/asio/serial_port.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
//using namespace::boost::asio;
class SerialPortConnection
{
public:
SerialPortConnection( std::string port_name_init, int baud_rate_init,
boost::asio::serial_port_base::character_size charSize000_init,
boost::asio::serial_port_base::stop_bits stopBit000_init,
boost::asio::serial_port_base::parity pariy000_init,
boost::asio::serial_port_base::flow_control flowControl_init );
virtual ~SerialPortConnection();
private:
std::string port_name;
int baud_rate;
boost::asio::serial_port_base::character_size charSize;
boost::asio::serial_port_base::stop_bits stopBit;
boost::asio::serial_port_base::parity pariy;
boost::asio::serial_port_base::flow_control flowControl;
boost::asio::io_service io;
typedef boost::asio::serial_port* serial_port_ptr;
boost::shared_ptr < serial_port_ptr > port;
};
#endif // SERIALPORTCONNECTION_H
编译器创建编译错误:
||=== Build: Debug in test-1 (compiler: GNU GCC Compiler) ===|
/home/mkh/cb-codes/test-prjs/test-1/src/SerialPortConnection.cpp||In constructor ‘SerialPortConnection::SerialPortConnection(std::__cxx11::string, int, boost::asio::serial_port_base::character_size, boost::asio::serial_port_base::stop_bits, boost::asio::serial_port_base::parity, boost::asio::serial_port_base::flow_control)’:|
/home/mkh/cb-codes/test-prjs/test-1/src/SerialPortConnection.cpp|17|error: no matching function for call to ‘boost::asio::basic_serial_port<>::basic_serial_port()’|
/usr/include/boost/asio/basic_serial_port.hpp|147|note: candidate: boost::asio::basic_serial_port<SerialPortService>::basic_serial_port(boost::asio::basic_serial_port<SerialPortService>&&) [with SerialPortService = boost::asio::serial_port_service]|
/usr/include/boost/asio/basic_serial_port.hpp|147|note: candidate expects 1 argument, 0 provided|
/usr/include/boost/asio/basic_serial_port.hpp|126|note: candidate: boost::asio::basic_serial_port<SerialPortService>::basic_serial_port(boost::asio::io_service&, const native_handle_type&) [with SerialPortService = boost::asio::serial_port_service; boost::asio::basic_serial_port<SerialPortService>::native_handle_type = int]|
/usr/include/boost/asio/basic_serial_port.hpp|126|note: candidate expects 2 arguments, 0 provided|
/usr/include/boost/asio/basic_serial_port.hpp|105|note: candidate: boost::asio::basic_serial_port<SerialPortService>::basic_serial_port(boost::asio::io_service&, const string&) [with SerialPortService = boost::asio::serial_port_service; std::__cxx11::string = std::__cxx11::basic_string<char>]|
/usr/include/boost/asio/basic_serial_port.hpp|105|note: candidate expects 2 arguments, 0 provided|
/usr/include/boost/asio/basic_serial_port.hpp|85|note: candidate: boost::asio::basic_serial_port<SerialPortService>::basic_serial_port(boost::asio::io_service&, const char*) [with SerialPortService = boost::asio::serial_port_service]|
/usr/include/boost/asio/basic_serial_port.hpp|85|note: candidate expects 2 arguments, 0 provided|
/usr/include/boost/asio/basic_serial_port.hpp|69|note: candidate: boost::asio::basic_serial_port<SerialPortService>::basic_serial_port(boost::asio::io_service&) [with SerialPortService = boost::asio::serial_port_service]|
/usr/include/boost/asio/basic_serial_port.hpp|69|note: candidate expects 1 argument, 0 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我尝试了各种解决方案。例如,我将这个添加到SerialPortConnection.h:
boost :: asio :: serial_port port0;
但它也不起作用。它实际上导致另一个编译错误。 现在我需要知道如何使用shared_ptr创建一个串口。
谢谢,
答案 0 :(得分:1)
答案在您的错误消息中。 no matching function for call to...
serial_port
没有构造函数接受零参数。你需要传递io_service之类的东西。以下是构造函数。
basic_serial_port documentation
所以你会:
port = boost::shared_ptr<serial_port_ptr>( boost::make_shared< boost::asio::serial_port >(io_service_));
请注意使用'make_shared
而不是new
。
这是一个编译
的例子答案 1 :(得分:1)
shared_ptr是不必要的。这是一段代码:
#include <boost/asio/serial_port.hpp>
class SerialPortConnection
{
public:
SerialPortConnection();
private:
boost::asio::io_service m_ioService; //order is important so that m_ioService gets constructed first
boost::asio::serial_port m_port;
};
SerialPortConnection::SerialPortConnection()
: m_port(m_ioService)
{
boost::system::error_code ec; // choice: without ec Boost.Asio may throw
m_port.open("COM1", ec);
if (!ec)
{
boost::asio::serial_port_base::baud_rate baud_rate1(9600);
m_port.set_option(baud_rate1);
//etc.
}
}
请注意,Boost.Asio主要使用异步模型。它也支持同步操作,但不支持超时(这是一个主要的用例),并且模仿这种行为很麻烦。