Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. UART communication using qt
Forum Updated to NodeBB v4.3 + New Features

UART communication using qt

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 5 Posters 6.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Apeksha
    wrote on last edited by VRonin
    #1

    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

    jsulmJ aha_1980A 2 Replies Last reply
    0
    • A Apeksha

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      3
      • A Apeksha

        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

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        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

        Qt has to stay free or it will die.

        1 Reply Last reply
        1
        • jsulmJ jsulm

          @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

          A Offline
          A Offline
          Apeksha
          wrote on last edited by
          #4

          @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();
          

          }

          J.HilkJ 1 Reply Last reply
          0
          • A Apeksha

            @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();
            

            }

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • BonganiB Offline
              BonganiB Offline
              Bongani
              wrote on last edited by Bongani
              #6

              @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;
              }
              
              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved