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 d7a0efc + c2d4137 commit 9b92fa6
Show file tree
Hide file tree
Showing 41 changed files with 886 additions and 914 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ Registers

Modbus Logging

![image](https://github.com/sanny32/OpenModSim/assets/13627951/1ebf4973-44a5-4464-aada-26aa751f65da)
![image](https://github.com/sanny32/OpenModSim/assets/13627951/23ae888d-4722-4065-a394-64e28f68ea62)


## Extended Featues

Modbus Message Parser

![image](https://github.com/sanny32/OpenModScan/assets/13627951/86a82340-015e-4ee9-a483-b5ab83527cc1)
![image](https://github.com/sanny32/OpenModSim/assets/13627951/7e9744b8-f4b3-439a-a312-79cbdc426dc2)


## Scripting
From version 1.2.0 Open ModSim supports scripting. Qt runtime implements the [ECMAScript Language Specification](http://www.ecma-international.org/publications/standards/Ecma-262.htm) standard, so Javascript is used to write code.

![image](https://github.com/sanny32/OpenModSim/assets/13627951/c03a7d23-3c69-43aa-a1d3-ae5096da4f54)
![image](https://github.com/sanny32/OpenModSim/assets/13627951/c03a7d23-3c69-43aa-a1d3-ae5096da4f54)

Scripts can be launched in two modes: Once or Periodically. If you run script in Once mode the script will stop after it finishes executing. In Periodically mode, the script will start after a certain period of time until the user stops it or the method is called
```javascript
Expand Down
23 changes: 21 additions & 2 deletions omodsim/controls/modbuslogwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <QEvent>
#include "htmldelegate.h"
#include "modbuslogwidget.h"

Expand Down Expand Up @@ -131,6 +132,19 @@ ModbusLogWidget::ModbusLogWidget(QWidget* parent)
});
}

///
/// \brief ModbusLogWidget::changeEvent
/// \param event
///
void ModbusLogWidget::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange)
{
update();
}
QListView::changeEvent(event);
}

///
/// \brief ModbusLogWidget::clear
///
Expand Down Expand Up @@ -162,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
5 changes: 4 additions & 1 deletion omodsim/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 All @@ -65,6 +65,9 @@ class ModbusLogWidget : public QListView
bool autoscroll() const;
void setAutoscroll(bool on);

protected:
void changeEvent(QEvent* event) override;

private:
bool _autoscroll;
DataDisplayMode _dataDisplayMode;
Expand Down
22 changes: 12 additions & 10 deletions omodsim/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
19 changes: 13 additions & 6 deletions omodsim/controls/outputwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,20 +583,24 @@ void OutputWidget::paint(const QRect& rc, QPainter& painter)
/// \brief OutputWidget::updateTraffic
/// \param request
/// \param server
/// \param transactionId
/// \param protocol
///
void OutputWidget::updateTraffic(const QModbusRequest& request, int server)
void OutputWidget::updateTraffic(const QModbusRequest& request, int server, int transactionId, ModbusMessage::ProtocolType protocol)
{
updateLogView(true, server, request);
updateLogView(true, server, transactionId, protocol, request);
}

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

///
Expand Down Expand Up @@ -827,11 +831,14 @@ void OutputWidget::showModbusMessage(const QModelIndex& index)
///
/// \brief OutputWidget::updateLogView
/// \param request
/// \param server
/// \param transactionId
/// \param protocol
/// \param pdu
///
void OutputWidget::updateLogView(bool request, int server, const QModbusPdu& pdu)
void OutputWidget::updateLogView(bool request, int server, int transactionId, ModbusMessage::ProtocolType protocol, 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
7 changes: 4 additions & 3 deletions omodsim/controls/outputwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QListWidgetItem>
#include <QModbusReply>
#include "enums.h"
#include "modbusmessage.h"
#include "datasimulator.h"
#include "displaydefinition.h"

Expand Down Expand Up @@ -113,8 +114,8 @@ class OutputWidget : public QWidget

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, ModbusMessage::ProtocolType protocol);
void updateTraffic(const QModbusResponse& response, int server, int transactionId, ModbusMessage::ProtocolType protocol);
void updateData(const QModbusDataUnit& data);

AddressDescriptionMap descriptionMap() const;
Expand All @@ -135,7 +136,7 @@ private slots:
private:
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, ModbusMessage::ProtocolType protocol, const QModbusPdu& pdu);

private:
Ui::OutputWidget *ui;
Expand Down
4 changes: 2 additions & 2 deletions omodsim/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
35 changes: 8 additions & 27 deletions omodsim/dialogs/dialogmsgparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
///
/// \brief DialogMsgParser::DialogMsgParser
/// \param mode
/// \param protocol
/// \param parent
///
DialogMsgParser::DialogMsgParser(DataDisplayMode mode, QWidget *parent)
DialogMsgParser::DialogMsgParser(DataDisplayMode mode, ModbusMessage::ProtocolType protocol, QWidget *parent)
: QDialog(parent)
, ui(new Ui::DialogMsgParser)
,_mm(nullptr)
Expand All @@ -21,6 +22,8 @@ DialogMsgParser::DialogMsgParser(DataDisplayMode mode, QWidget *parent)

ui->info->setShowTimestamp(false);
ui->hexView->setCheckState(mode == DataDisplayMode::Hex ? Qt::Checked : Qt::Unchecked);
ui->buttonRtu->setChecked(protocol == ModbusMessage::Rtu);
ui->buttonTcp->setChecked(protocol == ModbusMessage::Tcp);

auto dispatcher = QAbstractEventDispatcher::instance();
connect(dispatcher, &QAbstractEventDispatcher::awake, this, &DialogMsgParser::on_awake);
Expand Down Expand Up @@ -54,7 +57,6 @@ void DialogMsgParser::changeEvent(QEvent* event)
///
void DialogMsgParser::on_awake()
{
ui->deviceIdIncluded->setVisible(ui->buttonPdu->isChecked());
ui->pushButtonParse->setEnabled(!ui->bytesData->isEmpty());
}

Expand All @@ -81,10 +83,6 @@ void DialogMsgParser::on_bytesData_valueChanged(const QByteArray& value)

return;
}

const auto data = value.left(value.size() - 2);
const int checksum = makeWord(value[value.size() - 1], value[value.size() - 2], ByteOrder::LittleEndian);
ui->checksumIncluded->setChecked(QModbusAdu::calculateCRC(data, data.size()) == checksum);
}

///
Expand All @@ -95,28 +93,11 @@ void DialogMsgParser::on_pushButtonParse_clicked()
auto data = ui->bytesData->value();
if(data.isEmpty()) return;

ModbusMessage::Type type = ModbusMessage::Adu;
auto protocol = QModbusAdu::Tcp;
int checksum = 0;

if(ui->buttonPdu->isChecked())
{
type = ModbusMessage::Pdu;
}

if(!ui->deviceIdIncluded->isChecked())
{
data.push_front('\0');
}

if(ui->checksumIncluded->isChecked())
{
protocol = QModbusAdu::Rtu;
checksum = makeWord(data[data.size() - 1], data[data.size() - 2], ByteOrder::LittleEndian);
data = data.left(data.size() - 2);
}
auto protocol = ModbusMessage::Rtu;
if(ui->buttonTcp->isChecked())
protocol = ModbusMessage::Tcp;

if(_mm) delete _mm;
_mm = ModbusMessage::parse(data, type, protocol, ui->request->isChecked(), checksum);
_mm = ModbusMessage::create(data, protocol, QDateTime::currentDateTime(), ui->request->isChecked());
ui->info->setModbusMessage(_mm);
}
2 changes: 1 addition & 1 deletion omodsim/dialogs/dialogmsgparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DialogMsgParser : public QDialog
Q_OBJECT

public:
explicit DialogMsgParser(DataDisplayMode mode, QWidget *parent = nullptr);
explicit DialogMsgParser(DataDisplayMode mode, ModbusMessage::ProtocolType protocol, QWidget *parent = nullptr);
~DialogMsgParser();

protected:
Expand Down
25 changes: 4 additions & 21 deletions omodsim/dialogs/dialogmsgparser.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="buttonPdu">
<widget class="QPushButton" name="buttonRtu">
<property name="styleSheet">
<string notr="true">padding: 4px;</string>
</property>
<property name="text">
<string>PDU Message</string>
<string>RTU Message</string>
</property>
<property name="checkable">
<bool>true</bool>
Expand Down Expand Up @@ -56,12 +56,12 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonAdu">
<widget class="QPushButton" name="buttonTcp">
<property name="styleSheet">
<string notr="true">padding: 4px;</string>
</property>
<property name="text">
<string>ADU Message</string>
<string>TCP Message</string>
</property>
<property name="checkable">
<bool>true</bool>
Expand Down Expand Up @@ -124,23 +124,6 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="deviceIdIncluded">
<property name="text">
<string>Device Id included</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checksumIncluded">
<property name="text">
<string>Checksum included</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down
Loading

0 comments on commit 9b92fa6

Please sign in to comment.