serialport read not working(no errors)
-
Hi,
im wondering if the following has a problem:private: Ui::MainWindow *ui; QLabel *label; QSerialPort *m_serial = nullptr; };
Since the QSerialPort is a seperate class in a private window, would that be a problem when trying to display the read values with the following code?
connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::inputGet); connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData); ... serial.setPortName(portName); // replace with the name of the serial port serial.setBaudRate(QSerialPort::Baud19200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); if (serial.open(QIODevice::ReadWrite)) { const QByteArray data = serial.readAll(); plswork = data; ui->labTest2->setText(plswork); QString resultat = "data received"; std::cout << resultat.toStdString() << std::endl; std::cout << lol.toStdString() << std::endl; }else { QString err = "we have a problem here"; std::cout << err.toStdString() << std::endl;
The output is the following:
data received
(blank) -
Hi,
im wondering if the following has a problem:private: Ui::MainWindow *ui; QLabel *label; QSerialPort *m_serial = nullptr; };
Since the QSerialPort is a seperate class in a private window, would that be a problem when trying to display the read values with the following code?
connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::inputGet); connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData); ... serial.setPortName(portName); // replace with the name of the serial port serial.setBaudRate(QSerialPort::Baud19200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); if (serial.open(QIODevice::ReadWrite)) { const QByteArray data = serial.readAll(); plswork = data; ui->labTest2->setText(plswork); QString resultat = "data received"; std::cout << resultat.toStdString() << std::endl; std::cout << lol.toStdString() << std::endl; }else { QString err = "we have a problem here"; std::cout << err.toStdString() << std::endl;
The output is the following:
data received
(blank)serial.setPortName(portName); // replace with the name of the serial port serial.setBaudRate(QSerialPort::Baud19200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); if (serial.open(QIODevice::ReadWrite)) { qDebug()<<"port Open"; } else { qDebug()<<"Port OpenError"; } if(serial.waitForBytesWritten(2000)){//it waits to bytes written while(serial.waitForReadyRead(1000)){//it waits to read const QByteArray data = serial.readAll(); } }
-
Hi,
im wondering if the following has a problem:private: Ui::MainWindow *ui; QLabel *label; QSerialPort *m_serial = nullptr; };
Since the QSerialPort is a seperate class in a private window, would that be a problem when trying to display the read values with the following code?
connect(ui->lineEdit, &QLineEdit::textChanged, this, &MainWindow::inputGet); connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData); ... serial.setPortName(portName); // replace with the name of the serial port serial.setBaudRate(QSerialPort::Baud19200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); if (serial.open(QIODevice::ReadWrite)) { const QByteArray data = serial.readAll(); plswork = data; ui->labTest2->setText(plswork); QString resultat = "data received"; std::cout << resultat.toStdString() << std::endl; std::cout << lol.toStdString() << std::endl; }else { QString err = "we have a problem here"; std::cout << err.toStdString() << std::endl;
The output is the following:
data received
(blank)@agmar said in serialport read not working(no errors):
if (serial.open(QIODevice::ReadWrite)) {
const QByteArray data = serial.readAll();Please read documentation.
This simply can't work. Calling readAll does not mean that data was already received.
Proper way to implement is to use signals slots: connect a slot to https://doc.qt.io/qt-6/qiodevice.html#readyRead signal of your QSerialPort instance and call readAll there.
There are also examples showing how to do it: https://doc.qt.io/qt-6/qtserialport-examples.html -
serial.setPortName(portName); // replace with the name of the serial port serial.setBaudRate(QSerialPort::Baud19200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); if (serial.open(QIODevice::ReadWrite)) { qDebug()<<"port Open"; } else { qDebug()<<"Port OpenError"; } if(serial.waitForBytesWritten(2000)){//it waits to bytes written while(serial.waitForReadyRead(1000)){//it waits to read const QByteArray data = serial.readAll(); } }
@agmar Please use proper coding tags around all your code snippets.
That said, you do not show how you initialize nor configure
m_serial
.With the code you posted, you are likely not using
m_serial
at all, or are trying to access the same device twice which should fail.Qt is an asynchronous framework, you should first learn how it works especially with regards to serial ports.
Please take the time to properly read and understand the links @jsulm provided.
-
serial.setPortName(portName); // replace with the name of the serial port serial.setBaudRate(QSerialPort::Baud19200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); if (serial.open(QIODevice::ReadWrite)) { qDebug()<<"port Open"; } else { qDebug()<<"Port OpenError"; } if(serial.waitForBytesWritten(2000)){//it waits to bytes written while(serial.waitForReadyRead(1000)){//it waits to read const QByteArray data = serial.readAll(); } }
@Emre-MUTLU from my understanding, this is blocking code isnt it? this is not optimal and usable in a real time application if that's the case.
I did read in the documentation that readAll() would allow asynchronous operation, is there anyway to rewrite that to provide async operation? -
@Emre-MUTLU from my understanding, this is blocking code isnt it? this is not optimal and usable in a real time application if that's the case.
I did read in the documentation that readAll() would allow asynchronous operation, is there anyway to rewrite that to provide async operation? -
@agmar said in serialport read not working(no errors):
if (serial.open(QIODevice::ReadWrite)) {
const QByteArray data = serial.readAll();Please read documentation.
This simply can't work. Calling readAll does not mean that data was already received.
Proper way to implement is to use signals slots: connect a slot to https://doc.qt.io/qt-6/qiodevice.html#readyRead signal of your QSerialPort instance and call readAll there.
There are also examples showing how to do it: https://doc.qt.io/qt-6/qtserialport-examples.html@jsulm I have an MCU connected to to the serial port that sends back what i received(tested,works) , and i did read the documentation, but its only useful if you already know how QT works and i know about the terminal and enumerator ,blocking sender and receiver examples, they're not clear to me...
this is the whole function:
void MainWindow::sendMessage()sd
{
QSerialPort serial;
serial.setPortName(portNsame); // replace with the name of the serial port
serial.setBaudRate(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if (serial.open(QIODevice::ReadWrite)) {
QString message = ui->lineEdit->text();
std::cout << message.toStdString() << std::endl;
QByteArray messageData = message.toUtf8();
std::cout << messageData.toStdString() << std::endl;
serial.write(message.toUtf8());
serial.waitForBytesWritten(-1);
serial.close();}
serial.setPortName(portName); // replace with the name of the serial port
serial.setBaudRate(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if (serial.open(QIODevice::ReadWrite)) {const QByteArray data = serial.readAll();
lol = data;
ui->labTest2->setText(lol);
QString resultat = "data received";std::cout << resultat.toStdString() << std::endl;
std::cout << lol.toStdString() << std::endl;}else {
QString erri = "we have a problem here";
std::cout << erri.toStdString() << std::endl;}
}is the
connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
not valid? -
@jsulm I have an MCU connected to to the serial port that sends back what i received(tested,works) , and i did read the documentation, but its only useful if you already know how QT works and i know about the terminal and enumerator ,blocking sender and receiver examples, they're not clear to me...
this is the whole function:
void MainWindow::sendMessage()sd
{
QSerialPort serial;
serial.setPortName(portNsame); // replace with the name of the serial port
serial.setBaudRate(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if (serial.open(QIODevice::ReadWrite)) {
QString message = ui->lineEdit->text();
std::cout << message.toStdString() << std::endl;
QByteArray messageData = message.toUtf8();
std::cout << messageData.toStdString() << std::endl;
serial.write(message.toUtf8());
serial.waitForBytesWritten(-1);
serial.close();}
serial.setPortName(portName); // replace with the name of the serial port
serial.setBaudRate(QSerialPort::Baud19200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if (serial.open(QIODevice::ReadWrite)) {const QByteArray data = serial.readAll();
lol = data;
ui->labTest2->setText(lol);
QString resultat = "data received";std::cout << resultat.toStdString() << std::endl;
std::cout << lol.toStdString() << std::endl;}else {
QString erri = "we have a problem here";
std::cout << erri.toStdString() << std::endl;}
}is the
connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
not valid?@agmar
Theconnect()
looks right [edit, No, see end of my post?]. But not theif (serial.open(QIODevice::ReadWrite)) { const QByteArray data = serial.readAll();
All
readAll()
s belong in theMainWindow::readData()
slot. And don't forget that can/will be called multiple times, and each time there is a only a guarantee that at least 1 byte is ready to read, not more than that. So you may have to buffer the received data.As a separate issue. You seem to have one
QSerialPort serial;
local variable insendMessage()
, and some otherserial
used outside that. You open and close serial port for each read or write?? You are supposed to have one serial port open across a number of/all read/writes.Furthermore, that actually seems to make
connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
wrong, what is
m_serial
here?? Elsewhere you useserial
. I don't know what you are up to. Your application should (presumably) have just one instance ofQSerialPort
, with its settings performed and opened only once. -
@agmar Please use proper coding tags around all your code snippets.
That said, you do not show how you initialize nor configure
m_serial
.With the code you posted, you are likely not using
m_serial
at all, or are trying to access the same device twice which should fail.Qt is an asynchronous framework, you should first learn how it works especially with regards to serial ports.
Please take the time to properly read and understand the links @jsulm provided.
-
@SGaist How do i use proper coding tags all around my code snippets? i have no idea how to do it...
@agmar
When you type a post here you have a "toolbar" of icons above where you type, right? The one which looks like </> has a tooltip of Code and puts tags around whatever you have selected, or put lines of just 3 backtick characters```
before & after your snippet. -
@agmar
When you type a post here you have a "toolbar" of icons above where you type, right? The one which looks like </> has a tooltip of Code and puts tags around whatever you have selected, or put lines of just 3 backtick characters```
before & after your snippet. -
@JonB
I tried making it into serial, but this is what the compiler kept throwing at me, and thanks for the info earlier!
@agmar
I don't know what to say. This is straightforward C++ stuff. You are not supposed to just change things without understanding.Your code seems to have at least 3
QSerialPort
declarations as far as I can see, one form_serial
and 2 forserial
. I don't know why you would want any more than just one. Whatever you need to understand and sort out your code. -
@agmar
I don't know what to say. This is straightforward C++ stuff. You are not supposed to just change things without understanding.Your code seems to have at least 3
QSerialPort
declarations as far as I can see, one form_serial
and 2 forserial
. I don't know why you would want any more than just one. Whatever you need to understand and sort out your code.@JonB
I know, but to understand and learn you need to change, and if i need to understand everything to change anything, how will i ever be able to understand in the first place(catch22)?As for m_serial, that was used in the terminal example and just 'serial' in the other, this code is made from pieces of example code i found, too bad its not commented...
-
@JonB
I know, but to understand and learn you need to change, and if i need to understand everything to change anything, how will i ever be able to understand in the first place(catch22)?As for m_serial, that was used in the terminal example and just 'serial' in the other, this code is made from pieces of example code i found, too bad its not commented...
@agmar
My guess is you would just use them_serial
everywhere, like I said I don't think you will want to have any more that oneQSerialPort
instance.Sorry, but if you take bits of code from different places you have to understand how it works to get it right.