| 1 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "mainwindow.h" |
| 5 |
#include "modbustcpclient.h" |
| 6 |
|
| 7 |
#include <QLoggingCategory> |
| 8 |
#include <QModbusPdu> |
| 9 |
#include <QModbusRtuSerialClient> |
| 10 |
#include <QSerialPortInfo> |
| 11 |
|
| 12 |
#ifndef QT_STATIC |
| 13 |
QT_BEGIN_NAMESPACE |
| 14 |
Q_LOGGING_CATEGORY(QT_MODBUS, "qt.modbus") |
| 15 |
Q_LOGGING_CATEGORY(QT_MODBUS_LOW, "qt.modbus.lowlevel") |
| 16 |
QT_END_NAMESPACE |
| 17 |
#endif |
| 18 |
|
| 19 |
QT_USE_NAMESPACE |
| 20 |
|
| 21 |
MainWindow *s_instance = nullptr; |
| 22 |
|
| 23 |
static void HandlerFunction(QtMsgType, const QMessageLogContext &, const QString &msg) |
| 24 |
{ |
| 25 |
if (auto instance = MainWindow::instance()) |
| 26 |
instance->appendToLog(msg); |
| 27 |
} |
| 28 |
|
| 29 |
MainWindow::MainWindow(QWidget *parent) |
| 30 |
: QMainWindow(parent) |
| 31 |
, m_debugHandler(HandlerFunction) |
| 32 |
{ |
| 33 |
setupUi(this); |
| 34 |
s_instance = this; |
| 35 |
|
| 36 |
const auto ports = QSerialPortInfo::availablePorts(); |
| 37 |
for (const QSerialPortInfo &info : ports) |
| 38 |
serialPortCombo->addItem(info.portName(), false); |
| 39 |
serialPortCombo->insertSeparator(serialPortCombo->count()); |
| 40 |
serialPortCombo->addItem(tr("Add port..."), true); |
| 41 |
serialPortCombo->setInsertPolicy(QComboBox::InsertAtTop); |
| 42 |
|
| 43 |
connect(tcpRadio, &QRadioButton::toggled, this, [this](bool toggled) { |
| 44 |
stackedWidget->setCurrentIndex(toggled); |
| 45 |
}); |
| 46 |
connect(actionExit, &QAction::triggered, this, &QMainWindow::close); |
| 47 |
|
| 48 |
QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true")); |
| 49 |
} |
| 50 |
|
| 51 |
MainWindow::~MainWindow() |
| 52 |
{ |
| 53 |
disconnectAndDelete(); |
| 54 |
s_instance = nullptr; |
| 55 |
} |
| 56 |
|
| 57 |
MainWindow *MainWindow::instance() |
| 58 |
{ |
| 59 |
return s_instance; |
| 60 |
} |
| 61 |
|
| 62 |
void MainWindow::on_sendButton_clicked() |
| 63 |
{ |
| 64 |
const bool isSerial = serialRadio->isChecked(); |
| 65 |
const bool isCustom = (isSerial ? fcSerialDrop : fcTcpDrop)->currentIndex() == 0; |
| 66 |
const QByteArray pduData = QByteArray::fromHex((isSerial ? pduSerialLine : pduTcpLine)->text() |
| 67 |
.toLatin1()); |
| 68 |
|
| 69 |
QModbusReply *reply = nullptr; |
| 70 |
if (isCustom && pduData.isEmpty()) { |
| 71 |
qDebug() << "Error: Cannot send custom PDU without any data."; |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
const quint8 address = quint8((isSerial ? addressSpin : ui1Spin)->value()); |
| 76 |
if (isCustom) { |
| 77 |
qDebug() << "Send: Sending custom PDU."; |
| 78 |
reply = m_device->sendRawRequest(QModbusRequest(QModbusRequest::FunctionCode( |
| 79 |
pduData[0]), pduData.mid(1)), address); |
| 80 |
} else { |
| 81 |
qDebug() << "Send: Sending PDU with predefined function code."; |
| 82 |
quint16 fc = (isSerial ? fcSerialDrop : fcTcpDrop)->currentText().left(4).toUShort(0, 16); |
| 83 |
reply = m_device->sendRawRequest(QModbusRequest(QModbusRequest::FunctionCode(fc), pduData), |
| 84 |
address); |
| 85 |
} |
| 86 |
|
| 87 |
if (reply) { |
| 88 |
sendButton->setDisabled(true); |
| 89 |
if (!reply->isFinished()) { |
| 90 |
connect(reply, &QModbusReply::finished, this, [reply, this]() { |
| 91 |
sendButton->setEnabled(true); |
| 92 |
qDebug() << "Receive: Asynchronous response PDU: " << reply->rawResult() << Qt::endl; |
| 93 |
}); |
| 94 |
} else { |
| 95 |
sendButton->setEnabled(true); |
| 96 |
qDebug() << "Receive: Synchronous response pdu: " << reply->rawResult() << Qt::endl; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void MainWindow::on_connectButton_clicked() |
| 102 |
{ |
| 103 |
if (tcpRadio->isChecked()) { |
| 104 |
auto device = new ModbusTcpClient; |
| 105 |
connect(ti1Spin, &QSpinBox::valueChanged, |
| 106 |
device, &ModbusTcpClient::valueChanged); |
| 107 |
connect(ti2Spin, &QSpinBox::valueChanged, |
| 108 |
device, &ModbusTcpClient::valueChanged); |
| 109 |
|
| 110 |
connect(pi1Spin, &QSpinBox::valueChanged, |
| 111 |
device, &ModbusTcpClient::valueChanged); |
| 112 |
connect(pi2Spin, &QSpinBox::valueChanged, |
| 113 |
device, &ModbusTcpClient::valueChanged); |
| 114 |
|
| 115 |
connect(l1Spin, &QSpinBox::valueChanged, |
| 116 |
device, &ModbusTcpClient::valueChanged); |
| 117 |
connect(l2Spin, &QSpinBox::valueChanged, |
| 118 |
device, &ModbusTcpClient::valueChanged); |
| 119 |
|
| 120 |
connect(ui1Spin, &QSpinBox::valueChanged, |
| 121 |
device, &ModbusTcpClient::valueChanged); |
| 122 |
|
| 123 |
m_device = device; |
| 124 |
device->valueChanged(0); // trigger update |
| 125 |
m_device->setConnectionParameter(QModbusDevice::NetworkAddressParameter, |
| 126 |
tcpAddressEdit->text()); |
| 127 |
m_device->setConnectionParameter(QModbusDevice::NetworkPortParameter, |
| 128 |
tcpPortEdit->text()); |
| 129 |
} else { |
| 130 |
m_device = new QModbusRtuSerialClient; |
| 131 |
m_device->setConnectionParameter(QModbusDevice::SerialPortNameParameter, |
| 132 |
serialPortCombo->currentText()); |
| 133 |
|
| 134 |
int parity = parityCombo->currentIndex(); |
| 135 |
if (parity > 0) |
| 136 |
parity++; |
| 137 |
m_device->setConnectionParameter(QModbusDevice::SerialParityParameter, parity); |
| 138 |
m_device->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, |
| 139 |
dataBitsCombo->currentText().toInt()); |
| 140 |
m_device->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, |
| 141 |
stopBitsCombo->currentText().toInt()); |
| 142 |
m_device->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, |
| 143 |
baudRateCombo->currentText().toInt()); |
| 144 |
} |
| 145 |
m_device->setTimeout(timeoutSpin->value()); |
| 146 |
m_device->setNumberOfRetries(retriesSpin->value()); |
| 147 |
|
| 148 |
connect(m_device, &QModbusDevice::errorOccurred, this, [this](QModbusDevice::Error) { |
| 149 |
qDebug().noquote() << QStringLiteral("Error: %1").arg(m_device->errorString()); |
| 150 |
emit disconnectButton->clicked(); |
| 151 |
}, Qt::QueuedConnection); |
| 152 |
|
| 153 |
connect(m_device, &QModbusDevice::stateChanged, this, [](QModbusDevice::State state) { |
| 154 |
switch (state) { |
| 155 |
case QModbusDevice::UnconnectedState: |
| 156 |
qDebug().noquote() << QStringLiteral("State: Entered unconnected state."); |
| 157 |
break; |
| 158 |
case QModbusDevice::ConnectingState: |
| 159 |
qDebug().noquote() << QStringLiteral("State: Entered connecting state."); |
| 160 |
break; |
| 161 |
case QModbusDevice::ConnectedState: |
| 162 |
qDebug().noquote() << QStringLiteral("State: Entered connected state."); |
| 163 |
break; |
| 164 |
case QModbusDevice::ClosingState: |
| 165 |
qDebug().noquote() << QStringLiteral("State: Entered closing state."); |
| 166 |
break; |
| 167 |
} |
| 168 |
}); |
| 169 |
m_device->connectDevice(); |
| 170 |
} |
| 171 |
|
| 172 |
void MainWindow::on_disconnectButton_clicked() |
| 173 |
{ |
| 174 |
disconnectAndDelete(); |
| 175 |
} |
| 176 |
|
| 177 |
void MainWindow::on_serialPortCombo_currentIndexChanged(int index) |
| 178 |
{ |
| 179 |
const bool custom = serialPortCombo->itemData(index, Qt::UserRole).toBool(); |
| 180 |
serialPortCombo->setEditable(custom); |
| 181 |
if (custom) { |
| 182 |
serialPortCombo->clearEditText(); |
| 183 |
serialPortCombo->lineEdit()->setPlaceholderText(QStringLiteral("Type here...")); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void MainWindow::disconnectAndDelete() |
| 188 |
{ |
| 189 |
if (!m_device) |
| 190 |
return; |
| 191 |
m_device->disconnectDevice(); |
| 192 |
m_device->disconnect(); |
| 193 |
delete m_device; |
| 194 |
m_device = nullptr; |
| 195 |
} |