Skip to content

Commit

Permalink
Merge pull request #19 from slapab/manual_control_rts_dtr
Browse files Browse the repository at this point in the history
Added functionality to manually control state of RTS and DTR lines when port has been opened with no flow control or software flow control enabled.
  • Loading branch information
cyc1ingsir committed Oct 31, 2016
2 parents e94ca54 + f9a0b89 commit 7da86b3
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 5 deletions.
33 changes: 33 additions & 0 deletions controlpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ ControlPanel::ControlPanel(QWidget *parent, Settings *settings)
setAutoFillBackground(true);
// setWindowOpacity(0.1);
m_bt_open->setCheckable(true);
// Disable RTS and CTS checkboxes during construction
m_rts_line->setEnabled(false);
m_dtr_line->setEnabled(false);

// Connect button signal to slot
connect(m_bt_settings, &QPushButton::clicked, this, &ControlPanel::toggleMenu);
Expand Down Expand Up @@ -129,10 +132,19 @@ void ControlPanel::toggleDevice(bool open)
if (open) {
if (m_menuVisible)
toggleMenu();
// Enable RTS and DTR checkboxes when opening device. Note that visibility of these
// widgets depends on currently selected type of flow control.
m_rts_line->setEnabled(true);
m_dtr_line->setEnabled(true);

m_bt_settings->setEnabled(false);
m_bt_open->setText(tr("Cl&ose"));
emit openDeviceClicked();
} else {
// Disable RTS and DTR checkboxes when closing device.
m_rts_line->setEnabled(false);
m_dtr_line->setEnabled(false);

m_bt_settings->setEnabled(true);
emit closeDeviceClicked();
m_bt_open->setText(tr("&Open"));
Expand Down Expand Up @@ -319,6 +331,11 @@ void ControlPanel::fillFlowCombo()

connect(m_combo_flowControl, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
[=]() { emit settingChanged(Settings::FlowControl, m_combo_flowControl->currentData()); });

// Connect flow-control's combobox index changed signal to the slot that sets the visibility of RTS and DTR
// checkboxes
connect(m_combo_flowControl, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
&ControlPanel::changeVisibilityOfRTSandDTRCheckboxes);
}

void ControlPanel::fillParityCombo()
Expand Down Expand Up @@ -378,3 +395,19 @@ void ControlPanel::chooseLogFile()
m_logfile_edit->setText(logFile);
}
}

void ControlPanel::changeVisibilityOfRTSandDTRCheckboxes(int index)
{
// if index is valid then retrieve the flow control type and change visibility of RTS and DTR lines. RTS and CTS
// checkboxes are invisible only if hardware control is enabled.
if (-1 != index) {
auto data = static_cast<QSerialPort::FlowControl>(m_combo_flowControl->itemData(index).toInt());
if (QSerialPort::FlowControl::HardwareControl == data) {
m_rts_line->setVisible(false);
m_dtr_line->setVisible(false);
} else {
m_rts_line->setVisible(true);
m_dtr_line->setVisible(true);
}
}
}
9 changes: 9 additions & 0 deletions controlpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ class ControlPanel : public QFrame, public Ui::ControlPanel
void customBaudRateSet();
void chooseLogFile();

/**
* @brief Designed to catch QComboBox::currentIndexChanged(int) signal. Its roles is to set the
* proper visibility of RTS and DTR checkboxes which depends on selected type of flow control.
* If user selects the hardware flow control, then RTS and DTR checkboxes are being invisible.
* If user selects none or software flow control, then RTS and DTR checkboxes are being visible.
* @param index QComboBox item's index.
*/
void changeVisibilityOfRTSandDTRCheckboxes(int index);

private:
int m_x;
int m_y;
Expand Down
28 changes: 27 additions & 1 deletion controlpanel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>628</width>
<height>129</height>
<height>130</height>
</rect>
</property>
<property name="autoFillBackground">
Expand Down Expand Up @@ -242,6 +242,32 @@
<item>
<widget class="DeviceCombo" name="m_combo_device" native="true"/>
</item>
<item>
<widget class="QCheckBox" name="m_rts_line">
<property name="toolTip">
<string>The RTS line is set to logic HIGH when checked. Note that in RS232C the negative logic is used.</string>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string>RTS</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="m_dtr_line">
<property name="toolTip">
<string>The DTR line is set to logic HIGH when checked. Note that in RS232C the negative logic is used.</string>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string>DTR</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down
38 changes: 34 additions & 4 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ MainWindow::MainWindow(QWidget *parent, const QString &session)
connect(m_sessionManager, &SessionManager::sessionRenamed, m_settings, &Settings::renameSession);
connect(m_sessionManager, &SessionManager::sessionCloned, m_settings, &Settings::cloneSession);
connect(actionManager, &QAction::triggered, m_sessionManager, &QDialog::show);

connect(controlPanel->m_rts_line, &QCheckBox::stateChanged, this, &MainWindow::setRTSLineState);
connect(controlPanel->m_dtr_line, &QCheckBox::stateChanged, this, &MainWindow::setDTRLineState);
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
Expand Down Expand Up @@ -322,10 +325,15 @@ void MainWindow::openDevice()
m_device->setStopBits(session.stopBits);
m_device->setFlowControl(session.flowControl);

/* Disable RTS/DTR when no flow control is used */
if (session.flowControl == QSerialPort::NoFlowControl) {
m_device->setDataTerminalReady(false);
m_device->setRequestToSend(false);
/* Disable RTS/DTR when no flow control or software flow control is used */
if (QSerialPort::FlowControl::HardwareControl != session.flowControl) {
// Force to emit QCheckBox::stateChanged signals to set proper logic levels on DTR/RTS lines.
// Note that on Linux when opening serial device even with no flow control, DTR and RTS lines are set to
// logic high, in RS232 that means a negative voltage on these lines (or just 0V for TTL based USB-UARTs).
// So this applies the same settings after reopen the device, and sets RTS/DTR to logic low when opening for
// first time when no flow control or software flow control is set.
emit controlPanel->m_dtr_line->stateChanged(controlPanel->m_dtr_line->checkState());
emit controlPanel->m_rts_line->stateChanged(controlPanel->m_rts_line->checkState());
}

m_device->flush();
Expand Down Expand Up @@ -922,6 +930,28 @@ void MainWindow::removeSelectedInputItems(bool checked)
}
}

void MainWindow::setRTSLineState(int checked)
{
if ((nullptr != m_device) && (true == m_device->isOpen())) {
if (Qt::CheckState::Checked == static_cast<Qt::CheckState>(checked)) {
m_device->setRequestToSend(true);
} else {
m_device->setRequestToSend(false);
}
}
}

void MainWindow::setDTRLineState(int checked)
{
if ((nullptr != m_device) && (true == m_device->isOpen())) {
if (Qt::CheckState::Checked == static_cast<Qt::CheckState>(checked)) {
m_device->setDataTerminalReady(true);
} else {
m_device->setDataTerminalReady(false);
}
}
}

MainWindow::~MainWindow()
{
if (m_device->isOpen()) {
Expand Down
11 changes: 11 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ private slots:
void sendDone(int exitCode, QProcess::ExitStatus exitStatus);
void resizeEvent(QResizeEvent *event);

protected slots:
/**
* @brief Handles QCheckBox::stateChanged signal received from RTS checkbox (placed in control panel).
*/
void setRTSLineState(int checked);

/**
* @brief Handles QCheckBox::stateChanged signal recieved from DTR checkbox (placed in control panel).
*/
void setDTRLineState(int checked);

private:
void toggleLogging(bool start);
void fillLineTerminationChooser(const Settings::LineTerminator setting = Settings::LF);
Expand Down

0 comments on commit 7da86b3

Please sign in to comment.