MainWindow
::MainWindow(QWidget *parent
) , ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(serial, &QSerialPort::bytesWritten,
this, &MainWindow::handleBytesWritten);
connect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialReadyRead);
}
void MainWindow::on_PushButton_clicked()
{
configure serial port
serial port open for readWrite
runCommands()
}
void MainWindow
::writeData (const QByteArray &tx_cmd
) {
serial->write(writeData);
}
void MainWindow
::parseReceivedData(QByteArray receivedData
) {
Based on the command id, data is been processed here.
switch(cmd)
{
case 1:
if (particular_bit == 1)
response = true;
else
response = false;
break;
}
. ....
.....
}
void MainWindow::serialReadyRead()
{
m_readData += serial->readAll();
....
...
QByteArray data
= m_readData.
mid(startOfFrame
+ 1, enfOfFrame
- startOfFrame
- 1);
parseReceivedData(data);
}
void MainWindow::runCommands()
{
1. send command to check device is connected or not
writeData(tx_cmd);
2. If received response is success, then only next command has to be sent
If device is not connected, debug print with failure message and do nothing.
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(serial, &QSerialPort::bytesWritten,
this, &MainWindow::handleBytesWritten);
connect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialReadyRead);
}
void MainWindow::on_PushButton_clicked()
{
configure serial port
serial port open for readWrite
runCommands()
}
void MainWindow::writeData (const QByteArray &tx_cmd)
{
serial->write(writeData);
}
void MainWindow::parseReceivedData(QByteArray receivedData)
{
Based on the command id, data is been processed here.
switch(cmd)
{
case 1:
if (particular_bit == 1)
response = true;
else
response = false;
break;
}
. ....
.....
}
void MainWindow::serialReadyRead()
{
m_readData += serial->readAll();
....
...
QByteArray data = m_readData.mid(startOfFrame + 1, enfOfFrame - startOfFrame - 1);
parseReceivedData(data);
}
void MainWindow::runCommands()
{
1. send command to check device is connected or not
writeData(tx_cmd);
2. If received response is success, then only next command has to be sent
If device is not connected, debug print with failure message and do nothing.
}
To copy to clipboard, switch view to plain text mode
So what should be the solution here to know, received response is successful in runCommands() function. So that I can just decide to send next command or not.
Bookmarks