Browse Source

multithread tes2t

master
nasi 4 years ago
parent
commit
8c89ed1b04
  1. 35
      serialConnectionManager/ModbusMaster.cpp
  2. 2
      serialConnectionManager/ModbusMaster.h
  3. 3
      serialConnectionManager/mainwindow.cpp

35
serialConnectionManager/ModbusMaster.cpp

@ -109,8 +109,8 @@ QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize)
{
QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize);
mutex.lock();
if (!unit.valueCount())
modBusAnswerNotReady.wait(&mutex);
if(!freeThread)
modBusAnswerNotReady.wait(&mutex);
mutex.unlock();
@ -124,9 +124,7 @@ QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize)
coils.setBit((unit.value(i)));
}
mutex.lock();
modBusAnswerConsumed.wait(&mutex);
mutex.unlock();
return coils ;
@ -189,27 +187,28 @@ QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registe
{
QFuture<void> future = QtConcurrent::run([this, reply]() {
mutex.lock();
if (!reply->isFinished())
modBusAnswerNotReady.wait(&mutex);
mutex.unlock();
// qDebug() << "reply->isFinished() " << reply->isFinished();
// while(!reply->isFinished())
// {
// qDebug() << "reply->isFinished() " << reply->isFinished();
// qDebug() << "Hello from thread " << QThread::currentThread();
// }
while(!reply->isFinished())
{
qDebug() << "reply->isFinished() " << reply->isFinished();
qDebug() << "Hello from thread " << QThread::currentThread();
}
freeThread=true;
mutex.lock();
if (freeThread)
modBusAnswerNotReady.wakeAll();
mutex.unlock();
if(!checkForError(reply))
{
const QModbusDataUnit unit = reply->result();
return unit;
}
mutex.lock();
modBusAnswerConsumed.wakeAll();
mutex.unlock();
});
}
@ -219,7 +218,7 @@ QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registe
}
// reply->deleteLater();
//reply->deleteLater();
}
bool modBusMaster::checkForError(QModbusReply *_reply)
{

2
serialConnectionManager/ModbusMaster.h

@ -55,8 +55,8 @@ private:
QBitArray _coilFlags;
QVector<quint16> _registerValues;
QWaitCondition modBusAnswerNotReady;
QWaitCondition modBusAnswerConsumed;
QMutex mutex;
bool freeThread=false;
public:
QModbusReply *reply;
};

3
serialConnectionManager/mainwindow.cpp

@ -11,9 +11,6 @@ MainWindow::MainWindow(QWidget *parent)
ui->writeTable->addItem(tr("Discrete Inputs"), 1);
ui->writeTable->addItem(tr("Input Registers"), 2);
ui->writeTable->addItem(tr("Holding Registers"), 3);
QThread *upperModbusThread = new QThread();
modBusObj.moveToThread(upperModbusThread);
connect(&modBusObj, &modBusMaster::modbusConnectionState, this, &MainWindow::connectionState);
}

Loading…
Cancel
Save