Close() trigger runtime_error exception when device has been plugged out
xuniuer opened this issue · 1 comments
Hi Sir,
I use a usb type serial device, which is shown as /dev/ttyUSB0 when plugged in. In general, everything is OK by using libserial. Of course when device is plugged out, the related serial operation will throw exceptions.
However, when I re-plug the device in, the port changed to '/dev/ttyUSB1'. I think if I close the previous opened port in time, the port name will still be '/dev/ttyUSB0', if the time interval of plug action is enough long.
Then I try to close opened port, runtime_error
exception thrown. Seems that the SerialPort cannot be released normally in this case. The following is Close() code:
inline
void
SerialPort::Implementation::Close()
{
// Throw an exception if the serial port is not open.
if (not this->IsOpen())
{
throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
}
// Restore the old settings of the port.
if (tcsetattr(this->mFileDescriptor,
TCSANOW,
&mOldPortSettings) < 0)
{
throw std::runtime_error(std::strerror(errno)) ;
}
// Otherwise, close the serial port and set the file descriptor
// to an invalid value.
if (close(this->mFileDescriptor) < 0)
{
throw std::runtime_error(std::strerror(errno)) ;
}
// Set the file descriptor to an invalid value, -1.
mFileDescriptor = -1 ;
}
Based above, the Close() cannot be finished
when a opened port has been removed. Is it an issue?
I try to comment out some lines to 'solve' the program:
inline
void
SerialPort::Implementation::Close()
{
// Throw an exception if the serial port is not open.
if (not this->IsOpen())
{
throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
}
// Modification here - comment out the code block
// Restore the old settings of the port.
//if (tcsetattr(this->mFileDescriptor,
// TCSANOW,
// &mOldPortSettings) < 0)
//{
// throw std::runtime_error(std::strerror(errno)) ;
// }
// Otherwise, close the serial port and set the file descriptor
// to an invalid value.
if (close(this->mFileDescriptor) < 0)
{
throw std::runtime_error(std::strerror(errno)) ;
}
// Set the file descriptor to an invalid value, -1.
mFileDescriptor = -1 ;
}