Browse Source

14000701 write coil and register worked

master
nasi 4 years ago
parent
commit
5bd09ae8e4
  1. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/mainwindow.o
  2. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/modbusmaster.o
  3. BIN
      build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/serialConnectionManager
  4. 8
      serialConnectionManager/mainwindow.cpp
  5. 12
      serialConnectionManager/modbusmaster.cpp

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/mainwindow.o

Binary file not shown.

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/modbusmaster.o

Binary file not shown.

BIN
build-serialConnectionManager-Desktop_Qt_5_13_1_GCC_64bit-Debug/serialConnectionManager

Binary file not shown.

8
serialConnectionManager/mainwindow.cpp

@ -50,19 +50,23 @@ void MainWindow::printDataFromClient(QString readedData)
void MainWindow::on_writeButton_clicked()
{
uiCoils.resize(2);
uiholdingRegisters.resize(2);
int startAddress = ui->writeAddress->value();
// do not go beyond 10 entries
int numberOfEntries = qMin(ui->writeSize->currentText().toInt(), 10 - startAddress);
switch (ui->writeTable->currentData().toInt()) {
case 0:
qDebug() << "coil " << numberOfEntries ;
if(numberOfEntries>1)
{
uiCoils.setBit(1,ui->coilData1->isChecked());
uiCoils.setBit(2,ui->coilData2->isChecked());
uiCoils.setBit(0,ui->coilData1->isChecked());
uiCoils.setBit(1,ui->coilData2->isChecked());
modBusObj.setMultipleCoil(startAddress, numberOfEntries, uiCoils);
}
else if (numberOfEntries==1)
{
qDebug() << "coil umberOfEntries==1 " << numberOfEntries ;
modBusObj.setSingleCoil(startAddress, ui->coilData1->isChecked());
}
break;

12
serialConnectionManager/modbusmaster.cpp

@ -6,6 +6,8 @@
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);
}
@ -71,8 +73,10 @@ void modBusMaster::setSingleCoil(int startAddress,bool coilFlag)
{
if (!modbusDevice)
return;
m_coils.clear();
m_coils.setBit(1,coilFlag);
qDebug() << " ---- setSingleCoil " ;
m_coils.clearBit(2);
m_coils.setBit(0,coilFlag);
qDebug() << " ---- setSingleCoil coilFlag " <<coilFlag;
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, 1);
}
@ -82,7 +86,6 @@ void modBusMaster::setMultipleCoil(int startAddress, quint16 writeSize, QBitArra
if (!modbusDevice)
return;
m_holdingRegisters.clear();
m_coils.clear();
m_coils = someCoilFlags;
writeRequest(QModbusDataUnit::RegisterType::Coils, startAddress, writeSize);
}
@ -138,7 +141,6 @@ void modBusMaster::getHoldingRegister(int startAddress, quint16 readSize)
void modBusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int startAddress, quint16 readSize)
{
qDebug() << " first of readRequest";
auto table =registerType;
reply=modbusDevice->sendReadRequest(QModbusDataUnit(table, startAddress, readSize), serverId) ;
if (reply)
@ -159,9 +161,11 @@ void modBusMaster::readRequest(QModbusDataUnit::RegisterType registerType, int
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()]);

Loading…
Cancel
Save