readyRead serial will NOT work properly
-
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QCoreApplication> #include<QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); serialPort= new QSerialPort(this); serialPort->setPortName("COM3"); serialPort->setBaudRate(QSerialPort::Baud9600); serialPort->setDataBits(QSerialPort::Data8); serialPort->setParity(QSerialPort::NoParity); serialPort->setStopBits(QSerialPort::OneStop); serialPort->setFlowControl(QSerialPort::NoFlowControl); if (serialPort->open(QIODevice::ReadOnly)) { qInfo() << "GOOD"; } else { qInfo() << "BAD"; } connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::handleReadyRead); } void MainWindow::handleReadyRead() { qInfo() << "GOT SOMETHING"; } MainWindow::~MainWindow() { serialPort->disconnect(); delete ui; }
I've been at this for hours, it just refuses to receive anything. Both putty and a different serial reading application both can see the data I'm spamming on the COM3 serial port from my ATMega, but QT just refuses to see it. It does sometimes work, like every 20th try, and then it goes back to not responding. I'll then open putty and it's reading all of the info fine.
if I unplug my ATMega board, or use any COM port not in use, it outputs "bad", if I plug it back in and use COM3, it says "good" (every time) so it's obviously detecting it. Am I doing something wrong to get these unreliable results? Using the latest version 5 release.
If this info helps: every time it works properly (very rare) and I exit the application, it will just hang and stop responding.
-
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QCoreApplication> #include<QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); serialPort= new QSerialPort(this); serialPort->setPortName("COM3"); serialPort->setBaudRate(QSerialPort::Baud9600); serialPort->setDataBits(QSerialPort::Data8); serialPort->setParity(QSerialPort::NoParity); serialPort->setStopBits(QSerialPort::OneStop); serialPort->setFlowControl(QSerialPort::NoFlowControl); if (serialPort->open(QIODevice::ReadOnly)) { qInfo() << "GOOD"; } else { qInfo() << "BAD"; } connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::handleReadyRead); } void MainWindow::handleReadyRead() { qInfo() << "GOT SOMETHING"; } MainWindow::~MainWindow() { serialPort->disconnect(); delete ui; }
I've been at this for hours, it just refuses to receive anything. Both putty and a different serial reading application both can see the data I'm spamming on the COM3 serial port from my ATMega, but QT just refuses to see it. It does sometimes work, like every 20th try, and then it goes back to not responding. I'll then open putty and it's reading all of the info fine.
if I unplug my ATMega board, or use any COM port not in use, it outputs "bad", if I plug it back in and use COM3, it says "good" (every time) so it's obviously detecting it. Am I doing something wrong to get these unreliable results? Using the latest version 5 release.
If this info helps: every time it works properly (very rare) and I exit the application, it will just hang and stop responding.
hi @khhhhhhh and welcome.
first step I would suggest is connection to the error/errorOccured signal of QSerialPort and see if that helps identifying the issue, you should also store the serial port pointer, so you can get the actual errorString instead of only the enum, that the signal provides.
Make sure to do the connect call before you start setting your Connection Parameters
-
hi @khhhhhhh and welcome.
first step I would suggest is connection to the error/errorOccured signal of QSerialPort and see if that helps identifying the issue, you should also store the serial port pointer, so you can get the actual errorString instead of only the enum, that the signal provides.
Make sure to do the connect call before you start setting your Connection Parameters
-
@J-Hilk
I'm getting closer to having it work, however when it does work, the application fails to quit properly when I click the X button, and just hangs, forcing me to terminate studio as well.
-
the data I'm spamming on the COM3 serial port from my ATMega
what interval between sends are we talking here ?
@J-Hilk
The atmega chip is just writing to serial as fast as possible. I'm just trying to get this working properly. Putty and the other applications don't crash.
As for QT not reading the messages, it looks like (I could be wrong) that it was just garbage characters that QT wasn't seeing or was just disregarding altogether as garbage. Since then I've fixed that so they're proper english characters and it looks like it's receiving properly now (again could be wrong) but now the main issue is it's just crashing as soon as I quit.
I would reply faster but I'm limited every 600 seconds due to me being a new user.
-
@J-Hilk
The atmega chip is just writing to serial as fast as possible. I'm just trying to get this working properly. Putty and the other applications don't crash.
As for QT not reading the messages, it looks like (I could be wrong) that it was just garbage characters that QT wasn't seeing or was just disregarding altogether as garbage. Since then I've fixed that so they're proper english characters and it looks like it's receiving properly now (again could be wrong) but now the main issue is it's just crashing as soon as I quit.
I would reply faster but I'm limited every 600 seconds due to me being a new user.
@khhhhhhh said in readyRead serial will NOT work properly:
I would reply faster but I'm limited every 600 seconds due to me being a new user.
the upvote should have fixed that.
Try to delay the sending.
QSerialport is rather high-level API it has to go through all kinds of OS/dll and Qt Layers until your code is executed. Can be that the buffer of the comport is flooded and hundert of thousands signals are emitted (possibly internal of QSerialPort) and therefore compromises the functionality.
-
@khhhhhhh said in readyRead serial will NOT work properly:
I would reply faster but I'm limited every 600 seconds due to me being a new user.
the upvote should have fixed that.
Try to delay the sending.
QSerialport is rather high-level API it has to go through all kinds of OS/dll and Qt Layers until your code is executed. Can be that the buffer of the comport is flooded and hundert of thousands signals are emitted (possibly internal of QSerialPort) and therefore compromises the functionality.
-
Hi,
What happens if your read the data that actually arrived on the port in your slot ?
-
So it looks like the error never went away. If I unplug my microchip, and plug it back in, QT cannot read the serial port no matter how many times I restart the application. HOWEVER, if I start Putty and start listening in on that port (reads flawlessly in putty) and then I exit putty and start my QT application again, then QT is able to read from the COM port. Very weird bug.
Edit: Looks like I was missing two lines:
serialPort->setRequestToSend(true); serialPort->setDataTerminalReady(true);