Browse Source

code read and first step of code write

master
nasi 4 years ago
parent
commit
e8347a3aaf
  1. 51
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/Makefile
  2. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/main.o
  3. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/mainwindow.o
  4. 56
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_mainwindow.cpp
  5. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_mainwindow.o
  6. 53
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_modbusmaster.cpp
  7. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_modbusmaster.o
  8. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/modbusmaster.o
  9. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/serialConnectionManager
  10. 349
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/ui_mainwindow.h
  11. 70
      serialConnectionManager/mainwindow.cpp
  12. 12
      serialConnectionManager/mainwindow.h
  13. 424
      serialConnectionManager/mainwindow.ui
  14. 108
      serialConnectionManager/modbusmaster.cpp
  15. 16
      serialConnectionManager/modbusmaster.h
  16. 2
      serialConnectionManager/serialportexception.h

51
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/Makefile

@ -770,6 +770,23 @@ moc_mainwindow.cpp: ../serialConnectionManager/mainwindow.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtGui/qtouchdevice.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtWidgets/qtabwidget.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtGui/qicon.h \
../serialConnectionManager/modbusmaster.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QObject \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/QModbusDataUnit \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusdataunit.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/QModbusRtuSerialMaster \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusrtuserialmaster.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusclient.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusdevice.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qtserialbusglobal.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qtserialbus-config.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbuspdu.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusreply.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QBitArray \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qbitarray.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/QSerialPort \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/qserialport.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/qserialportglobal.h \
moc_predefs.h \
../../../Qt5.13.1/5.13.1/gcc_64/bin/moc
/home/nheydari/Qt5.13.1/5.13.1/gcc_64/bin/moc $(DEFINES) --include /home/nheydari/Desktop/serialConnectionProj/build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_predefs.h -I/home/nheydari/Qt5.13.1/5.13.1/gcc_64/mkspecs/linux-g++ -I/home/nheydari/Desktop/serialConnectionProj/serialConnectionManager -I/home/nheydari/Qt5.13.1/5.13.1/gcc_64/include -I/home/nheydari/Qt5.13.1/5.13.1/gcc_64/include/QtWidgets -I/home/nheydari/Qt5.13.1/5.13.1/gcc_64/include/QtGui -I/home/nheydari/Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus -I/home/nheydari/Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort -I/home/nheydari/Qt5.13.1/5.13.1/gcc_64/include/QtCore -I. -I/usr/include/c++/9 -I/usr/include/x86_64-linux-gnu/c++/9 -I/usr/include/c++/9/backward -I/usr/lib/gcc/x86_64-linux-gnu/9/include -I/usr/local/include -I/usr/include/x86_64-linux-gnu -I/usr/include ../serialConnectionManager/mainwindow.h -o moc_mainwindow.cpp
@ -987,6 +1004,23 @@ main.o: ../serialConnectionManager/main.cpp ../serialConnectionManager/mainwindo
../../../Qt5.13.1/5.13.1/gcc_64/include/QtGui/qtouchdevice.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtWidgets/qtabwidget.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtGui/qicon.h \
../serialConnectionManager/modbusmaster.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QObject \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/QModbusDataUnit \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusdataunit.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/QModbusRtuSerialMaster \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusrtuserialmaster.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusclient.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusdevice.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qtserialbusglobal.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qtserialbus-config.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbuspdu.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusreply.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QBitArray \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qbitarray.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/QSerialPort \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/qserialport.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/qserialportglobal.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtWidgets/QApplication \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtWidgets/qapplication.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qcoreapplication.h \
@ -1103,6 +1137,23 @@ mainwindow.o: ../serialConnectionManager/mainwindow.cpp ../serialConnectionManag
../../../Qt5.13.1/5.13.1/gcc_64/include/QtGui/qtouchdevice.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtWidgets/qtabwidget.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtGui/qicon.h \
../serialConnectionManager/modbusmaster.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QObject \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/QModbusDataUnit \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusdataunit.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/QModbusRtuSerialMaster \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusrtuserialmaster.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusclient.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusdevice.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qtserialbusglobal.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qtserialbus-config.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbuspdu.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusreply.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QBitArray \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qbitarray.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/QSerialPort \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/qserialport.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialPort/qserialportglobal.h \
ui_mainwindow.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o mainwindow.o ../serialConnectionManager/mainwindow.cpp

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/main.o

Binary file not shown.

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/mainwindow.o

Binary file not shown.

56
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_mainwindow.cpp

@ -22,8 +22,8 @@ QT_BEGIN_MOC_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_MainWindow_t {
QByteArrayData data[1];
char stringdata0[11];
QByteArrayData data[7];
char stringdata0[113];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
@ -32,10 +32,18 @@ struct qt_meta_stringdata_MainWindow_t {
)
static const qt_meta_stringdata_MainWindow_t qt_meta_stringdata_MainWindow = {
{
QT_MOC_LITERAL(0, 0, 10) // "MainWindow"
QT_MOC_LITERAL(0, 0, 10), // "MainWindow"
QT_MOC_LITERAL(1, 11, 24), // "on_connectButton_clicked"
QT_MOC_LITERAL(2, 36, 0), // ""
QT_MOC_LITERAL(3, 37, 21), // "on_readButton_clicked"
QT_MOC_LITERAL(4, 59, 19), // "printDataFromClient"
QT_MOC_LITERAL(5, 79, 10), // "readedData"
QT_MOC_LITERAL(6, 90, 22) // "on_writeButton_clicked"
},
"MainWindow"
"MainWindow\0on_connectButton_clicked\0"
"\0on_readButton_clicked\0printDataFromClient\0"
"readedData\0on_writeButton_clicked"
};
#undef QT_MOC_LITERAL
@ -45,22 +53,41 @@ static const uint qt_meta_data_MainWindow[] = {
8, // revision
0, // classname
0, 0, // classinfo
0, 0, // methods
4, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount
// slots: name, argc, parameters, tag, flags
1, 0, 34, 2, 0x08 /* Private */,
3, 0, 35, 2, 0x08 /* Private */,
4, 1, 36, 2, 0x08 /* Private */,
6, 0, 39, 2, 0x08 /* Private */,
// slots: parameters
QMetaType::Void,
QMetaType::Void,
QMetaType::Void, QMetaType::QString, 5,
QMetaType::Void,
0 // eod
};
void MainWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
Q_UNUSED(_o);
Q_UNUSED(_id);
Q_UNUSED(_c);
Q_UNUSED(_a);
if (_c == QMetaObject::InvokeMetaMethod) {
auto *_t = static_cast<MainWindow *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->on_connectButton_clicked(); break;
case 1: _t->on_readButton_clicked(); break;
case 2: _t->printDataFromClient((*reinterpret_cast< QString(*)>(_a[1]))); break;
case 3: _t->on_writeButton_clicked(); break;
default: ;
}
}
}
QT_INIT_METAOBJECT const QMetaObject MainWindow::staticMetaObject = { {
@ -89,6 +116,17 @@ void *MainWindow::qt_metacast(const char *_clname)
int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QMainWindow::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 4)
qt_static_metacall(this, _c, _id, _a);
_id -= 4;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 4)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 4;
}
return _id;
}
QT_WARNING_POP

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_mainwindow.o

Binary file not shown.

53
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_modbusmaster.cpp

@ -22,8 +22,8 @@ QT_BEGIN_MOC_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_modBusMaster_t {
QByteArrayData data[3];
char stringdata0[24];
QByteArrayData data[5];
char stringdata0[55];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
@ -33,11 +33,14 @@ struct qt_meta_stringdata_modBusMaster_t {
static const qt_meta_stringdata_modBusMaster_t qt_meta_stringdata_modBusMaster = {
{
QT_MOC_LITERAL(0, 0, 12), // "modBusMaster"
QT_MOC_LITERAL(1, 13, 9), // "readReady"
QT_MOC_LITERAL(2, 23, 0) // ""
QT_MOC_LITERAL(1, 13, 14), // "dataFromClient"
QT_MOC_LITERAL(2, 28, 0), // ""
QT_MOC_LITERAL(3, 29, 15), // "readedParameter"
QT_MOC_LITERAL(4, 45, 9) // "readReady"
},
"modBusMaster\0readReady\0"
"modBusMaster\0dataFromClient\0\0"
"readedParameter\0readReady"
};
#undef QT_MOC_LITERAL
@ -47,15 +50,21 @@ static const uint qt_meta_data_modBusMaster[] = {
8, // revision
0, // classname
0, 0, // classinfo
1, 14, // methods
2, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount
1, // signalCount
// signals: name, argc, parameters, tag, flags
1, 1, 24, 2, 0x06 /* Public */,
// slots: name, argc, parameters, tag, flags
1, 0, 19, 2, 0x0a /* Public */,
4, 0, 27, 2, 0x0a /* Public */,
// signals: parameters
QMetaType::Void, QMetaType::QString, 3,
// slots: parameters
QMetaType::Void,
@ -69,11 +78,20 @@ void modBusMaster::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id
auto *_t = static_cast<modBusMaster *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->readReady(); break;
case 0: _t->dataFromClient((*reinterpret_cast< QString(*)>(_a[1]))); break;
case 1: _t->readReady(); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast<int *>(_a[0]);
{
using _t = void (modBusMaster::*)(QString );
if (*reinterpret_cast<_t *>(_a[1]) == static_cast<_t>(&modBusMaster::dataFromClient)) {
*result = 0;
return;
}
}
}
Q_UNUSED(_a);
}
QT_INIT_METAOBJECT const QMetaObject modBusMaster::staticMetaObject = { {
@ -105,15 +123,22 @@ int modBusMaster::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 1)
if (_id < 2)
qt_static_metacall(this, _c, _id, _a);
_id -= 1;
_id -= 2;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 1)
if (_id < 2)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 1;
_id -= 2;
}
return _id;
}
// SIGNAL 0
void modBusMaster::dataFromClient(QString _t1)
{
void *_a[] = { nullptr, const_cast<void*>(reinterpret_cast<const void*>(std::addressof(_t1))) };
QMetaObject::activate(this, &staticMetaObject, 0, _a);
}
QT_WARNING_POP
QT_END_MOC_NAMESPACE

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_modbusmaster.o

Binary file not shown.

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/modbusmaster.o

Binary file not shown.

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/serialConnectionManager

Binary file not shown.

349
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/ui_mainwindow.h

@ -11,11 +11,21 @@
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
@ -24,8 +34,48 @@ class Ui_MainWindow
{
public:
QWidget *centralwidget;
QPushButton *pushButton;
QWidget *layoutWidget;
QGridLayout *gridLayout;
QLabel *label_27;
QSpinBox *serverEdit;
QLineEdit *portEdit;
QPushButton *connectButton;
QLabel *label;
QLabel *label_2;
QWidget *layoutWidget_2;
QHBoxLayout *horizontalLayout_2;
QGroupBox *groupBox_2;
QGridLayout *gridLayout_3;
QLabel *label_4;
QSpinBox *readAddress;
QComboBox *readSize;
QLabel *label_9;
QTextEdit *textEditRead;
QLabel *label_5;
QGroupBox *writeGroupBox;
QGridLayout *gridLayout_2;
QSpinBox *writeAddress;
QVBoxLayout *verticalLayout_2;
QCheckBox *coilData1;
QCheckBox *coilData2;
QComboBox *writeSize;
QLabel *label_7;
QLabel *label_8;
QVBoxLayout *verticalLayout;
QHBoxLayout *horizontalLayout_3;
QLabel *label_3;
QTextEdit *textEdit1;
QLabel *label_10;
QTextEdit *textEdit2;
QVBoxLayout *verticalLayout_3;
QWidget *layoutWidget_3;
QHBoxLayout *horizontalLayout;
QLabel *label_6;
QComboBox *writeTable;
QSpacerItem *horizontalSpacer_2;
QPushButton *readButton;
QPushButton *writeButton;
QPushButton *readWriteButton;
QMenuBar *menubar;
QStatusBar *statusbar;
@ -33,19 +83,254 @@ public:
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(469, 316);
MainWindow->resize(588, 520);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(60, 20, 89, 25));
label = new QLabel(centralwidget);
layoutWidget = new QWidget(centralwidget);
layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
layoutWidget->setGeometry(QRect(0, 10, 571, 28));
gridLayout = new QGridLayout(layoutWidget);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setContentsMargins(0, 0, 0, 0);
label_27 = new QLabel(layoutWidget);
label_27->setObjectName(QString::fromUtf8("label_27"));
QSizePolicy sizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(label_27->sizePolicy().hasHeightForWidth());
label_27->setSizePolicy(sizePolicy);
gridLayout->addWidget(label_27, 0, 4, 1, 1);
serverEdit = new QSpinBox(layoutWidget);
serverEdit->setObjectName(QString::fromUtf8("serverEdit"));
QSizePolicy sizePolicy1(QSizePolicy::Maximum, QSizePolicy::Fixed);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(serverEdit->sizePolicy().hasHeightForWidth());
serverEdit->setSizePolicy(sizePolicy1);
serverEdit->setMinimum(1);
serverEdit->setMaximum(247);
gridLayout->addWidget(serverEdit, 0, 5, 1, 1);
portEdit = new QLineEdit(layoutWidget);
portEdit->setObjectName(QString::fromUtf8("portEdit"));
QSizePolicy sizePolicy2(QSizePolicy::Preferred, QSizePolicy::Fixed);
sizePolicy2.setHorizontalStretch(0);
sizePolicy2.setVerticalStretch(0);
sizePolicy2.setHeightForWidth(portEdit->sizePolicy().hasHeightForWidth());
portEdit->setSizePolicy(sizePolicy2);
gridLayout->addWidget(portEdit, 0, 3, 1, 1);
connectButton = new QPushButton(layoutWidget);
connectButton->setObjectName(QString::fromUtf8("connectButton"));
sizePolicy1.setHeightForWidth(connectButton->sizePolicy().hasHeightForWidth());
connectButton->setSizePolicy(sizePolicy1);
connectButton->setCheckable(false);
connectButton->setAutoDefault(false);
gridLayout->addWidget(connectButton, 0, 6, 1, 1);
label = new QLabel(layoutWidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(210, 80, 67, 17));
gridLayout->addWidget(label, 0, 0, 1, 1);
label_2 = new QLabel(layoutWidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
sizePolicy.setHeightForWidth(label_2->sizePolicy().hasHeightForWidth());
label_2->setSizePolicy(sizePolicy);
gridLayout->addWidget(label_2, 0, 1, 1, 1);
layoutWidget_2 = new QWidget(centralwidget);
layoutWidget_2->setObjectName(QString::fromUtf8("layoutWidget_2"));
layoutWidget_2->setGeometry(QRect(0, 40, 571, 455));
horizontalLayout_2 = new QHBoxLayout(layoutWidget_2);
horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
horizontalLayout_2->setContentsMargins(0, 0, 0, 0);
groupBox_2 = new QGroupBox(layoutWidget_2);
groupBox_2->setObjectName(QString::fromUtf8("groupBox_2"));
groupBox_2->setMinimumSize(QSize(250, 0));
gridLayout_3 = new QGridLayout(groupBox_2);
gridLayout_3->setObjectName(QString::fromUtf8("gridLayout_3"));
label_4 = new QLabel(groupBox_2);
label_4->setObjectName(QString::fromUtf8("label_4"));
gridLayout_3->addWidget(label_4, 0, 1, 1, 1);
readAddress = new QSpinBox(groupBox_2);
readAddress->setObjectName(QString::fromUtf8("readAddress"));
readAddress->setMaximum(15);
gridLayout_3->addWidget(readAddress, 0, 2, 1, 1);
readSize = new QComboBox(groupBox_2);
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->addItem(QString());
readSize->setObjectName(QString::fromUtf8("readSize"));
gridLayout_3->addWidget(readSize, 1, 2, 1, 1);
label_9 = new QLabel(groupBox_2);
label_9->setObjectName(QString::fromUtf8("label_9"));
gridLayout_3->addWidget(label_9, 2, 1, 1, 1);
textEditRead = new QTextEdit(groupBox_2);
textEditRead->setObjectName(QString::fromUtf8("textEditRead"));
gridLayout_3->addWidget(textEditRead, 3, 1, 1, 2);
label_5 = new QLabel(groupBox_2);
label_5->setObjectName(QString::fromUtf8("label_5"));
gridLayout_3->addWidget(label_5, 1, 1, 1, 1);
horizontalLayout_2->addWidget(groupBox_2);
writeGroupBox = new QGroupBox(layoutWidget_2);
writeGroupBox->setObjectName(QString::fromUtf8("writeGroupBox"));
writeGroupBox->setMinimumSize(QSize(225, 0));
gridLayout_2 = new QGridLayout(writeGroupBox);
gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));
writeAddress = new QSpinBox(writeGroupBox);
writeAddress->setObjectName(QString::fromUtf8("writeAddress"));
writeAddress->setMaximum(9);
gridLayout_2->addWidget(writeAddress, 0, 1, 1, 1);
verticalLayout_2 = new QVBoxLayout();
verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
coilData1 = new QCheckBox(writeGroupBox);
coilData1->setObjectName(QString::fromUtf8("coilData1"));
verticalLayout_2->addWidget(coilData1);
coilData2 = new QCheckBox(writeGroupBox);
coilData2->setObjectName(QString::fromUtf8("coilData2"));
verticalLayout_2->addWidget(coilData2);
gridLayout_2->addLayout(verticalLayout_2, 2, 0, 1, 2);
writeSize = new QComboBox(writeGroupBox);
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->addItem(QString());
writeSize->setObjectName(QString::fromUtf8("writeSize"));
gridLayout_2->addWidget(writeSize, 1, 1, 1, 1);
label_7 = new QLabel(writeGroupBox);
label_7->setObjectName(QString::fromUtf8("label_7"));
gridLayout_2->addWidget(label_7, 0, 0, 1, 1);
label_8 = new QLabel(writeGroupBox);
label_8->setObjectName(QString::fromUtf8("label_8"));
gridLayout_2->addWidget(label_8, 1, 0, 1, 1);
verticalLayout = new QVBoxLayout();
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
horizontalLayout_3 = new QHBoxLayout();
horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
verticalLayout->addLayout(horizontalLayout_3);
label_3 = new QLabel(writeGroupBox);
label_3->setObjectName(QString::fromUtf8("label_3"));
verticalLayout->addWidget(label_3);
textEdit1 = new QTextEdit(writeGroupBox);
textEdit1->setObjectName(QString::fromUtf8("textEdit1"));
verticalLayout->addWidget(textEdit1);
label_10 = new QLabel(writeGroupBox);
label_10->setObjectName(QString::fromUtf8("label_10"));
verticalLayout->addWidget(label_10);
textEdit2 = new QTextEdit(writeGroupBox);
textEdit2->setObjectName(QString::fromUtf8("textEdit2"));
verticalLayout->addWidget(textEdit2);
verticalLayout_3 = new QVBoxLayout();
verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
verticalLayout->addLayout(verticalLayout_3);
gridLayout_2->addLayout(verticalLayout, 3, 0, 1, 2);
horizontalLayout_2->addWidget(writeGroupBox);
layoutWidget_3 = new QWidget(centralwidget);
layoutWidget_3->setObjectName(QString::fromUtf8("layoutWidget_3"));
layoutWidget_3->setGeometry(QRect(10, 350, 561, 27));
horizontalLayout = new QHBoxLayout(layoutWidget_3);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);
label_6 = new QLabel(layoutWidget_3);
label_6->setObjectName(QString::fromUtf8("label_6"));
horizontalLayout->addWidget(label_6);
writeTable = new QComboBox(layoutWidget_3);
writeTable->setObjectName(QString::fromUtf8("writeTable"));
horizontalLayout->addWidget(writeTable);
horizontalSpacer_2 = new QSpacerItem(13, 17, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer_2);
readButton = new QPushButton(layoutWidget_3);
readButton->setObjectName(QString::fromUtf8("readButton"));
sizePolicy1.setHeightForWidth(readButton->sizePolicy().hasHeightForWidth());
readButton->setSizePolicy(sizePolicy1);
horizontalLayout->addWidget(readButton);
writeButton = new QPushButton(layoutWidget_3);
writeButton->setObjectName(QString::fromUtf8("writeButton"));
horizontalLayout->addWidget(writeButton);
readWriteButton = new QPushButton(layoutWidget_3);
readWriteButton->setObjectName(QString::fromUtf8("readWriteButton"));
readWriteButton->setEnabled(false);
horizontalLayout->addWidget(readWriteButton);
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 469, 22));
menubar->setGeometry(QRect(0, 0, 588, 22));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
@ -53,14 +338,58 @@ public:
retranslateUi(MainWindow);
connectButton->setDefault(true);
readSize->setCurrentIndex(9);
writeSize->setCurrentIndex(9);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
pushButton->setText(QCoreApplication::translate("MainWindow", "PushButton", nullptr));
label->setText(QCoreApplication::translate("MainWindow", "jj;j'jl';'l'", nullptr));
label_27->setText(QCoreApplication::translate("MainWindow", "Server Address:", nullptr));
connectButton->setText(QCoreApplication::translate("MainWindow", "Connect", nullptr));
label->setText(QCoreApplication::translate("MainWindow", "Connection type Is Serial:", nullptr));
label_2->setText(QCoreApplication::translate("MainWindow", "Port:", nullptr));
groupBox_2->setTitle(QCoreApplication::translate("MainWindow", "Read", nullptr));
label_4->setText(QCoreApplication::translate("MainWindow", "Start address:", nullptr));
readSize->setItemText(0, QCoreApplication::translate("MainWindow", "1", nullptr));
readSize->setItemText(1, QCoreApplication::translate("MainWindow", "2", nullptr));
readSize->setItemText(2, QCoreApplication::translate("MainWindow", "3", nullptr));
readSize->setItemText(3, QCoreApplication::translate("MainWindow", "4", nullptr));
readSize->setItemText(4, QCoreApplication::translate("MainWindow", "5", nullptr));
readSize->setItemText(5, QCoreApplication::translate("MainWindow", "6", nullptr));
readSize->setItemText(6, QCoreApplication::translate("MainWindow", "7", nullptr));
readSize->setItemText(7, QCoreApplication::translate("MainWindow", "8", nullptr));
readSize->setItemText(8, QCoreApplication::translate("MainWindow", "9", nullptr));
readSize->setItemText(9, QCoreApplication::translate("MainWindow", "10", nullptr));
label_9->setText(QCoreApplication::translate("MainWindow", "Result:", nullptr));
label_5->setText(QCoreApplication::translate("MainWindow", "Number of values:", nullptr));
writeGroupBox->setTitle(QCoreApplication::translate("MainWindow", "Write", nullptr));
coilData1->setText(QCoreApplication::translate("MainWindow", "Coil1", nullptr));
coilData2->setText(QCoreApplication::translate("MainWindow", "Coil2", nullptr));
writeSize->setItemText(0, QCoreApplication::translate("MainWindow", "1", nullptr));
writeSize->setItemText(1, QCoreApplication::translate("MainWindow", "2", nullptr));
writeSize->setItemText(2, QCoreApplication::translate("MainWindow", "3", nullptr));
writeSize->setItemText(3, QCoreApplication::translate("MainWindow", "4", nullptr));
writeSize->setItemText(4, QCoreApplication::translate("MainWindow", "5", nullptr));
writeSize->setItemText(5, QCoreApplication::translate("MainWindow", "6", nullptr));
writeSize->setItemText(6, QCoreApplication::translate("MainWindow", "7", nullptr));
writeSize->setItemText(7, QCoreApplication::translate("MainWindow", "8", nullptr));
writeSize->setItemText(8, QCoreApplication::translate("MainWindow", "9", nullptr));
writeSize->setItemText(9, QCoreApplication::translate("MainWindow", "10", nullptr));
label_7->setText(QCoreApplication::translate("MainWindow", "Start address:", nullptr));
label_8->setText(QCoreApplication::translate("MainWindow", "Number of values:", nullptr));
label_3->setText(QCoreApplication::translate("MainWindow", "register1", nullptr));
label_10->setText(QCoreApplication::translate("MainWindow", "register2", nullptr));
label_6->setText(QCoreApplication::translate("MainWindow", "Table:", nullptr));
readButton->setText(QCoreApplication::translate("MainWindow", "Read", nullptr));
writeButton->setText(QCoreApplication::translate("MainWindow", "Write", nullptr));
readWriteButton->setText(QCoreApplication::translate("MainWindow", "Read-Write", nullptr));
} // retranslateUi
};

70
serialConnectionManager/mainwindow.cpp

@ -1,16 +1,84 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qdebug.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->writeTable->addItem(tr("Coils"), 0);
ui->writeTable->addItem(tr("Discrete Inputs"), 1);
ui->writeTable->addItem(tr("Input Registers"), 2);
ui->writeTable->addItem(tr("Holding Registers"), 3);
connect(&modBusObj, &modBusMaster::dataFromClient, this, &MainWindow::printDataFromClient);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_connectButton_clicked()
{
modBusObj.init();
modBusObj.open(ui->portEdit->text(),ui->serverEdit->value());
}
void MainWindow::on_readButton_clicked()
{
int startAddress = ui->readAddress->value();
// do not go beyond 10 entries
int numberOfEntries = qMin(ui->readSize->currentText().toInt(), 10 - startAddress);
switch (ui->writeTable->currentData().toInt()) {
case 0: modBusObj.getCoil(startAddress,static_cast<quint16>(numberOfEntries));
break;
case 1: modBusObj.getInputCoil(startAddress,static_cast<quint16>(numberOfEntries));
break;
case 2: modBusObj.getInputRegister(startAddress,static_cast<quint16>(numberOfEntries));
break;
case 3: modBusObj.getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries));
break;
}
}
void MainWindow::printDataFromClient(QString readedData)
{
ui->textEditRead->setText(readedData);
}
void MainWindow::on_writeButton_clicked()
{
int startAddress = ui->writeAddress->value();
// do not go beyond 10 entries
int numberOfEntries = qMin(ui->writeSize->currentText().toInt(), 10 - startAddress);
switch (ui->writeTable->currentData().toInt()) {
case 0:
if(numberOfEntries>1)
{
uiCoils.setBit(1,ui->coilData1->isChecked());
uiCoils.setBit(2,ui->coilData2->isChecked());
modBusObj.setMultipleCoil(startAddress, numberOfEntries, uiCoils);
}
else if (numberOfEntries==1)
{
modBusObj.setSingleCoil(startAddress, ui->coilData1->isChecked());
}
break;
case 3:
if(numberOfEntries>1)
{
uiholdingRegisters[0]=ui->textEdit1->toPlainText().toUInt();
uiholdingRegisters[1]=ui->textEdit1->toPlainText().toUInt();
modBusObj.setMultipleRegister(startAddress, numberOfEntries, uiholdingRegisters);
}
else if (numberOfEntries==1)
{
modBusObj.setSingleRgister(startAddress, ui->textEdit1->toPlainText().toUInt());
}
break;
}
}

12
serialConnectionManager/mainwindow.h

@ -2,7 +2,7 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include "modbusmaster.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
@ -16,7 +16,17 @@ public:
~MainWindow();
private slots:
void on_connectButton_clicked();
void on_readButton_clicked();
void printDataFromClient(QString readedData);
void on_writeButton_clicked();
private:
Ui::MainWindow *ui;
modBusMaster modBusObj;
QBitArray uiCoils;
QVector<quint16> uiholdingRegisters;
};
#endif // MAINWINDOW_H

424
serialConnectionManager/mainwindow.ui

@ -6,39 +6,427 @@
<rect>
<x>0</x>
<y>0</y>
<width>469</width>
<height>316</height>
<width>588</width>
<height>520</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>60</x>
<y>20</y>
<width>89</width>
<height>25</height>
<x>0</x>
<y>10</y>
<width>571</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="4">
<widget class="QLabel" name="label_27">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Server Address:</string>
</property>
</widget>
</item>
<item row="0" column="5">
<widget class="QSpinBox" name="serverEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>247</number>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QLineEdit" name="portEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="6">
<widget class="QPushButton" name="connectButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Connect</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Connection type Is Serial:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QLabel" name="label">
<widget class="QWidget" name="layoutWidget_2">
<property name="geometry">
<rect>
<x>210</x>
<y>80</y>
<width>67</width>
<height>17</height>
<x>0</x>
<y>40</y>
<width>571</width>
<height>455</height>
</rect>
</property>
<property name="text">
<string>jj;j'jl';'l'</string>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="minimumSize">
<size>
<width>250</width>
<height>0</height>
</size>
</property>
<property name="title">
<string>Read</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Start address:</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QSpinBox" name="readAddress">
<property name="maximum">
<number>15</number>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QComboBox" name="readSize">
<property name="currentIndex">
<number>9</number>
</property>
<item>
<property name="text">
<string>1</string>
</property>
</item>
<item>
<property name="text">
<string>2</string>
</property>
</item>
<item>
<property name="text">
<string>3</string>
</property>
</item>
<item>
<property name="text">
<string>4</string>
</property>
</item>
<item>
<property name="text">
<string>5</string>
</property>
</item>
<item>
<property name="text">
<string>6</string>
</property>
</item>
<item>
<property name="text">
<string>7</string>
</property>
</item>
<item>
<property name="text">
<string>8</string>
</property>
</item>
<item>
<property name="text">
<string>9</string>
</property>
</item>
<item>
<property name="text">
<string>10</string>
</property>
</item>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Result:</string>
</property>
</widget>
</item>
<item row="3" column="1" colspan="2">
<widget class="QTextEdit" name="textEditRead"/>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Number of values:</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="writeGroupBox">
<property name="minimumSize">
<size>
<width>225</width>
<height>0</height>
</size>
</property>
<property name="title">
<string>Write</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<widget class="QSpinBox" name="writeAddress">
<property name="maximum">
<number>9</number>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="coilData1">
<property name="text">
<string>Coil1</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="coilData2">
<property name="text">
<string>Coil2</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="writeSize">
<property name="currentIndex">
<number>9</number>
</property>
<item>
<property name="text">
<string>1</string>
</property>
</item>
<item>
<property name="text">
<string>2</string>
</property>
</item>
<item>
<property name="text">
<string>3</string>
</property>
</item>
<item>
<property name="text">
<string>4</string>
</property>
</item>
<item>
<property name="text">
<string>5</string>
</property>
</item>
<item>
<property name="text">
<string>6</string>
</property>
</item>
<item>
<property name="text">
<string>7</string>
</property>
</item>
<item>
<property name="text">
<string>8</string>
</property>
</item>
<item>
<property name="text">
<string>9</string>
</property>
</item>
<item>
<property name="text">
<string>10</string>
</property>
</item>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Start address:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Number of values:</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3"/>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>register1</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="textEdit1"/>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>register2</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="textEdit2"/>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="layoutWidget_3">
<property name="geometry">
<rect>
<x>10</x>
<y>350</y>
<width>561</width>
<height>27</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Table:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="writeTable"/>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>13</width>
<height>17</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="readButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Read</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="writeButton">
<property name="text">
<string>Write</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="readWriteButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Read-Write</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
@ -46,7 +434,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>469</width>
<width>588</width>
<height>22</height>
</rect>
</property>

108
serialConnectionManager/modbusmaster.cpp

@ -1,9 +1,9 @@
#include "modbusmaster.h"
#include <QModbusTcpClient>
#include <QModbusRtuSerialMaster>
#include <qdebug.h>
#include "serialportexception.h"
enum { NumColumn = 0, CoilsColumn = 1, HoldingColumn = 2, ColumnCount = 3, RowCount = 10 };
modBusMaster::modBusMaster(QObject *parent) : QObject(parent)
{
@ -11,20 +11,24 @@ modBusMaster::modBusMaster(QObject *parent) : QObject(parent)
void modBusMaster::init()
{
close();
modbusDevice = nullptr;
modbusDevice = new QModbusRtuSerialMaster(this);
connect(modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error)
{
throw serialPortException(modbusDevice->errorString());
});
if (!modbusDevice)
throw serialPortException("Could not create Modbus master.");
}
void modBusMaster::open(QString _serialPort , int _serverId)
{
serialPort=_serialPort;
serverId=_serverId;
if (!modbusDevice)
@ -60,15 +64,54 @@ void modBusMaster::close()
void modBusMaster::stop()
{
}
void modBusMaster::setSingleCoil(int startAddress,bool coilFlag)
{
if (!modbusDevice)
return;
m_coils.clear();
m_coils.setBit(1,coilFlag);
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, 1);
}
void modBusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray someCoilFlags)
{
if (!modbusDevice)
return;
m_holdingRegisters.clear();
m_coils.clear();
m_coils = someCoilFlags;
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize);
}
void modBusMaster::setSingleRgister(int startAddress, quint16 RegisterValue)
{
if (!modbusDevice)
return;
m_holdingRegisters.clear();
m_holdingRegisters[0]=RegisterValue;
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, 1);
}
void modBusMaster::setMultipleRegister(int startAddress, quint16 writeSize , QVector<quint16> someRegisterValue)
{
if (!modbusDevice)
return;
m_holdingRegisters = someRegisterValue;
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, writeSize);
}
void modBusMaster::getCoil(int startAddress, quint16 readSize)
{
if (!modbusDevice)
return;
readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize);
}
void modBusMaster::getInputCoil(int startAddress, quint16 readSize)
@ -95,46 +138,47 @@ void modBusMaster::getHoldingRegister(int startAddress, quint16 readSize)
void modBusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize)
{
qDebug() << " first of readRequest";
auto table =registerType;
Q_ASSERT(startAddress >= 0 && startAddress < 10);
reply=modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), serverId) ;
if (reply)
{
if (!reply->isFinished())
connect(reply, &QModbusReply::finished, this, &modBusMaster::readReady);
connect(reply, &QModbusReply::finished, this, &modBusMaster::readReady);
else
delete reply; // broadcast replies return immediately
}
else
{
throw serialPortException(modbusDevice->errorString());
}
}
void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize)
void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 writeSize)
{
auto table =registerType;
Q_ASSERT(startAddress >= 0 && startAddress < 10);
QModbusDataUnit writeUnit = QModbusDataUnit(table, startAddress, readSize);
QModbusDataUnit writeUnit = QModbusDataUnit(table, startAddress, writeSize);
for (int i = 0; i < writeUnit.valueCount(); i++) {
if (table == QModbusDataUnit::Coils)
writeUnit.setValue(i, m_coils[i + writeUnit.startAddress()]);
else
writeUnit.setValue(i, m_holdingRegisters[i + writeUnit.startAddress()]);
}
if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, serverId)) {
if (!reply->isFinished()) {
connect(reply, &QModbusReply::finished, this, [this, reply]() {
connect(reply, &QModbusReply::finished, this, [reply]() {
if (reply->error() == QModbusDevice::ProtocolError) {
throw serialPortException("Write response error: %1 (Mobus exception: 0x%2)");
throw serialPortException(tr("Write response error: %1 (Mobus exception: 0x%2)")
.arg(reply->errorString()).arg(reply->rawResult().exceptionCode(), -1, 16));
} else if (reply->error() != QModbusDevice::NoError) {
throw serialPortException("Write response error: %1 (code: 0x%2)");
throw serialPortException(tr("Write response error: %1 (code: 0x%2)").
arg(reply->errorString()).arg(reply->error(), -1, 16));
}
reply->deleteLater();
});
@ -143,13 +187,39 @@ QModbusDataUnit writeUnit = QModbusDataUnit(table, startAddress, readSize);
reply->deleteLater();
}
} else {
throw serialPortException(tr("Write error: ") + modbusDevice->errorString());
throw serialPortException(tr("Write error: ") + modbusDevice->errorString());
}
}
void modBusMaster::readReady()
{
auto reply = qobject_cast<QModbusReply *>(sender());
if (!reply)
return;
QString readedParameter;
if (reply->error() == QModbusDevice::NoError) {
const QModbusDataUnit unit = reply->result();
for (uint i = 0; i < unit.valueCount(); i++) {
const QString entry = tr("Address: %1, Value: %2").arg(static_cast<quint16>(unit.startAddress())+ i)
.arg(QString::number(unit.value(i),
unit.registerType() <= QModbusDataUnit::Coils ? 10 : 16));
readedParameter += (entry + "\n" );
}
emit dataFromClient(readedParameter);
} else if (reply->error() == QModbusDevice::ProtocolError) {
throw serialPortException(tr("Read response error: %1 (Mobus exception: 0x%2)").
arg(reply->errorString()).
arg(reply->rawResult().exceptionCode(), -1, 16));
} else {
throw serialPortException(tr("Read response error: %1 (code: 0x%2)").
arg(reply->errorString()).
arg(reply->error(), -1, 16));
}
reply->deleteLater();
}

16
serialConnectionManager/modbusmaster.h

@ -25,25 +25,25 @@ public:
void deinit();
void close();
void stop();
void setSingleCoil();
void setMultipleCoil();
void setSingleRgister();
void setMultipleRegister();
void setSingleCoil(int startAddress,bool coilFlag);
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray someCoilFlags);
void setSingleRgister(int startAddress, quint16 RegisterValue);
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> someRegisterValue);
void getCoil(int startAddress, quint16 readSize);
void getInputCoil(int startAddress, quint16 readSize);
void getInputRegister(int startAddress, quint16 readSize);
void getHoldingRegister(int startAddress, quint16 readSize);
void getHoldingRegister(int startAddress, quint16 readSize);
void readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
void writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
void writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
signals:
void dataFromClient(QString readedParameter);
public slots:
void readReady();
private:
QModbusReply *lastRequest;
QModbusClient *modbusDevice;
QModbusClient *modbusDevice=nullptr;
QString serialPort ;
int serverId;
public:

2
serialConnectionManager/serialportexception.h

@ -11,7 +11,7 @@ public:
serialPortException(QString str)
{
_str = str;
qDebug() << _str ;
qDebug() << "errror" <<_str ;
}
virtual const char* what() const throw()

Loading…
Cancel
Save