I have used Qt with an Arduino UNO to build a data acquisition system. Now I want to migrate that to Qt with an AVR ATmege32u4 microcontroller.
Please look at the code below. I am reading incoming serial data with Qt triggered by the ReadyRead signal, then printing those data to the Qt debug window. This works fine with Qt+UNO, but not Qt+32u4. I rule out any problems with the 32u4 (e.g. bad chip) because the data are printed fine to the serial port monitor in the Arduino IDE.
My questions are:
(1) why does the program work for one AVR device and not the other?
(2) how can I make the program work for the 32u4 microcontroller?
Arduino code (simplified to highlight the problem):
void setup(){
Serial.begin(115200);
while(!Serial){}
}
void loop(){
// wait for incoming serial data from Qt (code not shown)
// record data
// send data back. In this example, send back some text: "abababab"
for(unsigned int i=0;i<4;i++){
Serial.write{0x61}; // "a"
Serial.write{0x62}; // "b"
}
}
Relevant Qt code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QtSerialPort>
#include <QDataStream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
microcontroller = new QSerialPort(this);
// initialize the serial port (baud rate, parity etc.)
connect(microcontroller, &QSerialPort::readyRead, this, &MainWindow::readData);
void MainWindow::readData(){
serialData += microcontroller->readAll();
qDebug() << serialData;
}