summaryrefslogtreecommitdiffstats
path: root/examples/serialport
diff options
context:
space:
mode:
authorLaszlo Papp <lpapp@kde.org>2014-03-07 02:39:47 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-17 07:29:00 +0100
commit5be9ea056632e54fa8390b4fe865950ca3370260 (patch)
tree70cf718ade7038d43d0d71bb3d63dd81eed3dbd0 /examples/serialport
parent0e46e8d319d054c7325d00b97ba4d7bd30f97e7c (diff)
Set the port settings before open in the examples
Change-Id: I946a3f1d3a64a9b8c0d901347aa8ff5959ebc601 Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Diffstat (limited to 'examples/serialport')
-rw-r--r--examples/serialport/blockingmaster/masterthread.cpp30
-rw-r--r--examples/serialport/blockingslave/slavethread.cpp30
-rw-r--r--examples/serialport/creaderasync/main.cpp29
-rw-r--r--examples/serialport/creadersync/main.cpp29
-rw-r--r--examples/serialport/cwriterasync/main.cpp30
-rw-r--r--examples/serialport/cwritersync/main.cpp29
-rw-r--r--examples/serialport/master/dialog.cpp30
-rw-r--r--examples/serialport/slave/dialog.cpp30
-rw-r--r--examples/serialport/slave/slavethread.cpp30
-rw-r--r--examples/serialport/terminal/mainwindow.cpp18
10 files changed, 16 insertions, 269 deletions
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());