Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanny32 committed Nov 3, 2023
2 parents 09f57c4 + 1a78943 commit 3ddff51
Show file tree
Hide file tree
Showing 55 changed files with 1,813 additions and 1,562 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Registers

Modbus Logging

![image](https://github.com/sanny32/OpenModScan/assets/13627951/de14b977-08ba-460c-814c-7affd0d88f91)
![image](https://github.com/sanny32/OpenModScan/assets/13627951/b3e43b25-ea7b-4beb-96c9-c7b9d6a65a03)


## Extended Featues
Expand All @@ -38,15 +38,15 @@ Modbus Logging

- Modbus Scanner (supports both Modbus RTU and Modbus TCP scanning)

![image](https://github.com/sanny32/OpenModScan/assets/13627951/768a9c69-0201-4c1e-bad5-4fa9b24d9553)
![image](https://github.com/sanny32/OpenModScan/assets/13627951/cd35c0c4-ca9c-41fe-872c-1c2bb2286fc7)

- Modbus Message Parser

![image](https://github.com/sanny32/OpenModScan/assets/13627951/86a82340-015e-4ee9-a483-b5ab83527cc1)
![image](https://github.com/sanny32/OpenModScan/assets/13627951/4f05f38e-d739-4c49-8bc3-f12e7b74d8ab)

- Modbus User Message

![image](https://github.com/sanny32/OpenModScan/assets/13627951/5ee959a2-9c5c-4ffe-8056-d6205e3b5aa3)
![image](https://github.com/sanny32/OpenModScan/assets/13627951/1aba6329-873c-4ff2-8db8-939245a50722)

## Building
Now building is available with Qt/qmake (version 5.15 and above) or Qt Creator. Supports both OS Microsoft Windows and Linux.
Expand Down
34 changes: 30 additions & 4 deletions omodscan/controls/bytelisttextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
#include "formatutils.h"
#include "bytelisttextedit.h"

///
/// \brief splitBySep
/// \param str
/// \param sep
/// \return
///
QString splitBySep(const QString& str, QChar sep)
{
QString s;
for(int i = 0; i < str.length() - 1; i+=2)
{
s.append(str[i]);
s.append(str[i+1]);
if(i + 3 < str.length()) s.append(sep);
}
return s;
}

///
/// \brief ByteListTextEdit::ByteListTextEdit
/// \param parent
Expand Down Expand Up @@ -182,8 +200,11 @@ void ByteListTextEdit::keyPressEvent(QKeyEvent *e)
///
bool ByteListTextEdit::canInsertFromMimeData(const QMimeData* source) const
{
if (!source->hasText())
return false;

int pos = 0;
auto text = source->text().trimmed();
auto text = source->text().remove("0x").trimmed();
const auto state = _validator->validate(text, pos);

return state == QValidator::Acceptable;
Expand All @@ -195,13 +216,18 @@ bool ByteListTextEdit::canInsertFromMimeData(const QMimeData* source) const
///
void ByteListTextEdit::insertFromMimeData(const QMimeData* source)
{
if (!source->hasText())
return;

int pos = 0;
auto text = source->text().trimmed();
const auto state = _validator->validate(text, pos);
auto text = source->text().remove("0x").trimmed();
if(text.length() > 2 && !text.contains(_separator))
text = splitBySep(text, _separator);

const auto state = _validator->validate(text, pos);
if(state == QValidator::Acceptable)
{
QPlainTextEdit::insertFromMimeData(source);
textCursor().insertText(text);
updateValue();
}
}
Expand Down
9 changes: 7 additions & 2 deletions omodscan/controls/modbuslogwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,22 @@ QModelIndex ModbusLogWidget::index(int row)
///
/// \brief ModbusLogWidget::addItem
/// \param pdu
/// \param protocol
/// \param deviceId
/// \param transactionId
/// \param timestamp
/// \param request
/// \return
///
const ModbusMessage* ModbusLogWidget::addItem(const QModbusPdu& pdu, int deviceId, const QDateTime& timestamp, bool request)
const ModbusMessage* ModbusLogWidget::addItem(const QModbusPdu& pdu, ModbusMessage::ProtocolType protocol, int deviceId, int transactionId, const QDateTime& timestamp, bool request)
{
const ModbusMessage* msg = nullptr;
if(model())
{
msg = ModbusMessage::create(pdu, (QModbusAdu::Type)-1, deviceId, timestamp, request);
msg = ModbusMessage::create(pdu, protocol, deviceId, timestamp, request);
if(protocol == ModbusMessage::Tcp)
((QModbusAduTcp*)msg->adu())->setTransactionId(transactionId);

((ModbusLogModel*)model())->append(msg);
}
return msg;
Expand Down
2 changes: 1 addition & 1 deletion omodscan/controls/modbuslogwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ModbusLogWidget : public QListView
int rowCount() const;
QModelIndex index(int row);

const ModbusMessage* addItem(const QModbusPdu& pdu, int deviceId, const QDateTime& timestamp, bool request);
const ModbusMessage* addItem(const QModbusPdu& pdu, ModbusMessage::ProtocolType protocol, int deviceId, int transactionId, const QDateTime& timestamp, bool request);
const ModbusMessage* itemAt(const QModelIndex& index);

DataDisplayMode dataDisplayMode() const;
Expand Down
22 changes: 12 additions & 10 deletions omodscan/controls/modbusmessagewidget.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <QEvent>
#include "formatutils.h"
#include "htmldelegate.h"
#include "modbusmessagewidget.h"
#include "modbusmessages.h"
#include "modbusmessagewidget.h"

///
/// \brief ModbusMessageWidget::ModbusMessageWidget
Expand Down Expand Up @@ -147,16 +147,17 @@ void ModbusMessageWidget::update()
}

auto addChecksum = [&]{
if(_mm->protocolType() == QModbusAdu::Rtu)
if(_mm->protocolType() == ModbusMessage::Rtu)
{
const auto checksum = formatWordValue(_dataDisplayMode, _mm->checksum());
if(_mm->matchingChecksum())
auto adu = reinterpret_cast<const QModbusAduRtu*>(_mm->adu());
const auto checksum = formatWordValue(_dataDisplayMode, adu->checksum());
if(adu->matchingChecksum())
{
addItem(tr("<b>Checksum:</b> %1").arg(checksum));
}
else
{
const auto calcChecksum = formatWordValue(_dataDisplayMode, _mm->calcChecksum());
const auto calcChecksum = formatWordValue(_dataDisplayMode, adu->calcChecksum());
addItem(tr("<b>Checksum:</b> <span style='color:%3'>%1</span> (Expected: %2)").arg(checksum, calcChecksum, _statusClr.name()));
}
}
Expand All @@ -165,11 +166,12 @@ void ModbusMessageWidget::update()
addItem(tr("<b>Type:</b> %1").arg(_mm->isRequest() ? tr("Request (Tx)") : tr("Response (Rx)")));
if(_showTimestamp) addItem(tr("<b>Timestamp:</b> %1").arg(_mm->timestamp().toString(Qt::ISODateWithMs)));

if(_mm->type() == ModbusMessage::Adu)
if(_mm->protocolType() == ModbusMessage::Tcp)
{
const auto transactionId = _mm->isValid() ? formatWordValue(_dataDisplayMode, _mm->transactionId()) : "??";
const auto protocolId = _mm->isValid() ? formatWordValue(_dataDisplayMode, _mm->protocolId()): "??";
const auto length = _mm->isValid() ? formatWordValue(_dataDisplayMode, _mm->length()): "??";
auto adu = reinterpret_cast<const QModbusAduTcp*>(_mm->adu());
const auto transactionId = adu->isValid() ? formatWordValue(_dataDisplayMode, adu->transactionId()) : "??";
const auto protocolId = adu->isValid() ? formatWordValue(_dataDisplayMode, adu->protocolId()): "??";
const auto length = adu->isValid() ? formatWordValue(_dataDisplayMode, adu->length()): "??";
addItem(tr("<b>Transaction ID:</b> %1").arg(transactionId));
addItem(tr("<b>Protocol ID:</b> %1").arg(protocolId));
addItem(tr("<b>Length:</b> %1").arg(length));
Expand Down Expand Up @@ -197,7 +199,7 @@ void ModbusMessageWidget::update()
case QModbusPdu::ReadCoils:
if(_mm->isRequest())
{
auto req = reinterpret_cast<const ReadCoilsRequest*>(_mm);
auto req = reinterpret_cast<const ReadCoilsRequest*>(_mm);
const auto startAddress = req->isValid() ? formatWordValue(_dataDisplayMode, req->startAddress()) : "??";
const auto length = req->isValid() ? formatWordValue(_dataDisplayMode, req->length()): "??";
addItem(tr("<b>Start Address:</b> %1").arg(startAddress));
Expand Down
49 changes: 41 additions & 8 deletions omodscan/controls/outputwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,15 @@ QVector<quint16> OutputWidget::data() const
///
/// \brief OutputWidget::setup
/// \param dd
/// \param protocol
/// \param simulations
///
void OutputWidget::setup(const DisplayDefinition& dd, const ModbusSimulationMap& simulations)
void OutputWidget::setup(const DisplayDefinition& dd, ModbusMessage::ProtocolType protocol, const ModbusSimulationMap& simulations)
{
_descriptionMap.insert(descriptionMap());
_displayDefinition = dd;

setProtocol(protocol);
setLogViewLimit(dd.LogViewLimit);

_listModel->clear();
Expand All @@ -369,6 +371,7 @@ void OutputWidget::setup(const DisplayDefinition& dd, const ModbusSimulationMap&
setDescription(key.first, key.second, _descriptionMap[key]);

_listModel->update();

}

///
Expand Down Expand Up @@ -519,6 +522,14 @@ void OutputWidget::setLogViewLimit(int l)
ui->logView->setRowLimit(l);
}

///
/// \brief OutputWidget::clearLogView
///
void OutputWidget::clearLogView()
{
ui->logView->clear();
}

///
/// \brief OutputWidget::setStatus
/// \param status
Expand Down Expand Up @@ -582,21 +593,23 @@ void OutputWidget::paint(const QRect& rc, QPainter& painter)
///
/// \brief OutputWidget::updateTraffic
/// \param request
/// \param server
/// \param deviceId
/// \param transactionId
///
void OutputWidget::updateTraffic(const QModbusRequest& request, int server)
void OutputWidget::updateTraffic(const QModbusRequest& request, int deviceId, int transactionId)
{
updateLogView(true, server, request);
updateLogView(true, deviceId, transactionId, request);
}

///
/// \brief OutputWidget::updateTraffic
/// \param response
/// \param deviceId
/// \param transactionId
///
void OutputWidget::updateTraffic(const QModbusResponse& response, int deviceId)
void OutputWidget::updateTraffic(const QModbusResponse& response, int deviceId, int transactionId)
{
updateLogView(false, deviceId, response);
updateLogView(false, deviceId, transactionId, response);
}

///
Expand Down Expand Up @@ -695,6 +708,24 @@ void OutputWidget::setDataDisplayMode(DataDisplayMode mode)
_listModel->update();
}

///
/// \brief OutputWidget::protocol
/// \return
///
ModbusMessage::ProtocolType OutputWidget::protocol() const
{
return _protocol;
}

///
/// \brief OutputWidget::setProtocol
/// \param type
///
void OutputWidget::setProtocol(ModbusMessage::ProtocolType type)
{
_protocol = type;
}

///
/// \brief OutputWidget::byteOrder
/// \return
Expand Down Expand Up @@ -817,11 +848,13 @@ void OutputWidget::showModbusMessage(const QModelIndex& index)
///
/// \brief OutputWidget::updateLogView
/// \param request
/// \param server
/// \param transactionId
/// \param pdu
///
void OutputWidget::updateLogView(bool request, int server, const QModbusPdu& pdu)
void OutputWidget::updateLogView(bool request, int server, int transactionId, const QModbusPdu& pdu)
{
auto msg = ui->logView->addItem(pdu, server, QDateTime::currentDateTime(), request);
auto msg = ui->logView->addItem(pdu, _protocol, server, transactionId, QDateTime::currentDateTime(), request);
if(captureMode() == CaptureMode::TextCapture && msg != nullptr)
{
const auto str = QString("%1: %2 %3 %4").arg(
Expand Down
14 changes: 10 additions & 4 deletions omodscan/controls/outputwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ class OutputWidget : public QWidget

QVector<quint16> data() const;

void setup(const DisplayDefinition& dd, const ModbusSimulationMap& simulations);
void setup(const DisplayDefinition& dd, ModbusMessage::ProtocolType protocol, const ModbusSimulationMap& simulations);

DisplayMode displayMode() const;
void setDisplayMode(DisplayMode mode);

DataDisplayMode dataDisplayMode() const;
void setDataDisplayMode(DataDisplayMode mode);

ModbusMessage::ProtocolType protocol() const;
void setProtocol(ModbusMessage::ProtocolType type);

ByteOrder byteOrder() const;
void setByteOrder(ByteOrder order);

Expand All @@ -108,12 +111,14 @@ class OutputWidget : public QWidget
int logViewLimit() const;
void setLogViewLimit(int l);

void clearLogView();

void setStatus(const QString& status);

void paint(const QRect& rc, QPainter& painter);

void updateTraffic(const QModbusRequest& request, int server);
void updateTraffic(const QModbusResponse& response, int server);
void updateTraffic(const QModbusRequest& request, int server, int transactionId);
void updateTraffic(const QModbusResponse& response, int server, int transactionId);
void updateData(const QModbusDataUnit& data);

AddressDescriptionMap descriptionMap() const;
Expand All @@ -135,7 +140,7 @@ private slots:
void setUninitializedStatus();
void captureString(const QString& s);
void showModbusMessage(const QModelIndex& index);
void updateLogView(bool request, int deviceId, const QModbusPdu& pdu);
void updateLogView(bool request, int deviceId, int transactionId, const QModbusPdu& pdu);

private:
Ui::OutputWidget *ui;
Expand All @@ -144,6 +149,7 @@ private slots:
bool _displayHexAddresses;
DisplayMode _displayMode;
DataDisplayMode _dataDisplayMode;
ModbusMessage::ProtocolType _protocol;
ByteOrder _byteOrder;
DisplayDefinition _displayDefinition;
QFile _fileCapture;
Expand Down
4 changes: 2 additions & 2 deletions omodscan/controls/outputwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>2</verstretch>
<verstretch>3</verstretch>
</sizepolicy>
</property>
<property name="autoScroll">
Expand Down Expand Up @@ -170,7 +170,7 @@
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
<verstretch>2</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
Expand Down
Loading

0 comments on commit 3ddff51

Please sign in to comment.