Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSerialPort interface with arduino problem



  • Hi, Im facing several problems to interact between qt and arduino.

    1. Is it normal that we cant open arduino serial monitor right after we run the qt code? It means i cant open arduino serial monitor and qt window (after run) at the same time, that makes me failed to observe the character send from qt to arduino.
      Thanks in advance for the help.
    #include <QCoreApplication>
    #include <QSerialPort>
    #include <QDebug>
    #include <iostream>
    
    QSerialPort serial;
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        serial.setPortName("com6");
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
        serial.open(QIODevice::ReadWrite);
        serial.write("NOT GOOD");
        while(1)
        {
            if(serial.bytesAvailable()>0 ||  serial.waitForReadyRead(1))
            {    serial.write("NOT GOOD");
                QByteArray ba;
                ba=serial.readAll();
                serial.write(ba);
                qDebug()<<ba;
    
            }
        }
    
        serial.close();
        return a.exec();
    }
    

    [added code tags mrjj]


  • Lifetime Qt Champion

    Hi @Mashirotham, welcome!

    First of all, serial ports can only be opened by one application. The only way to debug is using serial port monitors that operate at driver level.

    Regarding your code: You need to use Signals&Slots to work with serial ports. Please have a look at the examples, e.g. the Terminal: ttp://doc.qt.io/qt-5/qtserialport-terminal-example.html

    Regards


  • Lifetime Qt Champion

    Hi
    You make an infinite loop with the while(1) so Qt app can never quit and hence the serialport is
    "in use"/blocked for other apps.
    it will never reach serial.close();



  • @mrjj @aha_1980 ok thanks! will try later. really appreciate for the help.


Log in to reply