'QT QSerialPort Automatic connection to the com port when the connection is lost

There is a connection to the com port and receiving data. When the connection breaks, and then the port is again available, the data does not go, you have to restart the program. How can I make the program constantly monitor the availability of the port, and if it is available, connect to it and continue to read data?

SerialPort::SerialPort(QObject *parent):QObject(parent)
{
  m_serialPort = new QSerialPort(this);
  connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPort::onReadData);
  openDefault();
}

SerialPort::~SerialPort()
{
  delete m_serialPort;
}

void SerialPort::set_serial_data(QString newValue)
{
   mSerial_data = newValue;
   emit serial_data_Changed(mSerial_data);
}

void SerialPort::onReadData()
{
   while (m_serialPort->canReadLine()) {
       QByteArray data = m_serialPort->readLine();
       QString value = QString(data).trimmed();
       set_serial_data(value);
   }
}

void SerialPort::openDefault()
{
   m_serialPort->setPortName("ttyAMA0");
   m_serialPort->setBaudRate(QSerialPort::Baud115200);
   m_serialPort->setDataBits(QSerialPort::Data8);
   m_serialPort->setParity(QSerialPort::NoParity);
   m_serialPort->setStopBits(QSerialPort::OneStop);
   m_serialPort->setFlowControl(QSerialPort::NoFlowControl); 
}

QString SerialPort::get_serial_data() const
{
   return mSerial_data;
}
qt


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source