UART communication using qt



  • Hi,

    I am trying to write to uart using Qt app. initially I am trying to write on Uart, but I am failing to write.
    I am attaching code:

    QApplication a(argc, argv);
        QFile file("C:/Users/rgummal/Documents/uartTest/read.txt");
        if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
            qDebug()<<"file open error";
        QSerialPort serial;
        QByteArray datas;
        serial.setPortName("COM24");
        if(!serial.setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections))
            qDebug() << serial.errorString();
        if(!serial.setDataBits(QSerialPort::Data8))
            qDebug() << serial.errorString();
        if(!serial.setParity(QSerialPort::NoParity))
            qDebug() << serial.errorString();
        if(!serial.setFlowControl(QSerialPort::NoFlowControl))
            qDebug() << serial.errorString();
        if(!serial.setStopBits(QSerialPort::OneStop))
            qDebug() << serial.errorString();
        if(!serial.open(QIODevice::ReadWrite))
            qDebug() << serial.errorString();
    
        //while(serial.isOpen())
        if(serial.isOpen())
        {
            qDebug()<<"serial port is open...";
            serial.write("hello");
            datas = serial.readAll();
            if(datas.size() == 0)
                qDebug()<<"Arrived data is 0";
    
            //if(!serial.waitForReadyRead(-1)) //block until new data arrives
            // qDebug() << "error: " << serial.errorString();
            else{
                qDebug() << "New data available: " << serial.bytesAvailable();
                for(int i=0;i<datas.size();i++){
                    if(datas.at(i)){
                        qDebug()<<datas[i];
                        QByteArray ba("Hello");
                        serial.write(ba);
                        // serial.write("\r\n"); for enter key
    
                        qDebug() << datas;
                        QTextStream in(&file);
                        in<<datas;
                        file.close();
                    }
                }
            }
        }
        else
            qDebug()<<"serial port open error"<<serial.errorString();
    
        return a.exec();
    

    Port was opening successfully. but i am unable to write, Can anyone please help me.

    Thanks in advance


  • Moderators

    @Apeksha said in UART communication using qt:

    but i am unable to write

    How do you know?
    Do you really have this while loop?
    Also, you're trying to read just after writing. This is not going to work properly as at that time maybe nothing was received from device. You should either use readyRead() signal or use http://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead


  • Qt Champions 2017

    Hi @Apeksha,

    QSerialPort needs the Qt event loop to work.

    In your code, you create a QApplication a(argc, argv);, then all your serial port code follows and at the end you call return a.exec();

    That cannot work. The serial communication has to happen within a.exec(). Please take a look at the examples, e.g. [1] to see how to do this.

    And of course what @jsulm said applies to you also.

    [1] http://doc.qt.io/qt-5/qtserialport-terminal-example.html



  • @jsulm

    Now I have chnaged code, I am using readRead signal, still it's not working.

    #include <QCoreApplication>
    #include <QSerialPort>
    #include <QSerialPortInfo>
    #include <QDebug>
    #include<QFile>
    #include<QApplication>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QFile file("C:/Users/rgummal/Documents/uartTest/read.txt");
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
    qDebug()<<"file open error";
    QSerialPort serial;
    QByteArray datas;
    serial.setPortName("COM24");
    if(!serial.setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections))
    qDebug() << serial.errorString();
    if(!serial.setDataBits(QSerialPort::Data8))
    qDebug() << serial.errorString();
    if(!serial.setParity(QSerialPort::NoParity))
    qDebug() << serial.errorString();
    if(!serial.setFlowControl(QSerialPort::NoFlowControl))
    qDebug() << serial.errorString();
    if(!serial.setStopBits(QSerialPort::OneStop))
    qDebug() << serial.errorString();
    if(!serial.open(QIODevice::ReadWrite))
    qDebug() << serial.errorString();
    QByteArray ba("Hello");
    serial.write(ba);

    //while(serial.isOpen())
    if(serial.isOpen())
    {
       // qDebug() << datas;
    
        qDebug()<<"serial port is open...";
        if(!serial.waitForReadyRead(-1)) //block until new data arrives
             qDebug() << "error: " << serial.errorString();
        datas = serial.readAll();
        qDebug() << datas;
    
        if(datas.size() == 0)
            qDebug()<<"Arrived data is 0";
        else{
            qDebug() << "New data available: " << serial.bytesAvailable();
            for(int i=0;i<datas.size();i++){
                if(datas.at(i)){
                    qDebug()<<datas[i];
    

    // QByteArray ba("Hello");
    // serial.write(ba);
    QTextStream in(&file);
    in<<datas;
    file.close();
    }
    }
    }
    }
    else
    qDebug()<<"serial port open error"<<serial.errorString();

    return a.exec();
    

    }



  • @Apeksha said in UART communication using qt:

    if(!serial.waitForReadyRead(-1))

    this will not timeout, as long as you don't get any data from the other end, this will literally lock your program up.

    and you wait to read, as soon as you open the device.



  • @Apeksha Firstly, most serial communication is terminated with a courage return or a new line or both.

    For example

    QByteArray ba("Hello\r\n");
    serial.write(ba);
    
    The serial device might be expecting an "end of message" signal, which is usually a courage return and a new line.
    

    On the second note, try and create a separate object for your serial communication class, like below :

    SerialData::SerialData(QString port, QObject *parent) :
        QObject(parent)
    {
        mPort = new QSerialPort(this);
        connect(mPort, SIGNAL(readyRead()), SLOT(readData()));
    
        mPort->setPortName(port);
        mPort->setBaudRate(QSerialPort::Baud115200);
        mPort->setDataBits(QSerialPort::Data8);
        mPort->setParity(QSerialPort::NoParity);
        mPort->setStopBits(QSerialPort::OneStop);
        mPort->setFlowControl(QSerialPort::NoFlowControl);
    
        if(mPort->open(QIODevice::ReadWrite)){
            mPort->setTextModeEnabled(true);
            mPort->write("Hello\r\n");
            qDebug() << "Port open at " << mPort->portName();
     }
     else{
            qDebug() << "Failed to open tty port " << mPort->portName();
     }
    }
    
    void SerialData::readData()
    {
        QString data = mPort->readAll();
        qDebug() << "Serial data IN : " << data;
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.