Browse Source

14000719

master
nasi 4 years ago
parent
commit
7be5f3da82
  1. 20
      serialConnectionManager/ModbusMaster.cpp
  2. 12
      serialConnectionManager/ModbusMaster.h

20
serialConnectionManager/ModbusMaster.cpp

@ -7,8 +7,7 @@
enum { NumColumn = 0, CoilsColumn = 1, HoldingColumn = 2, ColumnCount = 3, RowCount = 10 };
modBusMaster::modBusMaster(QObject *parent) : QObject(parent)
{
m_coils.resize(2);
m_holdingRegisters.resize(2);
}
@ -75,7 +74,7 @@ void modBusMaster::setSingleCoil(int startAddress,bool coilFlag)
{
if (!modbusDevice)
return;
m_coils.setBit(0,coilFlag);
_coilFlags.setBit(0,coilFlag);
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, 1);
}
@ -84,7 +83,7 @@ void modBusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArra
{
if (!modbusDevice)
return;
m_coils = coilFlags;
_coilFlags = coilFlags;
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize);
}
@ -92,7 +91,7 @@ void modBusMaster::setSingleRegister(int startAddress, quint16 registerValue)
{
if (!modbusDevice)
return;
m_holdingRegisters[0]=registerValue;
_registerValues[0]=registerValue;
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, 1);
}
@ -100,7 +99,7 @@ void modBusMaster::setMultipleRegister(int startAddress, quint16 writeSize , QVe
{
if (!modbusDevice)
return;
m_holdingRegisters = registerValues;
_registerValues = registerValues;
writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, writeSize);
}
@ -187,7 +186,7 @@ QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registe
return unit;
}
});
future.waitForFinished();
// future.waitForFinished();
}
else
@ -226,9 +225,9 @@ void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int
qDebug() << " ---- writeUnit.valueCount()" << writeUnit.valueCount();
for (int i = 0; i < writeUnit.valueCount(); i++) {
if (table == QModbusDataUnit::Coils)
writeUnit.setValue(i, m_coils[i + writeUnit.startAddress()]);
writeUnit.setValue(i, _coilFlags[i + writeUnit.startAddress()]);
else
writeUnit.setValue(i, m_holdingRegisters[i + writeUnit.startAddress()]);
writeUnit.setValue(i, _registerValues[i + writeUnit.startAddress()]);
}
if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, _slaveAddress)) {
@ -238,13 +237,14 @@ void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int
{
qDebug() << "reply->isFinished() " << reply->isFinished();
}
qDebug() << "reply->isFinished() after " << reply->isFinished();
if(!checkForError(reply))
{
const QModbusDataUnit unit = reply->result();
}
});
future.waitForFinished();
// future.waitForFinished();
// if (!reply->isFinished()) {
// connect(reply, &QModbusReply::finished, this, [reply]() {

12
serialConnectionManager/ModbusMaster.h

@ -19,41 +19,43 @@ class modBusMaster : public QObject
Q_OBJECT
public:
explicit modBusMaster(QObject *parent = nullptr);
void init();
void open(QString serialPort, int slaveAddress);
void start();
void deinit();
void close();
void stop();
void setSingleCoil(int startAddress, bool coilFlag);
void setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags);
void setSingleRegister(int startAddress, quint16 registerValue);
void setMultipleRegister(int startAddress, quint16 writeSize, QVector<quint16> registerValues);
QBitArray getCoil(int startAddress, quint16 readSize);
QBitArray getInputCoil(int startAddress, quint16 readSize);
QVector<quint16> getInputRegister(int startAddress, quint16 readSize);
QVector<quint16> getHoldingRegister(int startAddress, quint16 readSize);
QModbusDataUnit readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
void writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize);
bool checkForError(QModbusReply *_reply);
signals:
void dataFromClient(QString readedParameter);
void modbusConnectionState(bool state);
public slots:
private:
QModbusReply *lastRequest;
QModbusClient *modbusDevice = nullptr;
QString _serialPort ;
int _slaveAddress;
SettingSerialParameters _modbusSettings;
QBitArray _coilFlags;
QVector<quint16> _registerValues;
public:
QModbusReply *reply;
QBitArray m_coils;
QVector<quint16> m_holdingRegisters;
};
#endif // MODBUSMASTER_H

Loading…
Cancel
Save