C++ boost-处理串行端口的文件结束错误(usb电缆重新连接)
我使用boost从串行端口读/写。设备通过USB(ttyACM0)连接。我的代码可以工作,但我不确定我是否正确处理了在读取过程中断开usb电缆时出现的文件结束错误。打开串行端口如下所示(我在这里只列出了代码中最重要的部分): 以下是阅读功能:C++ boost-处理串行端口的文件结束错误(usb电缆重新连接),c++,boost,C++,Boost,我使用boost从串行端口读/写。设备通过USB(ttyACM0)连接。我的代码可以工作,但我不确定我是否正确处理了在读取过程中断开usb电缆时出现的文件结束错误。打开串行端口如下所示(我在这里只列出了代码中最重要的部分): 以下是阅读功能: void startRead() { serialPort_.async_read_some(boost::asio::buffer(readSerialPortData_), boost::bind(&SerialPortAccess
void startRead()
{
serialPort_.async_read_some(boost::asio::buffer(readSerialPortData_),
boost::bind(&SerialPortAccess::readComplete, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void readComplete(const boost::system::error_code& error, size_t bytesTransferred)
{
if (!error)
{
if (bytesTransferred > 0 && !readSerialPortData_.empty())
{
//handle data
}
startRead();
}
else
{
if(error == boost::asio::error::eof)
{
//async try to open serial port (someone may connect usb cable again), notify other thread (code presented above)
open("/dev/ttyACM0");
}
else
{
close();
}
}
}
实现是否正确?您的
打开
和关闭
函数做什么?
void startRead()
{
serialPort_.async_read_some(boost::asio::buffer(readSerialPortData_),
boost::bind(&SerialPortAccess::readComplete, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void readComplete(const boost::system::error_code& error, size_t bytesTransferred)
{
if (!error)
{
if (bytesTransferred > 0 && !readSerialPortData_.empty())
{
//handle data
}
startRead();
}
else
{
if(error == boost::asio::error::eof)
{
//async try to open serial port (someone may connect usb cable again), notify other thread (code presented above)
open("/dev/ttyACM0");
}
else
{
close();
}
}
}