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

QT reading Serialport of Arduino



  • Hi. all. i'm stuck in reading some string from arduino. The Serial Port didn't open automatically when I debug first time in QT. But when I open the serial monitor from Arduino IDE, then I close it, then I debug again in QT, then the arduino begin to send its string. But when I upload the code to arduino again, then I debug the QT, it didn't sent anything. It seems my arduino didn't open automatically of its serial port. How can I debug the QT without open the serial monitor from arduino ide in the first?

    This is my simple code

    //================ Arduino Code ==============
    void setup()
    {
        Serial.begin(9600);
        pinMode (led, OUTPUT);
    }  
    
    void loop()
    {
        Serial.println ("test");
    }
    

    and this is QT Code

    SerialPortManager::SerialPortManager(QObject *parent) :
        QObject(parent)
    {
       arduino = new QSerialPort(this);
       connect(arduino, SIGNAL(readyRead()), this, SLOT(readData()));
    }
    
    void SerialPortManager::openSerialPort()
    {
       arduino->setPortName("COM8");
       arduino->open(QIODevice::ReadWrite);
       arduino->setBaudRate(QSerialPort::Baud9600);
       arduino->setDataBits(QSerialPort::Data8);
       arduino->setParity(QSerialPort::NoParity);
       arduino->setStopBits(QSerialPort::OneStop);
       arduino->setFlowControl(QSerialPort::NoFlowControl);
    }
    
    void SerialPortManager::readData()
    {
        QByteArray data = arduino->readAll();
    //    console->putData(data);
        qDebug() << "UART:" << data;
    }
    


  • Hai everyone. I found some clues about this topic. The serial communication have to enable the DTR (Data Terminal Ready). So this is my current code that I used

    void SerialPortManager::openSerialPort()
    {
       arduino->setPortName("COM8");
       arduino->open(QIODevice::ReadWrite);
       arduino->setBaudRate(QSerialPort::Baud9600);
       arduino->setDataBits(QSerialPort::Data8);
       arduino->setParity(QSerialPort::NoParity);
       arduino->setStopBits(QSerialPort::OneStop);
       arduino->setFlowControl(QSerialPort::NoFlowControl);
       arduino->setDataTerminalReady(true);
    }
    

    Actually, I don't know why its working, but some of tutorial it's not a problem without writing setDataTerminalReady


  • Lifetime Qt Champion

    @mimamrafi said in QT reading Serialport of Arduino:

    The Serial Port didn't open automatically

    What do you mean? Do you mean arduino->open(QIODevice::ReadWrite); returns false (you should check the return value!)?


  • Moderators

    @mimamrafi said in QT reading Serialport of Arduino:

    void SerialPortManager::openSerialPort()
    {
    arduino->setPortName("COM8");
    arduino->open(QIODevice::ReadWrite);
    arduino->setBaudRate(QSerialPort::Baud9600);
    arduino->setDataBits(QSerialPort::Data8);
    arduino->setParity(QSerialPort::NoParity);
    arduino->setStopBits(QSerialPort::OneStop);
    arduino->setFlowControl(QSerialPort::NoFlowControl);
    }

    you have to open the serial port after setting the parameters.



  • @jsulm yes, it return false. but after I open the serial monitor in Arduino IDE, then I close the serial monitor in arduino ide, then I debug the qt. now it return true. How can I debug the QT without open the serial monitor in arduino ide in the first time?


  • Lifetime Qt Champion

    @mimamrafi Please read what @J-Hilk wrote



  • @J-Hilk it is the command to open the serial port right?

    arduino->open(QIODevice::ReadWrite);
    

  • Lifetime Qt Champion

    @mimamrafi said in QT reading Serialport of Arduino:

    it is the command to open the serial port right?

    Yes, it should be AFTER you set the parameters (like setFlowControl).



  • @jsulm yes, I have been open it and the output is true, but my issue is the arduino send nothing to serial when I debug the QT first. If I open the serial monitor and close it again then debug the QT, it works properly


  • Lifetime Qt Champion

    @mimamrafi I don't know how your Arduino side is working, so can't comment


  • Moderators

    @mimamrafi said in QT reading Serialport of Arduino:

    yes, I have been open it and the output is true

    yes, thats obvious, but did you open it after setting the settings ? e.g:

    void SerialPortManager::openSerialPort()
    {
    arduino->setPortName("COM8");
    arduino->setBaudRate(QSerialPort::Baud9600);
    arduino->setDataBits(QSerialPort::Data8);
    arduino->setParity(QSerialPort::NoParity);
    arduino->setStopBits(QSerialPort::OneStop);
    arduino->setFlowControl(QSerialPort::NoFlowControl);
    
    arduino->open(QIODevice::ReadWrite);
    }
    

    assuming of course, those are the same settings as the other program uses.



  • @jsulm I only send

    Serial.println ("test"); 
    

    in void loop



  • @J-Hilk Hey, I've got a clue. I'm using Arduino leonardo, I don't know why it didn't send the serial to QT without open the serial monitor. When I try Arduino UNO, it works properly without open the serial monitor first


  • Moderators

    @mimamrafi try adding a sleep/delay at the end of your Arduino loop, see if that improves things.


  • Qt Champions 2020

    @jsulm said in QT reading Serialport of Arduino:

    Yes, it should be AFTER you set the parameters (like setFlowControl).

    No. It is irrelevant.


  • Moderators

    @kuzulis I was about to vehemently deny this, but then I noticed the setters have boolean return type.

    I was only going on the notes from https://doc.qt.io/qt-5/qserialport.html#open
    that says it sets the settings during open().

    did this change in one of the newer versions? apparently not. At least not as far back as 5.12



  • @J-Hilk No, it still print nothing. Maybe, I will switch to arduino uno. It's hardware issue, not the QT. Thanks for help



  • Hai everyone. I found some clues about this topic. The serial communication have to enable the DTR (Data Terminal Ready). So this is my current code that I used

    void SerialPortManager::openSerialPort()
    {
       arduino->setPortName("COM8");
       arduino->open(QIODevice::ReadWrite);
       arduino->setBaudRate(QSerialPort::Baud9600);
       arduino->setDataBits(QSerialPort::Data8);
       arduino->setParity(QSerialPort::NoParity);
       arduino->setStopBits(QSerialPort::OneStop);
       arduino->setFlowControl(QSerialPort::NoFlowControl);
       arduino->setDataTerminalReady(true);
    }
    

    Actually, I don't know why its working, but some of tutorial it's not a problem without writing setDataTerminalReady


Log in to reply