我不太熟悉boost.asio,但我遇到了一个UART串口程序的问题,我正在设置从串口异步读取的地方。为了记录,我正在使用最新版本的Manjaro Linux。我将包含下面的代码,但无论出于何种原因,处理程序都没有触发(它被称为HandlePortOnReceieve)。我意识到的async_read_some函数确实正在触发。这就是它的样子,在我详细说明我认为什么“可能”成为问题之后,我不知道如何修复甚至测试这个以确定我的想法是否正确
//starts the connection
void SerialHandler::StartConnection(int _baudRate, string _comPort)
{
//handle error codes
boost::system::error_code ec;
//if ports already open, we don't want to run the code
if (serialPort)
{
cout <<"Port already open..." << endl;
return;
}
//create serial port at pointer location and open it
serialPort = serial_port_ptr (new boost::asio::serial_port(ioService));
serialPort->open(_comPort, ec);
//prints error if one is incurred
if (ec)
{
cout << "error: serialPort->open() failed..." <<endl;
cout << "Com port name: " << _comPort << "\nerror code: " << ec.message().c_str() <<endl;
}
//set serial port properties
serialPort->set_option(boost::asio::serial_port_base::baud_rate(_baudRate));
serialPort->set_option(boost::asio::serial_port_base::character_size(8));
serialPort->set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
serialPort->set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
serialPort->set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none));
AsyncReadSome();
boost::thread t1(boost::bind(&boost::asio::io_service::run, &ioService));
}
void SerialHandler::AsyncReadSome()
{
if (serialPort.get()==NULL || !serialPort->is_open())
{
return;
}
//calls async read some, passing in the handler that is called (as well as an error code, of course), to be called when the process is finished in ioService (which is acting in another thread for now).
serialPort->async_read_some (
boost::asio::buffer(rawBuffer, SERIAL_PORT_READ_BUF_SIZE),
boost::bind(
&SerialHandler::HandlePortOnReceive,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void SerialHandler::HandlePortOnReceive(const boost::system::error_code& error, size_t bytes_transferred)
{
boost::mutex::scoped_lock lock(mutex_);
cout<<"test"<<endl;
//if port isnt open at this point or doesn't exist (i.e. if disconnected during running), then we can avoid an error and quit out
if (serialPort.get()==NULL || !serialPort->is_open())
return;
//if there's an error, we'll lose data, but we can always try again as long as program didn't return in the first phase of function
if (error){
AsyncReadSome();
return;
}
for (unsigned int i = 0; i<bytes_transferred; i++)
{
char c = rawBuffer[i];
cout << c << endl;
}
//loop back on itself and start the next read
AsyncReadSome();
}
所以,在我看来,我认为问题在于我至少可以识别的两个可能的地方之一。要么问题是将ioService传递给另一个要运行的线程,这可能只会导致错误,因为我的印象是这样做可能会打开程序以便稍后进行扩展以获得更快的读取。虽然,这只是我读到的内容,因为我从一个我发现的例子中借用了这一特定的行,所以,因为我没有写它,我不完全确定它是如何影响的。
其次,我认为io_service可能正在运行,但它没有工作要做,因为我所定义的AsyncReadSome函数运行得更快,因此它会耗尽工作并返回。
这些都只是工作理论,而且我不知道如何测试,因为它们部分源于我对Boost.Asio的不熟悉。我做了一些研究,但无法找到任何确切的东西,所以也许这里的人可以提供一些见解。提前感谢任何输入