Browse Source

Add Init & Connect

test
nasicurious 3 years ago
parent
commit
bf4389160c
  1. 9
      Servo/Servo.pro
  2. 17
      Servo/include/ModbusConfig.h
  3. 25
      Servo/include/ModbusMaster.h
  4. 20
      Servo/include/ModbusWrapper.h
  5. 25
      Servo/include/ServoException.h
  6. 68
      Servo/src/ModbusMaster.cpp
  7. 42
      Servo/src/ModbusWrapper.cpp
  8. 1
      ServoApplication.pro
  9. 24
      Test/MainWindow.cpp
  10. 22
      Test/MainWindow.h
  11. 27
      Test/MainWindow.ui
  12. 5
      Test/Test.pro
  13. 34
      Test/main.cpp

9
Servo/Servo.pro

@ -1,5 +1,8 @@
QT -= gui QT -= gui
QT += serialbus
QT += serialport
TEMPLATE = lib TEMPLATE = lib
DEFINES += SERVO_LIBRARY DEFINES += SERVO_LIBRARY
@ -18,12 +21,10 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
$$files(*.cpp, true) \ \ $$files(*.cpp, true)
HEADERS += \ HEADERS += \
$$files(*.h, true) \ \ $$files(*.h, true)
INCLUDEPATH += $$PWD/include INCLUDEPATH += $$PWD/include

17
Servo/include/ModbusConfig.h

@ -0,0 +1,17 @@
#ifndef SETTINGSERIALPARAMETERS_H
#define SETTINGSERIALPARAMETERS_H
#include <QSerialPort>
struct ModbusConfig {
QString serialPort;
int clientAddress;
int parity = QSerialPort::EvenParity;
int baud = QSerialPort::Baud19200;
int dataBits = QSerialPort::Data8;
int stopBits = QSerialPort::OneStop;
int responseTime = 1000;
int numberOfRetries = 3;
};
#endif //SETTINGSERIALPARAMETERS_H

25
Servo/include/ModbusMaster.h

@ -2,16 +2,31 @@
#define MODBUSMASTER_H #define MODBUSMASTER_H
#include <QObject> #include <QObject>
#include <QModbusDevice>
#include "ServoException.h"
#include "ModbusConfig.h"
class QModbusClient;
class ModbusMaster : public QObject class ModbusMaster : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
explicit ModbusMaster(QObject *parent = nullptr);
signals: private:
bool _initialized = false;
QModbusClient* _modbusDevice = nullptr;
int _clientAddress;
public:
explicit ModbusMaster(QObject* parent = nullptr);
public slots: //uncrustify off
public slots:
//uncrustify on
void init();
void connectToDevice(ModbusConfig modbusConfig);
void connectionStateChanged(QModbusDevice::State state);
}; };
#endif // MODBUSMASTER_H #endif //MODBUSMASTER_H

20
Servo/include/ModbusWrapper.h

@ -2,16 +2,32 @@
#define MODBUSWRAPPER_H #define MODBUSWRAPPER_H
#include <QObject> #include <QObject>
#include <QThread>
#include "ModbusMaster.h"
#include "ModbusConfig.h"
class ModbusWrapper : public QObject class ModbusWrapper : public QObject
{ {
Q_OBJECT Q_OBJECT
private:
ModbusMaster modbusMaster;
QThread workerThread;
public: public:
explicit ModbusWrapper(QObject *parent = nullptr); explicit ModbusWrapper(QObject* parent = nullptr);
~ModbusWrapper();
void connectToDevice(ModbusConfig modbusConfig);
void init();
signals: signals:
void connectOrder(ModbusConfig modbusConfig);
void initOrder();
//uncrustify off
public slots: public slots:
//uncrustify on
}; };
#endif // MODBUSWRAPPER_H #endif //MODBUSWRAPPER_H

25
Servo/include/ServoException.h

@ -0,0 +1,25 @@
#ifndef SERVOEXCEPTION_H
#define SERVOEXCEPTION_H
#include <QDebug>
#include <QString>
class ServoException : public std::exception
{
private:
QString _str;
public:
ServoException(QString str)
{
_str = str;
qDebug() << "Error Text is: " << _str;
}
virtual const char* what() const throw()
{
return _str.toStdString().c_str();
}
};
#endif //SERVOEXCEPTION_H

68
Servo/src/ModbusMaster.cpp

@ -1,6 +1,70 @@
#include "../include/ModbusMaster.h" #include "ModbusMaster.h"
ModbusMaster::ModbusMaster(QObject *parent) : QObject(parent) #include <QModbusRtuSerialMaster>
ModbusMaster::ModbusMaster(QObject* parent) : QObject(parent)
{
}
/*************************************************************************************************/
void ModbusMaster::init()
{
if(!_initialized)
{
_modbusDevice = nullptr;
_modbusDevice = new QModbusRtuSerialMaster(this);
connect(_modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error)
{
throw ServoException(_modbusDevice->errorString());
});
connect(_modbusDevice, &QModbusDevice::stateChanged, this,
&ModbusMaster::connectionStateChanged);
_initialized = true;
}
else
{
throw ServoException("Modbus Device Created Before Handle it");
}
qDebug() << "internal init";
}
/*************************************************************************************************/
void ModbusMaster::connectToDevice(ModbusConfig modbusConfig)
{
if(_modbusDevice->state() != QModbusDevice::ConnectedState)
{
_clientAddress = modbusConfig.clientAddress;
_modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
modbusConfig.serialPort);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter,
modbusConfig.parity);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
modbusConfig.baud);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
modbusConfig.dataBits);
_modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
modbusConfig.stopBits);
_modbusDevice->setTimeout(modbusConfig.responseTime);
_modbusDevice->setNumberOfRetries(modbusConfig.numberOfRetries);
if(!_modbusDevice->connectDevice())
{
throw ServoException(_modbusDevice->errorString());
}
}
}
/*************************************************************************************************/
void ModbusMaster::connectionStateChanged(QModbusDevice::State state)
{ {
if(state == QModbusDevice::UnconnectedState)
{
throw ServoException("Connection wasnt prepared");
}
qDebug() << "internal connect";
} }

42
Servo/src/ModbusWrapper.cpp

@ -1,5 +1,47 @@
#include "ModbusWrapper.h" #include "ModbusWrapper.h"
#include <QEventLoop>
ModbusWrapper::ModbusWrapper(QObject* parent) : QObject(parent) ModbusWrapper::ModbusWrapper(QObject* parent) : QObject(parent)
{ {
} }
/*************************************************************************************************/
ModbusWrapper::~ModbusWrapper()
{
workerThread.quit();
workerThread.wait();
}
/*************************************************************************************************/
void ModbusWrapper::init()
{
modbusMaster.moveToThread(&workerThread);
workerThread.setObjectName("workerThread");
workerThread.start();
QEventLoop loop;
connect(&workerThread, &QThread::started, &loop, &QEventLoop::quit);
loop.exec();
connect(this,
&ModbusWrapper::initOrder,
&modbusMaster,
&ModbusMaster::init,
Qt::BlockingQueuedConnection);
connect(this,
&ModbusWrapper::connectOrder,
&modbusMaster,
&ModbusMaster::connectToDevice,
Qt::BlockingQueuedConnection);
emit initOrder();
qDebug() << "init done";
}
/*************************************************************************************************/
void ModbusWrapper::connectToDevice(ModbusConfig modbusConfig)
{
emit connectOrder(modbusConfig);
qDebug() << "connect done";
}

1
ServoApplication.pro

@ -5,3 +5,4 @@ SUBDIRS += \
Test Test
Test.depends += Servo Test.depends += Servo

24
Test/MainWindow.cpp

@ -1,15 +1,37 @@
#include "MainWindow.h" #include "MainWindow.h"
#include "ui_MainWindow.h" #include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
} }
/*************************************************************************************************/
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete ui; delete ui;
} }
/*************************************************************************************************/
void MainWindow::on_connect_clicked()
{
try
{
configDevice.serialPort = "/dev/pts/2";
configDevice.clientAddress = 1;
configDevice.responseTime = 1000;
configDevice.numberOfRetries = 3;
qDebug() << "before init";
modbusWrapper.init();
qDebug() << "after init";
qDebug() << "before connect";
modbusWrapper.connectToDevice(configDevice);
qDebug() << "after connect";
}
catch(ServoException ex)
{
qDebug() << ex.what();
}
}

22
Test/MainWindow.h

@ -3,19 +3,31 @@
#include <QMainWindow> #include <QMainWindow>
#include "ModbusWrapper.h"
#include "ModbusConfig.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow;
}
QT_END_NAMESPACE QT_END_NAMESPACE
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
private:
Ui::MainWindow* ui;
ModbusWrapper modbusWrapper;
ModbusConfig configDevice;
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget* parent = nullptr);
~MainWindow(); ~MainWindow();
private: //uncrustify off
Ui::MainWindow *ui; private slots:
void on_connect_clicked();
//uncrustify on
}; };
#endif // MAINWINDOW_H
#endif //MAINWINDOW_H

27
Test/MainWindow.ui

@ -13,8 +13,31 @@
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>MainWindow</string>
</property> </property>
<widget class="QWidget" name="centralwidget"/> <widget class="QWidget" name="centralwidget">
<widget class="QMenuBar" name="menubar"/> <widget class="QPushButton" name="connect">
<property name="geometry">
<rect>
<x>420</x>
<y>40</y>
<width>89</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>Connect</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/> <widget class="QStatusBar" name="statusbar"/>
</widget> </widget>
<resources/> <resources/>

5
Test/Test.pro

@ -2,6 +2,9 @@ QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += serialbus
QT += serialport
CONFIG += c++11 CONFIG += c++11
# The following define makes your compiler emit warnings if you use # The following define makes your compiler emit warnings if you use
@ -22,6 +25,8 @@ SOURCES += \
HEADERS += \ HEADERS += \
MainWindow.h MainWindow.h
INCLUDEPATH += $$PWD/../Servo/include
FORMS += \ FORMS += \
MainWindow.ui MainWindow.ui

34
Test/main.cpp

@ -1,11 +1,37 @@
#include "MainWindow.h" #include "MainWindow.h"
#include <QtGui>
#include <QApplication> #include <QApplication>
int main(int argc, char *argv[]) class MyApplication : public QApplication
{ {
QApplication a(argc, argv); public:
MyApplication(int& argc, char** argv) :
QApplication(argc, argv)
{
}
virtual ~MyApplication()
{
}
//reimplemented from QApplication so we can throw exceptions in slots
virtual bool notify(QObject* receiver, QEvent* event) {
try {
return QApplication::notify(receiver, event);
}
catch(std::exception& e)
{
;
}
return false;
}
};
int main(int argc, char* argv[]) {
MyApplication app(argc, argv);
MainWindow w; MainWindow w;
w.show(); w.show();
return a.exec();
return app.exec();
} }

Loading…
Cancel
Save