Solved Sending a data to Arduino through serial port using Qt
-
First while has ";" after it so probably you should fix it.
while(!serial.isOpen()) serial.open(QIODevice::ReadWrite);
I usually use
readyRead
singal or I usewaitForBytesWritten
andwaitForReadReady
combination. -
@SGaist They seems to me rather complicated and poorly explained.
-
Hi, I would sugget you to read about how Qt event system works. All Qt IODevice derived classes work asynchronously. You need to use QApplication in order to host its object system. After that you need to change your code so that its not blocking io thread of QSerialPort.
-
@asanka424 Thanks I have solved the problem but this time I have another problem. When I connect the arduino with the usb cable my code works perfectly, but I when I use bluetooth module it does not work correctly. To show what the problem is I will share secreenshot of output with usb cable and bluetooth module.
With USB cable: http://i.hizliresim.com/MpWPPQ.png
With bluetooth: http://i.hizliresim.com/Va6gAr.pngAs you can see when working with bluetooth it sometimes get the data wrong.
Why does it happen?My code is here:
#include <QCoreApplication> #include <iostream> #include <QSerialPort> #include <QDebug> #include <Windows.h> using namespace std; QSerialPort serial; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); serial.setPortName("COM21"); serial.open(QIODevice::ReadWrite); serial.setBaudRate(QSerialPort::Baud115200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); while(!serial.isOpen()) serial.open(QIODevice::ReadWrite); if (serial.isOpen() && serial.isWritable()) { qDebug() << "Serial is open"; QByteArray output; QByteArray input; while(true) { output = "a"; serial.write(output); serial.flush(); serial.waitForBytesWritten(1000); serial.waitForReadyRead(1000); input = serial.readAll(); qDebug()<<input; } } return a.exec(); }
-
The data you receive is not wrong it's just that you don't receive it at the same pace with both devices e.g. the underlying buffer are handled differently. But all in all the data is the same.
-
@SGaist How can I modify my code so that I get the same output?
-
IMO 2 options:
- QSerialPort::readData() - make your program read data of given length
http://doc.qt.io/qt-5/qserialport.html#readData - QSerialPort::bytesAvailable() - check if there is expected number of bytes available
http://doc.qt.io/qt-5/qserialport.html#bytesAvailable
- QSerialPort::readData() - make your program read data of given length
-
If you know the amount of bytes in a message, don't read it until you have more or same available to read. When you read, only read that number of bytes.
-
@michelson That works, thanks. I want to share final version of the code
#include <QCoreApplication> #include <iostream> #include <QSerialPort> #include <QDebug> #include <Windows.h> #include <QElapsedTimer> using namespace std; QSerialPort serial; QElapsedTimer timer; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); serial.setPortName("COM21"); serial.open(QIODevice::ReadWrite); serial.setBaudRate(QSerialPort::Baud115200); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); while(!serial.isOpen()) serial.open(QIODevice::ReadWrite); if (serial.isOpen() && serial.isWritable()) { qDebug() << "Serial is open"; QByteArray output; QByteArray input; while(true) { output = "a"; serial.write(output); serial.flush(); timer.start(); // Sleep(80); qDebug() << timer.elapsed(); serial.waitForBytesWritten(100); serial.waitForReadyRead(100); if(serial.bytesAvailable()>=18) input = serial.read(18); qDebug()<<input; } } return a.exec(); }
-
That while on isOpen is just plain wrong. You already opened the serial port and if for any reason if fails to open you'r going to get stuck in that loop.
Do the check on the first open and act accordingly.
-
@SGaist
I add this option because sometimes the computer cannot connect to the bluetooth module, therefore if it cannot connect the first time I want my program to try connecting bluetooth module again, but anyway I exluded this option, since I am using GUI in my project. -
Then add a "Try Again" button to your GUI. It will be cleaner.
-
@brotify could you show me the IDE code