diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -59,6 +59,9 @@ const QMap panelSettingMap {6, "Log"} }); +const char* BPS_TOOLTIP = "bits per second"; +const char* BPS_TOOLTIP_ERR = "Maximum baud rate may be reached!"; + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), @@ -233,7 +236,7 @@ MainWindow::MainWindow(QWidget *parent) // init bps (bits per second) counter bpsLabel.setText("0bps"); - bpsLabel.setToolTip(tr("bits per second")); + bpsLabel.setToolTip(tr(BPS_TOOLTIP)); ui->statusBar->addPermanentWidget(&bpsLabel); connect(&bpsTimer, &QTimer::timeout, this, &MainWindow::onBpsTimeout); @@ -372,6 +375,7 @@ void MainWindow::onPortToggled(bool open bpsTimer.stop(); // if not cleared last displayed value is stuck bpsLabel.setText("0bps"); + bpsLabel.setToolTip(tr(BPS_TOOLTIP)); spsLabel.setText("0sps"); } } @@ -404,7 +408,20 @@ void MainWindow::onSpsChanged(float sps) void MainWindow::onBpsTimeout() { unsigned bits = dataFormatPanel.getBytesRead() * 8; - bpsLabel.setText(QString("%1bps").arg(bits)); + unsigned maxBps = portControl.maxBitRate(); + QString str; + if (bits >= maxBps) + { + // TODO: an icon for bps warning + str = QString(tr("!%1/%2bps")).arg(bits).arg(maxBps); + bpsLabel.setToolTip(tr(BPS_TOOLTIP_ERR)); + } + else + { + str = QString(tr("%1bps")).arg(bits); + bpsLabel.setToolTip(tr(BPS_TOOLTIP)); + } + bpsLabel.setText(str); } bool MainWindow::isDemoRunning() diff --git a/src/portcontrol.cpp b/src/portcontrol.cpp --- a/src/portcontrol.cpp +++ b/src/portcontrol.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2017 Hasan Yavuz Özderya + Copyright © 2019 Hasan Yavuz Özderya This file is part of serialplot. @@ -453,6 +453,27 @@ QString PortControl::currentFlowControlT } } +unsigned PortControl::maxBitRate() const +{ + float baud = serialPort->baudRate(); + float dataBits = serialPort->dataBits(); + float parityBits = serialPort->parity() == QSerialPort::NoParity ? 0 : 1; + + float stopBits; + if (serialPort->stopBits() == QSerialPort::OneAndHalfStop) + { + stopBits = 1.5; + } + else + { + stopBits = serialPort->stopBits(); + } + + float frame_size = 1 /* start bit */ + dataBits + parityBits + stopBits; + + return float(baud) / frame_size; +} + void PortControl::saveSettings(QSettings* settings) { settings->beginGroup(SettingGroup_Port); diff --git a/src/portcontrol.h b/src/portcontrol.h --- a/src/portcontrol.h +++ b/src/portcontrol.h @@ -1,5 +1,5 @@ /* - Copyright © 2017 Hasan Yavuz Özderya + Copyright © 2019 Hasan Yavuz Özderya This file is part of serialplot. @@ -47,6 +47,9 @@ public: QSerialPort* serialPort; QToolBar* toolBar(); + /// Returns maximum bit rate for current baud rate + unsigned maxBitRate() const; + /// Stores port settings into a `QSettings` void saveSettings(QSettings* settings); /// Loads port settings from a `QSettings`. If open serial port is closed.