nasicurious
3 years ago
13 changed files with 294 additions and 25 deletions
@ -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
|
@ -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
|
@ -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"; |
|||
} |
|||
|
@ -1,5 +1,47 @@ |
|||
#include "ModbusWrapper.h" |
|||
#include <QEventLoop> |
|||
|
|||
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,15 +1,37 @@ |
|||
#include "MainWindow.h" |
|||
#include "ui_MainWindow.h" |
|||
|
|||
MainWindow::MainWindow(QWidget *parent) |
|||
MainWindow::MainWindow(QWidget* parent) |
|||
: QMainWindow(parent) |
|||
, ui(new Ui::MainWindow) |
|||
{ |
|||
ui->setupUi(this); |
|||
} |
|||
|
|||
/*************************************************************************************************/ |
|||
MainWindow::~MainWindow() |
|||
{ |
|||
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(); |
|||
} |
|||
} |
|||
|
@ -1,11 +1,37 @@ |
|||
#include "MainWindow.h" |
|||
|
|||
#include <QtGui> |
|||
#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; |
|||
w.show(); |
|||
return a.exec(); |
|||
|
|||
return app.exec(); |
|||
} |
|||
|
Loading…
Reference in new issue