From 5be9ea056632e54fa8390b4fe865950ca3370260 Mon Sep 17 00:00:00 2001 From: Laszlo Papp Date: Fri, 7 Mar 2014 02:39:47 +0000 Subject: Set the port settings before open in the examples Change-Id: I946a3f1d3a64a9b8c0d901347aa8ff5959ebc601 Reviewed-by: Sergey Belyashov --- .../serialport/blockingmaster/masterthread.cpp | 30 ---------------------- examples/serialport/blockingslave/slavethread.cpp | 30 ---------------------- examples/serialport/creaderasync/main.cpp | 29 +++------------------ examples/serialport/creadersync/main.cpp | 29 +++------------------ examples/serialport/cwriterasync/main.cpp | 30 ++-------------------- examples/serialport/cwritersync/main.cpp | 29 +++------------------ examples/serialport/master/dialog.cpp | 30 ---------------------- examples/serialport/slave/dialog.cpp | 30 ---------------------- examples/serialport/slave/slavethread.cpp | 30 ---------------------- examples/serialport/terminal/mainwindow.cpp | 18 ++++--------- 10 files changed, 16 insertions(+), 269 deletions(-) (limited to 'examples/serialport') diff --git a/examples/serialport/blockingmaster/masterthread.cpp b/examples/serialport/blockingmaster/masterthread.cpp index 5f28e82d..bef37f93 100644 --- a/examples/serialport/blockingmaster/masterthread.cpp +++ b/examples/serialport/blockingmaster/masterthread.cpp @@ -109,36 +109,6 @@ void MasterThread::run() .arg(portName).arg(serial.error())); return; } - - if (!serial.setBaudRate(QSerialPort::Baud9600)) { - emit error(tr("Can't set baud rate 9600 baud to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setDataBits(QSerialPort::Data8)) { - emit error(tr("Can't set 8 data bits to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setParity(QSerialPort::NoParity)) { - emit error(tr("Can't set no parity to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setStopBits(QSerialPort::OneStop)) { - emit error(tr("Can't set 1 stop bit to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setFlowControl(QSerialPort::NoFlowControl)) { - emit error(tr("Can't set no flow control to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } } //! [7] //! [8] // write request diff --git a/examples/serialport/blockingslave/slavethread.cpp b/examples/serialport/blockingslave/slavethread.cpp index 0ceff320..4328e750 100644 --- a/examples/serialport/blockingslave/slavethread.cpp +++ b/examples/serialport/blockingslave/slavethread.cpp @@ -105,36 +105,6 @@ void SlaveThread::run() .arg(portName).arg(serial.error())); return; } - - if (!serial.setBaudRate(QSerialPort::Baud9600)) { - emit error(tr("Can't set baud rate 9600 baud to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setDataBits(QSerialPort::Data8)) { - emit error(tr("Can't set 8 data bits to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setParity(QSerialPort::NoParity)) { - emit error(tr("Can't set no parity to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setStopBits(QSerialPort::OneStop)) { - emit error(tr("Can't set 1 stop bit to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setFlowControl(QSerialPort::NoFlowControl)) { - emit error(tr("Can't set no flow control to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } } if (serial.waitForReadyRead(currentWaitTimeout)) { diff --git a/examples/serialport/creaderasync/main.cpp b/examples/serialport/creaderasync/main.cpp index 96fecddf..5befaca1 100644 --- a/examples/serialport/creaderasync/main.cpp +++ b/examples/serialport/creaderasync/main.cpp @@ -67,34 +67,11 @@ int main(int argc, char *argv[]) QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); - if (!serialPort.open(QIODevice::ReadOnly)) { - standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; - if (!serialPort.setBaudRate(serialPortBaudRate)) { - standardOutput << QObject::tr("Failed to set 9600 baud for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setDataBits(QSerialPort::Data8)) { - standardOutput << QObject::tr("Failed to set 8 data bits for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } + serialPort.setBaudRate(serialPortBaudRate); - if (!serialPort.setParity(QSerialPort::NoParity)) { - standardOutput << QObject::tr("Failed to set no parity for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setStopBits(QSerialPort::OneStop)) { - standardOutput << QObject::tr("Failed to set 1 stop bit for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setFlowControl(QSerialPort::NoFlowControl)) { - standardOutput << QObject::tr("Failed to set no flow control for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + if (!serialPort.open(QIODevice::ReadOnly)) { + standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } diff --git a/examples/serialport/creadersync/main.cpp b/examples/serialport/creadersync/main.cpp index 47f6ee8a..0c0ff8c3 100644 --- a/examples/serialport/creadersync/main.cpp +++ b/examples/serialport/creadersync/main.cpp @@ -64,34 +64,11 @@ int main(int argc, char *argv[]) QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); - if (!serialPort.open(QIODevice::ReadOnly)) { - standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.error()) << endl; - return 1; - } - int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; - if (!serialPort.setBaudRate(serialPortBaudRate)) { - standardOutput << QObject::tr("Failed to set 9600 baud for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setDataBits(QSerialPort::Data8)) { - standardOutput << QObject::tr("Failed set 8 data bits for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } + serialPort.setBaudRate(serialPortBaudRate); - if (!serialPort.setParity(QSerialPort::NoParity)) { - standardOutput << QObject::tr("Failed to set no parity for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setStopBits(QSerialPort::OneStop)) { - standardOutput << QObject::tr("Failed to set 1 stop bit for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setFlowControl(QSerialPort::NoFlowControl)) { - standardOutput << QObject::tr("Failed set no flow control for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + if (!serialPort.open(QIODevice::ReadOnly)) { + standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.error()) << endl; return 1; } diff --git a/examples/serialport/cwriterasync/main.cpp b/examples/serialport/cwriterasync/main.cpp index 4277914f..310d896b 100644 --- a/examples/serialport/cwriterasync/main.cpp +++ b/examples/serialport/cwriterasync/main.cpp @@ -67,36 +67,10 @@ int main(int argc, char *argv[]) QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); - if (!serialPort.open(QIODevice::WriteOnly)) { - standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; - if (!serialPort.setBaudRate(serialPortBaudRate)) { - standardOutput << QObject::tr("Failed to set 9600 baud for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setDataBits(QSerialPort::Data8)) { - standardOutput << QObject::tr("Failed to set 8 data bits for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } + serialPort.setBaudRate(serialPortBaudRate); - if (!serialPort.setParity(QSerialPort::NoParity)) { - standardOutput << QObject::tr("Failed to set no parity for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setStopBits(QSerialPort::OneStop)) { - standardOutput << QObject::tr("Failed to set 1 stop bit for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setFlowControl(QSerialPort::NoFlowControl)) { - standardOutput << QObject::tr("Failed to set no flow control for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } + serialPort.open(QIODevice::WriteOnly); QFile dataFile; if (!dataFile.open(stdin, QIODevice::ReadOnly)) { diff --git a/examples/serialport/cwritersync/main.cpp b/examples/serialport/cwritersync/main.cpp index 6433c7c2..4b68315a 100644 --- a/examples/serialport/cwritersync/main.cpp +++ b/examples/serialport/cwritersync/main.cpp @@ -65,34 +65,11 @@ int main(int argc, char *argv[]) QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); - if (!serialPort.open(QIODevice::WriteOnly)) { - standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; - if (!serialPort.setBaudRate(serialPortBaudRate)) { - standardOutput << QObject::tr("Failed to set 9600 baud for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setDataBits(QSerialPort::Data8)) { - standardOutput << QObject::tr("Failed to set 8 data bits for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } + serialPort.setBaudRate(serialPortBaudRate); - if (!serialPort.setParity(QSerialPort::NoParity)) { - standardOutput << QObject::tr("Failed to set no parity for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setStopBits(QSerialPort::OneStop)) { - standardOutput << QObject::tr("Failed to set 1 stop bit for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; - return 1; - } - - if (!serialPort.setFlowControl(QSerialPort::NoFlowControl)) { - standardOutput << QObject::tr("Failed to set no flow control for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; + if (!serialPort.open(QIODevice::WriteOnly)) { + standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } diff --git a/examples/serialport/master/dialog.cpp b/examples/serialport/master/dialog.cpp index 9525cc57..7794b2d1 100644 --- a/examples/serialport/master/dialog.cpp +++ b/examples/serialport/master/dialog.cpp @@ -107,36 +107,6 @@ void Dialog::sendRequest() .arg(serial.portName()).arg(serial.error())); return; } - - if (!serial.setBaudRate(QSerialPort::Baud9600)) { - processError(tr("Can't set rate 9600 baud to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setDataBits(QSerialPort::Data8)) { - processError(tr("Can't set 8 data bits to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setParity(QSerialPort::NoParity)) { - processError(tr("Can't set no parity to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setStopBits(QSerialPort::OneStop)) { - processError(tr("Can't set 1 stop bit to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setFlowControl(QSerialPort::NoFlowControl)) { - processError(tr("Can't set no flow control to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } } setControlsEnabled(false); diff --git a/examples/serialport/slave/dialog.cpp b/examples/serialport/slave/dialog.cpp index 2475869c..b874d90b 100644 --- a/examples/serialport/slave/dialog.cpp +++ b/examples/serialport/slave/dialog.cpp @@ -114,36 +114,6 @@ void Dialog::startSlave() .arg(serial.portName()).arg(serial.error())); return; } - - if (!serial.setBaudRate(QSerialPort::Baud9600)) { - processError(tr("Can't set baud rate 9600 baud to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setDataBits(QSerialPort::Data8)) { - processError(tr("Can't set 8 data bits to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setParity(QSerialPort::NoParity)) { - processError(tr("Can't set no parity to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setStopBits(QSerialPort::OneStop)) { - processError(tr("Can't set 1 stop bit to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } - - if (!serial.setFlowControl(QSerialPort::NoFlowControl)) { - processError(tr("Can't set no flow control to port %1, error code %2") - .arg(serial.portName()).arg(serial.error())); - return; - } } runButton->setEnabled(false); diff --git a/examples/serialport/slave/slavethread.cpp b/examples/serialport/slave/slavethread.cpp index b54ddcdd..e7808459 100644 --- a/examples/serialport/slave/slavethread.cpp +++ b/examples/serialport/slave/slavethread.cpp @@ -99,36 +99,6 @@ void SlaveThread::run() .arg(portName).arg(serial.error())); return; } - - if (!serial.setBaudRate(QSerialPort::Baud9600)) { - emit error(tr("Can't set baud rate 9600 baud to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setDataBits(QSerialPort::Data8)) { - emit error(tr("Can't set 8 data bits to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setParity(QSerialPort::NoParity)) { - emit error(tr("Can't set no parity to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setStopBits(QSerialPort::OneStop)) { - emit error(tr("Can't set 1 stop bit to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } - - if (!serial.setFlowControl(QSerialPort::NoFlowControl)) { - emit error(tr("Can't set no flow control to port %1, error code %2") - .arg(portName).arg(serial.error())); - return; - } } if (serial.waitForReadyRead(currentWaitTimeout)) { diff --git a/examples/serialport/terminal/mainwindow.cpp b/examples/serialport/terminal/mainwindow.cpp index f4fc5879..c78693cf 100644 --- a/examples/serialport/terminal/mainwindow.cpp +++ b/examples/serialport/terminal/mainwindow.cpp @@ -92,13 +92,12 @@ void MainWindow::openSerialPort() { SettingsDialog::Settings p = settings->settings(); serial->setPortName(p.name); + serial->setBaudRate(p.baudRate); + serial->setDataBits(p.dataBits); + serial->setParity(p.parity); + serial->setStopBits(p.stopBits); + serial->setFlowControl(p.flowControl); if (serial->open(QIODevice::ReadWrite)) { - if (serial->setBaudRate(p.baudRate) - && serial->setDataBits(p.dataBits) - && serial->setParity(p.parity) - && serial->setStopBits(p.stopBits) - && serial->setFlowControl(p.flowControl)) { - console->setEnabled(true); console->setLocalEchoEnabled(p.localEchoEnabled); ui->actionConnect->setEnabled(false); @@ -107,13 +106,6 @@ void MainWindow::openSerialPort() ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits) .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl)); - - } else { - serial->close(); - QMessageBox::critical(this, tr("Error"), serial->errorString()); - - ui->statusBar->showMessage(tr("Configure error")); - } } else { QMessageBox::critical(this, tr("Error"), serial->errorString()); -- cgit v1.2.3