|
@ -107,8 +107,14 @@ void modBusMaster::setMultipleRegister(int startAddress, quint16 writeSize , QVe |
|
|
|
|
|
|
|
|
QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) |
|
|
QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); |
|
|
QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); |
|
|
|
|
|
mutex.lock(); |
|
|
|
|
|
if (!unit.valueCount()) |
|
|
|
|
|
modBusAnswerNotReady.wait(&mutex); |
|
|
|
|
|
mutex.unlock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QBitArray coils; |
|
|
QBitArray coils; |
|
|
coils.resize(2); |
|
|
coils.resize(2); |
|
|
auto num = unit.valueCount(); |
|
|
auto num = unit.valueCount(); |
|
@ -117,7 +123,15 @@ QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) |
|
|
{ |
|
|
{ |
|
|
coils.setBit((unit.value(i))); |
|
|
coils.setBit((unit.value(i))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mutex.lock(); |
|
|
|
|
|
modBusAnswerConsumed.wait(&mutex); |
|
|
|
|
|
mutex.unlock(); |
|
|
|
|
|
|
|
|
return coils ; |
|
|
return coils ; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
QBitArray modBusMaster::getInputCoil(int startAddress, quint16 readSize) |
|
|
QBitArray modBusMaster::getInputCoil(int startAddress, quint16 readSize) |
|
@ -174,19 +188,29 @@ QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registe |
|
|
if (auto *reply =modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), _slaveAddress)) |
|
|
if (auto *reply =modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), _slaveAddress)) |
|
|
{ |
|
|
{ |
|
|
QFuture<void> future = QtConcurrent::run([this, reply]() { |
|
|
QFuture<void> future = QtConcurrent::run([this, reply]() { |
|
|
qDebug() << "reply->isFinished() " << reply->isFinished(); |
|
|
|
|
|
while(!reply->isFinished()) |
|
|
mutex.lock(); |
|
|
{ |
|
|
if (!reply->isFinished()) |
|
|
qDebug() << "reply->isFinished() " << reply->isFinished(); |
|
|
modBusAnswerNotReady.wait(&mutex); |
|
|
qDebug() << "Hello from thread " << QThread::currentThread(); |
|
|
mutex.unlock(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// qDebug() << "reply->isFinished() " << reply->isFinished();
|
|
|
|
|
|
// while(!reply->isFinished())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// qDebug() << "reply->isFinished() " << reply->isFinished();
|
|
|
|
|
|
// qDebug() << "Hello from thread " << QThread::currentThread();
|
|
|
|
|
|
// }
|
|
|
if(!checkForError(reply)) |
|
|
if(!checkForError(reply)) |
|
|
{ |
|
|
{ |
|
|
const QModbusDataUnit unit = reply->result(); |
|
|
const QModbusDataUnit unit = reply->result(); |
|
|
return unit; |
|
|
return unit; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mutex.lock(); |
|
|
|
|
|
modBusAnswerConsumed.wakeAll(); |
|
|
|
|
|
mutex.unlock(); |
|
|
}); |
|
|
}); |
|
|
// future.waitForFinished();
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
@ -244,7 +268,7 @@ void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
// future.waitForFinished();
|
|
|
// future.waitForFinished();
|
|
|
|
|
|
|
|
|
// if (!reply->isFinished()) {
|
|
|
// if (!reply->isFinished()) {
|
|
|
// connect(reply, &QModbusReply::finished, this, [reply]() {
|
|
|
// connect(reply, &QModbusReply::finished, this, [reply]() {
|
|
|