Browse Source

multithread

design
nasi 3 years ago
parent
commit
804a1b3fe1
  1. 62
      serialConnectionManager/ModbusMaster.cpp
  2. 6
      serialConnectionManager/Modbuswrapper.cpp
  3. 6
      serialConnectionManager/Modbuswrapper.h
  4. 32
      serialConnectionManager/mainwindow.cpp
  5. 2
      serialConnectionManager/mainwindow.h

62
serialConnectionManager/ModbusMaster.cpp

@ -21,52 +21,14 @@ void ModBusMaster::init()
connect(&objsimModbus, &Simulatermodbus::coilIsReady , this, &ModBusMaster::modbusReplyRecievedSimulate);
qDebug() << " modBusMaster::init " <<QThread::currentThreadId() ;
objsimModbus.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 slaveAddress)
{
qDebug() << "imodBusMaster::open " << QThread::currentThreadId() ;
objsimModbus.open(serialPort, slaveAddress);
/* _serialPort = serialPort;
_slaveAddress = slaveAddress;
if (!modbusDevice)
return;
if (modbusDevice->state() != QModbusDevice::ConnectedState)
{
modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
serialPort);
modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter,
_modbusSettings.modbusSetting.parity);
modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
_modbusSettings.modbusSetting.baud);
modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
_modbusSettings.modbusSetting.dataBits);
modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
_modbusSettings.modbusSetting.stopBits);
modbusDevice->setTimeout(_modbusSettings.modbusSetting.responseTime);
modbusDevice->setNumberOfRetries(_modbusSettings.modbusSetting.numberOfRetries);
emit modbusConnectionState(true);
}
if (!modbusDevice->connectDevice())
{
throw serialPortException(modbusDevice->errorString());
} */
}
void ModBusMaster::close()
{
if (modbusDevice)
@ -118,30 +80,8 @@ void ModBusMaster::setMultipleRegister(int startAddress, quint16 writeSize , QVe
QBitArray ModBusMaster::getCoil(int startAddress, quint16 readSize)
{
objsimModbus.readCoil(startAddress,readSize);
return clientCoil;
/*
qDebug() << " QThread::currentThreadId() getCoil befor" <<QThread::currentThreadId() ;
QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize);
qDebug() << " QThread::currentThreadId() getCoil after" <<QThread::currentThreadId() ;
mutex.lock();
if(!freeThread)
modBusAnswerNotReady.wait(&mutex);
mutex.unlock();
QBitArray coils;
coils.resize(2);
auto num = unit.valueCount();
coils.resize(static_cast<int>(num));
for (uint i = 0; i < unit.valueCount(); i++)
{
coils.setBit((unit.value(i)));
}
return coils ;
*/
}
void ModBusMaster::modbusReplyRecieved()

6
serialConnectionManager/Modbuswrapper.cpp

@ -13,7 +13,7 @@ ModbusWrapper::~ModbusWrapper()
workerThread.wait();
}
void ModbusWrapper::initWrapper(QString serialPort, int slaveAddress)
void ModbusWrapper::init(QString serialPort, int slaveAddress)
{
modBusObj.moveToThread(&workerThread);
workerThread.setObjectName("workerThread");
@ -29,12 +29,12 @@ void ModbusWrapper::initWrapper(QString serialPort, int slaveAddress)
emit openConnectionOrder(serialPort, slaveAddress);
}
void ModbusWrapper::stopWrapper()
void ModbusWrapper::stop()
{
emit stopConnectionOrder();
}
QBitArray ModbusWrapper::getCoilWrapper(int startAddress, quint16 numberOfEntries)
QBitArray ModbusWrapper::getCoil(int startAddress, quint16 numberOfEntries)
{
QBitArray coilDataFromClient = emit getCoilOrder(startAddress, numberOfEntries);
return coilDataFromClient;

6
serialConnectionManager/Modbuswrapper.h

@ -10,9 +10,9 @@ class ModbusWrapper : public QObject
public:
explicit ModbusWrapper(QObject *parent = nullptr);
~ModbusWrapper();
void initWrapper(QString serialPort, int slaveAddress);
void stopWrapper();
QBitArray getCoilWrapper( int startAddress, quint16 numberOfEntries);
void init(QString serialPort, int slaveAddress);
void stop();
QBitArray getCoil( int startAddress, quint16 numberOfEntries);
signals:
void openConnectionOrder(QString _serialPort, int _slaveAddress);

32
serialConnectionManager/mainwindow.cpp

@ -14,7 +14,7 @@ MainWindow::MainWindow(QWidget *parent)
ui->writeTable->addItem(tr("Holding Registers"), 3);
// the thread starts the event loop and blocks waiting for events
// connect(modBusObj, &modBusMaster::modbusConnectionState, this, &MainWindow::connectionState);
// connect(modBusObj, &modBusMaster::modbusConnectionState, this, &MainWindow::connectionState);
}
@ -26,15 +26,15 @@ MainWindow::~MainWindow()
void MainWindow::on_connectButton_clicked()
{
qDebug() << " QThread::currentThreadId() MainWindow on_connectButton_clicked" <<QThread::currentThreadId() ;
qDebug() << " QThread::currentThreadId() MainWindow on_connectButton_clicked" <<QThread::currentThreadId() ;
if( ui->connectButton->text()==tr("Disconnect"))
{
modBusWrapperObj.stopWrapper();
modBusWrapper.stop();
}
else
{
qDebug() << "on_connectButton_clicked else ui " << QThread::currentThreadId();
modBusWrapperObj.initWrapper(ui->portEdit->text(),ui->serverEdit->value());
qDebug() << "on_connectButton_clicked else ui " << QThread::currentThreadId();
modBusWrapper.init(ui->portEdit->text(),ui->serverEdit->value());
}
@ -45,14 +45,14 @@ void MainWindow::on_readButton_clicked()
// do not go beyond 10 entries
int numberOfEntries = qMin(ui->readSize->currentText().toInt(), 10 - startAddress);
switch (ui->writeTable->currentData().toInt()) {
case 0: printCoilsDataFromSlave(modBusWrapperObj.getCoilWrapper(startAddress,static_cast<quint16>(numberOfEntries)));
case 0: printCoilsDataFromSlave(modBusWrapper.getCoil(startAddress,static_cast<quint16>(numberOfEntries)));
break;
// case 1: printCoilsDataFromSlave(modBusObj->getInputCoil(startAddress,static_cast<quint16>(numberOfEntries)));
// break;
// case 2: printRegisterDataFromSlave(modBusObj->getInputRegister(startAddress,static_cast<quint16>(numberOfEntries)));
// break;
// case 3: printRegisterDataFromSlave(modBusObj->getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries)));
// break;
// case 1: printCoilsDataFromSlave(modBusObj->getInputCoil(startAddress,static_cast<quint16>(numberOfEntries)));
// break;
// case 2: printRegisterDataFromSlave(modBusObj->getInputRegister(startAddress,static_cast<quint16>(numberOfEntries)));
// break;
// case 3: printRegisterDataFromSlave(modBusObj->getHoldingRegister(startAddress,static_cast<quint16>(numberOfEntries)));
// break;
}
}
@ -100,11 +100,11 @@ void MainWindow::on_writeButton_clicked()
{
uiCoils.setBit(0,ui->coilData1->isChecked());
uiCoils.setBit(1,ui->coilData2->isChecked());
// modBusObj->setMultipleCoil(startAddress, numberOfEntries, uiCoils);
// modBusObj->setMultipleCoil(startAddress, numberOfEntries, uiCoils);
}
else if (numberOfEntries==1)
{
// modBusObj->setSingleCoil(startAddress, ui->coilData1->isChecked());
// modBusObj->setSingleCoil(startAddress, ui->coilData1->isChecked());
}
break;
@ -113,11 +113,11 @@ void MainWindow::on_writeButton_clicked()
{
uiholdingRegisters[0]=ui->textEdit1->toPlainText().toUInt();
uiholdingRegisters[1]=ui->textEdit2->toPlainText().toUInt();
// modBusObj->setMultipleRegister(startAddress, numberOfEntries, uiholdingRegisters);
// modBusObj->setMultipleRegister(startAddress, numberOfEntries, uiholdingRegisters);
}
else if (numberOfEntries==1)
{
// modBusObj->setSingleRegister(startAddress, ui->textEdit1->toPlainText().toUInt());
// modBusObj->setSingleRegister(startAddress, ui->textEdit1->toPlainText().toUInt());
}
break;
}

2
serialConnectionManager/mainwindow.h

@ -27,7 +27,7 @@ private slots:
private:
Ui::MainWindow *ui;
ModbusWrapper modBusWrapperObj;
ModbusWrapper modBusWrapper;
QBitArray uiCoils;
QVector<quint16> uiholdingRegisters;

Loading…
Cancel
Save