I'm trying to click on the read button on my Qt app and read an integer from an STM32 board.
I can send a frame from the Qt app to the STM32 board which is "5\r" and the board reads it normally, but when the board sends the integer it dosen't reach the QT app. This is the code for sending the first frame to the STM32:
send "5\r" to stm32
connect(button2, &QPushButton::clicked, [=]() {
textBrowser->clear();
Uart* uart = Uart::getInstance();
QSerialPort* serialPort = uart->getSerialPort();
char delimiter1[2] = "*";
QByteArray packet1;
packet1.append("5");
// packet1.append(delimiter1);
packet1.append("\r");
qDebug() << "packet1 :"<< packet1 ;
if (serialPort->isOpen() && serialPort->isWritable()) {
qint64 bytesWritten = serialPort->write(packet1);
if (bytesWritten == -1) {
qDebug() << "Error: Failed to write data to serial port";
} else {
qDebug() << bytesWritten << "bytes written to serial port";
textBrowser->append("Request send ...");
}
} else {
qDebug() << "Error: Serial port is not open or not writable";
}
QString style = "color: #AA4A44;"; // Adresse de couleur pour le vert (green)
textBrowser->setStyleSheet(style);
The code that sends the integer to the Qt app:
char data = '1'; // Define a character variable with the value '1'
HAL_UART_Transmit(&UartHandle, (uint8_t *)&data, 1, 500);
The code for receiving the integer that doesn't work:
QString lastResponse = ""; // Initialiser lastResponse à une chaîne vide
QByteArray responseData;
while (serialPort->waitForReadyRead(1000)) {
responseData.append(serialPort->readAll());
}
if (!responseData.isEmpty()) {
lastResponse = QString::fromUtf8(responseData);
textBrowser->clear();
textBrowser->append("recived data :");
qDebug() << "Received data:" << lastResponse;
qDebug() << "Received data:" << responseData;
textBrowser->append(lastResponse);
} else {
qDebug() << "No data received from serial port";
textBrowser->append("No data received from serial port");
}
What is the solution for receiving the integer?
pmacfarlane
4,5023 gold badges14 silver badges35 bronze badges
lang-cpp
connect(serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead()));Once you got that, come back here and rephrase your question accordingly