0

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
asked Jun 18, 2024 at 11:17
4
  • You don't really show much context for your "code receiving integer" stuff. There will be some delay between the STM32 receiving something and it sending the reply. If you don't account for that, and just instantly check for a response, you might assume you did not get a response. Commented Jun 18, 2024 at 21:50
  • 2
    Forget about Qt app for time being, as a debugging step, run a terminal program on your PC, will you be able to receive what the STM32 sent? Commented Jun 19, 2024 at 0:36
  • And once you made sure, that a stupid terminal works thus really knowing, what the serial settings look like, then you should have a connection between QSerialPort and your code: connect(serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead())); Once you got that, come back here and rephrase your question accordingly Commented Jun 19, 2024 at 12:14
  • char delimiter1[2] = "*"; here and later on you manipulate individual characters, but you use double quotes as if it's a string. Replace " with ' in several places, where you operate on a char level. Also, check raw values you receive, before any processing. char '1' is raw 0x31. Commented Jun 19, 2024 at 12:35

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.