Browse Source

code read/Write v1 14000624

master
nasi 4 years ago
parent
commit
d1d3b8fe8d
  1. 27
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/Makefile
  2. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/mainwindow.o
  3. 40
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_modbusmaster.cpp
  4. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/moc_modbusmaster.o
  5. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/modbusmaster.o
  6. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/serialConnectionManager
  7. 15
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/ui_mainwindow.h
  8. 44
      serialConnectionManager/mainwindow.ui
  9. 129
      serialConnectionManager/modbusmaster.cpp
  10. 38
      serialConnectionManager/modbusmaster.h

27
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/Makefile

@ -841,6 +841,20 @@ moc_modbusmaster.cpp: ../serialConnectionManager/modbusmaster.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qcontiguouscache.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qsharedpointer.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qsharedpointer_impl.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/QtCore/qdatastream.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/modbusmaster.h -o moc_modbusmaster.cpp
@ -1159,8 +1173,8 @@ modbusmaster.o: ../serialConnectionManager/modbusmaster.cpp ../serialConnectionM
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qcontiguouscache.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qsharedpointer.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qsharedpointer_impl.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/QModbusTcpClient \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbustcpclient.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 \
@ -1168,8 +1182,13 @@ modbusmaster.o: ../serialConnectionManager/modbusmaster.cpp ../serialConnectionM
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbuspdu.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/qdatastream.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbusreply.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/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/QtSerialBus/QModbusTcpClient \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtSerialBus/qmodbustcpclient.h \
../serialConnectionManager/serialportexception.h \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QDebug \
../../../Qt5.13.1/5.13.1/gcc_64/include/QtCore/QString

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

Binary file not shown.

40
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[1];
char stringdata0[13];
QByteArrayData data[3];
char stringdata0[24];
};
#define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
@ -32,10 +32,12 @@ 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(0, 0, 12), // "modBusMaster"
QT_MOC_LITERAL(1, 13, 9), // "readReady"
QT_MOC_LITERAL(2, 23, 0) // ""
},
"modBusMaster"
"modBusMaster\0readReady\0"
};
#undef QT_MOC_LITERAL
@ -45,21 +47,32 @@ static const uint qt_meta_data_modBusMaster[] = {
8, // revision
0, // classname
0, 0, // classinfo
0, 0, // methods
1, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount
// slots: name, argc, parameters, tag, flags
1, 0, 19, 2, 0x0a /* Public */,
// slots: parameters
QMetaType::Void,
0 // eod
};
void modBusMaster::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
Q_UNUSED(_o);
Q_UNUSED(_id);
Q_UNUSED(_c);
if (_c == QMetaObject::InvokeMetaMethod) {
auto *_t = static_cast<modBusMaster *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->readReady(); break;
default: ;
}
}
Q_UNUSED(_a);
}
@ -89,6 +102,17 @@ void *modBusMaster::qt_metacast(const char *_clname)
int modBusMaster::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 1)
qt_static_metacall(this, _c, _id, _a);
_id -= 1;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 1)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 1;
}
return _id;
}
QT_WARNING_POP

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.

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

@ -11,8 +11,10 @@
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QWidget>
@ -22,6 +24,8 @@ class Ui_MainWindow
{
public:
QWidget *centralwidget;
QPushButton *pushButton;
QLabel *label;
QMenuBar *menubar;
QStatusBar *statusbar;
@ -29,12 +33,19 @@ public:
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 600);
MainWindow->resize(469, 316);
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);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(210, 80, 67, 17));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 469, 22));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
@ -48,6 +59,8 @@ public:
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));
} // retranslateUi
};

44
serialConnectionManager/mainwindow.ui

@ -6,15 +6,51 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
<width>469</width>
<height>316</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>60</x>
<y>20</y>
<width>89</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>210</x>
<y>80</y>
<width>67</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>jj;j'jl';'l'</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>469</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>

129
serialConnectionManager/modbusmaster.cpp

@ -11,7 +11,7 @@ modBusMaster::modBusMaster(QObject *parent) : QObject(parent)
void modBusMaster::init()
{
stop();
close();
modbusDevice = nullptr;
modbusDevice = new QModbusRtuSerialMaster(this);
connect(modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error)
@ -23,14 +23,133 @@ void modBusMaster::init()
throw serialPortException("Could not create Modbus master.");
}
void modBusMaster::close()
void modBusMaster::open(QString _serialPort , int _serverId)
{
serialPort=_serialPort;
serverId=_serverId;
if (!modbusDevice)
return;
if (modbusDevice->state() != QModbusDevice::ConnectedState)
{
modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
serialPort);
#if QT_CONFIG(modbus_serialport)
modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter,
modbusSettings.parity);
modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
modbusSettings.baud);
modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
modbusSettings.dataBits);
modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
modbusSettings.stopBits);
#endif
modbusDevice->setTimeout(modbusSettings.responseTime);
modbusDevice->setNumberOfRetries(modbusSettings.numberOfRetries);
}
if (!modbusDevice->connectDevice())
{
throw serialPortException(modbusDevice->errorString());
}
}
void modBusMaster::stop()
void modBusMaster::close()
{
if (modbusDevice)
modbusDevice->disconnectDevice();
delete modbusDevice;
}
void modBusMaster::stop()
{
}
void modBusMaster::getCoil(int startAddress, quint16 readSize)
{
if (!modbusDevice)
return;
readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize);
}
void modBusMaster::getInputCoil(int startAddress, quint16 readSize)
{
if (!modbusDevice)
return;
readRequest(QModbusDataUnit::RegisterType::DiscreteInputs, startAddress, readSize);
}
void modBusMaster::getInputRegister(int startAddress, quint16 readSize)
{
if (!modbusDevice)
return;
readRequest(QModbusDataUnit::RegisterType::InputRegisters, startAddress, readSize);
}
void modBusMaster::getHoldingRegister(int startAddress, quint16 readSize)
{
if (!modbusDevice)
return;
readRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, readSize);
}
void modBusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize)
{
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);
else
delete reply; // broadcast replies return immediately
}
else
{
throw serialPortException(modbusDevice->errorString());
}
}
void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize)
{
auto table =registerType;
Q_ASSERT(startAddress >= 0 && startAddress < 10);
QModbusDataUnit writeUnit = QModbusDataUnit(table, startAddress, readSize);
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]() {
if (reply->error() == QModbusDevice::ProtocolError) {
throw serialPortException("Write response error: %1 (Mobus exception: 0x%2)");
} else if (reply->error() != QModbusDevice::NoError) {
throw serialPortException("Write response error: %1 (code: 0x%2)");
}
reply->deleteLater();
});
} else {
// broadcast replies return immediately
reply->deleteLater();
}
} else {
throw serialPortException(tr("Write error: ") + modbusDevice->errorString());
}
}
void modBusMaster::readReady()
{
}

38
serialConnectionManager/modbusmaster.h

@ -3,6 +3,12 @@
#include <QObject>
#include <QModbusDataUnit>
#include <QModbusRtuSerialMaster>
#include <QtSerialBus/qtserialbusglobal.h>
#include <QBitArray>
#if QT_CONFIG(modbus_serialport)
#include <QSerialPort>
#endif
class QModbusClient;
class QModbusReply;
@ -14,18 +20,46 @@ class modBusMaster : public QObject
public:
explicit modBusMaster(QObject *parent = nullptr);
void init();
void open(QString serialPort, quint16 pid);
void open(QString _serialPort , int _serverId);
void start();
void deinit();
void close();
void stop();
void setSingleCoil();
void setMultipleCoil();
void setSingleRgister();
void setMultipleRegister();
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 readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
void writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
signals:
public slots:
void readReady();
private:
QModbusReply *lastRequest;
QModbusClient *modbusDevice;
QString serialPort ;
int serverId;
public:
QModbusReply *reply;
QBitArray m_coils;
QVector<quint16> m_holdingRegisters;
struct Settings {
#if QT_CONFIG(modbus_serialport)
int parity = QSerialPort::EvenParity;
int baud = QSerialPort::Baud19200;
int dataBits = QSerialPort::Data8;
int stopBits = QSerialPort::OneStop;
#endif
int responseTime = 1000;
int numberOfRetries = 3;
} modbusSettings;
};

Loading…
Cancel
Save