I have a bidirectional communication through the serial port.
MyClass::MyClass(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
//setUp
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
SLOT(handleError(QSerialPort::SerialPortError)));
}
void MyClass::handleError(QSerialPort::SerialPortError error)
{
//show error
//close connection
}
void MyClass::send(QString data)
{
if(!data.endsWith('\r'))
data.append('\r');
//should be like this:
serial->write(data.toLocal8Bit());
serial->waitForBytesWritten(TIMEOUT);
//or this?
if( serial->write(data.toLocal8Bit()) < 0 )
emit serial->error(QSerialPort::WriteError);
if(!serial->waitForBytesWritten(TIMEOUT))
emit serial->error(QSerialPort::TimeoutError);
}
void MyClass::receive(char &dest, int size)
{
//similar to send
}
Namely, handleError will automatically handle the Serial Port Errors or should I detect & throw them?
Which of the 2 ways is more correct? Or none of them is correct?