#include "ModbusMaster.h" #include #include #include #include "QtConcurrent/QtConcurrent" #include "SerialportException.h" 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); } void modBusMaster::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) { _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) modbusDevice->disconnectDevice(); delete modbusDevice; } void modBusMaster::stop() { if (modbusDevice) modbusDevice->disconnectDevice(); emit modbusConnectionState(false); } void modBusMaster::setSingleCoil(int startAddress,bool coilFlag) { if (!modbusDevice) return; m_coils.setBit(0,coilFlag); writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, 1); } void modBusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArray coilFlags) { if (!modbusDevice) return; m_coils = coilFlags; writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize); } void modBusMaster::setSingleRegister(int startAddress, quint16 registerValue) { if (!modbusDevice) return; m_holdingRegisters[0]=registerValue; writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, 1); } void modBusMaster::setMultipleRegister(int startAddress, quint16 writeSize , QVector registerValues) { if (!modbusDevice) return; m_holdingRegisters = registerValues; writeRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, writeSize); } QBitArray modBusMaster::getCoil(int startAddress, quint16 readSize) { QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::Coils, startAddress, readSize); QBitArray coils; coils.resize(2); auto num = unit.valueCount(); coils.resize(static_cast(num)); for (uint i = 0; i < unit.valueCount(); i++) { coils.setBit((unit.value(i))); } return coils ; } QBitArray modBusMaster::getInputCoil(int startAddress, quint16 readSize) { QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::DiscreteInputs, startAddress, readSize); QBitArray coils; coils.resize(2); auto num = unit.valueCount(); coils.resize(static_cast(num)); for (uint i = 0; i < unit.valueCount(); i++) { coils.setBit((unit.value(i))); } return coils ; } QVector modBusMaster::getInputRegister(int startAddress, quint16 readSize) { QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::InputRegisters, startAddress, readSize); QVector registerValues; registerValues.resize(2); auto num = unit.valueCount(); registerValues.resize(static_cast(num)); for (uint i = 0; i < unit.valueCount(); i++) { registerValues[i]=unit.value(i); } return registerValues ; } QVector modBusMaster::getHoldingRegister(int startAddress, quint16 readSize) { // if (!modbusDevice) // return; QModbusDataUnit unit = readRequest(QModbusDataUnit::RegisterType::HoldingRegisters, startAddress, readSize); QVector registerValues; registerValues.resize(2); auto num = unit.valueCount(); registerValues.resize(static_cast(num)); for (uint i = 0; i < unit.valueCount(); i++) { registerValues[i]=unit.value(i); } return registerValues ; } QModbusDataUnit modBusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize) { auto table =registerType; if (auto *reply =modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), _slaveAddress)) { QFuture future = QtConcurrent::run([this, reply]() { qDebug() << "reply->isFinished() " << reply->isFinished(); while(!reply->isFinished()) { qDebug() << "reply->isFinished() " << reply->isFinished(); qDebug() << "Hello from thread " << QThread::currentThread(); } if(!checkForError(reply)) { const QModbusDataUnit unit = reply->result(); return unit; } }); future.waitForFinished(); } else { throw serialPortException(modbusDevice->errorString()); } // reply->deleteLater(); } bool modBusMaster::checkForError(QModbusReply *_reply) { if (_reply->error() == QModbusDevice::NoError) { return false; } else if (_reply->error() == QModbusDevice::ProtocolError) { throw serialPortException(tr("Read response error: %1 (Mobus exception: 0x%2)"). arg(reply->errorString()). arg(reply->rawResult().exceptionCode(), -1, 16)); } else { throw serialPortException(tr("Read response error: %1 (code: 0x%2)"). arg(reply->errorString()). arg(reply->error(), -1, 16)); } } void modBusMaster::writeRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 writeSize) { qDebug() << " ---- setSingleCoil coilFlag writeRequest "; auto table =registerType; Q_ASSERT(startAddress >= 0 && startAddress < 10); QModbusDataUnit writeUnit = QModbusDataUnit(table, startAddress, writeSize); 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()]); else writeUnit.setValue(i, m_holdingRegisters[i + writeUnit.startAddress()]); } if (auto *reply = modbusDevice->sendWriteRequest(writeUnit, _slaveAddress)) { QFuture future = QtConcurrent::run([this, reply]() { while(!reply->isFinished()) { qDebug() << "reply->isFinished() " << reply->isFinished(); } if(!checkForError(reply)) { const QModbusDataUnit unit = reply->result(); } }); future.waitForFinished(); // if (!reply->isFinished()) { // connect(reply, &QModbusReply::finished, this, [reply]() { // if (reply->error() == QModbusDevice::ProtocolError) { // throw serialPortException(tr("Write response error: %1 (Mobus exception: 0x%2)") // .arg(reply->errorString()).arg(reply->rawResult().exceptionCode(), -1, 16)); // } else if (reply->error() != QModbusDevice::NoError) { // throw serialPortException(tr("Write response error: %1 (code: 0x%2)"). // arg(reply->errorString()).arg(reply->error(), -1, 16)); // } // reply->deleteLater(); // }); // } else { //reply->deleteLater(); //} qDebug() << " ---- sreply reply "<< reply->error(); } else { throw serialPortException(tr("Write error: ") + modbusDevice->errorString()); } }