Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Reading UART (RS485) with Qt

    Mobile and Embedded
    c++ qt serial port serialport-byte
    5
    8
    1384
    Loading More Posts
    • 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.
    • S
      SheldonC last edited by

      Hello everyone,
      I'm trying to read a byte stream with Qt, later I want to send packets with integers. I can see the data on HTerm, but with Qt I have trouble getting it. The AT90 sends ones a second, about 100 bytes. I wrote the following code:

      #include <QCoreApplication>
      #include <QSerialPort>
      #include <QSerialPortInfo>
      #include <QDebug>
      
      static QSerialPort *serial;        //serial port
      
      static void open_serial();
      static void close_serial();
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          serial = new QSerialPort;
          open_serial();
      
          QByteArray data_in;                 //incoming data
      
          for(int i = 0; i < 10; i++){
      
              data_in.append(serial->readAll());
              qDebug() << "Empfangene Daten: " << data_in;
              //data_in.clear();
          }
      
          close_serial();
          qDebug() << "Serial closed";
      
          return a.exec();
      }
      
      static void open_serial()
      {
          serial->setPortName("COM14");
          serial->setBaudRate(QSerialPort::Baud57600);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
      
          //connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
      
      
      
          if (serial->open(QIODevice::ReadWrite))
          {
              qDebug() << "Connected AT90CAN\n";
          } else
          {
              qDebug() << "can not connect AT90CAN\n";
          }
      }
      
      static void close_serial()
      {
          serial->close();
      }
      

      And I get:

      Connected AT90CAN
      
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Empfangene Daten: ""
      Serial closed
      

      What am I doing wrong? Thanks for your help.

      Pablo J. Rogina 1 Reply Last reply Reply Quote 0
      • Pablo J. Rogina
        Pablo J. Rogina @SheldonC last edited by

        @SheldonC said in Reading UART (RS485) with Qt:

        What am I doing wrong?

        Not using QSerialPort with Qt signal & slots approach (event-driven way).
        Please take a look at serial terminal example where you'll see how data is read when it is available

        When the serial port receives new data, the signal readyRead() is emitted, and that signal is connected to the MainWindow::readData() slot

        void MainWindow::readData() {
            const QByteArray data = m_serial->readAll();
            m_console->putData(data);
        }
        

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 3
        • S
          SheldonC last edited by

          Hi,
          I am still experiencing the same problem, in the meantime I switched to the terminal example and installed a second Qt on a Linux virtual box. But that does not work with the UART either.
          On Windows I can connect, but I get no data and on Linux comes "permission denied" as soon as I try to connect. Does anyone have a solution to either problem?

          Pablo J. Rogina 1 Reply Last reply Reply Quote 0
          • Pablo J. Rogina
            Pablo J. Rogina @SheldonC last edited by

            @SheldonC said in Reading UART (RS485) with Qt:

            I am still experiencing the same problem,

            Have you changed your code to use signals & slots?

            Linux comes "permission denied" as soon as I try to connect

            Does the user you run the Qt example have the proper rights to access the serial port? Check this answer just in case

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 1
            • S
              SheldonC last edited by

              Thank you, it works with Linux now.
              With Windows it doesn't work with signals and slots either.

              aha_1980 1 Reply Last reply Reply Quote 0
              • aha_1980
                aha_1980 Lifetime Qt Champion @SheldonC last edited by

                @SheldonC

                With Windows it doesn't work with signals and slots either.

                Can you show your code?

                Qt has to stay free or it will die.

                1 Reply Last reply Reply Quote 0
                • R
                  RunThiner last edited by

                  @SheldonC said in Reading UART (RS485) with Qt:

                  serial

                  you need make sure the serial buffer has data before you call the serial->readAll():
                  int bytes=serial->bytesAvailable();
                  if(bytes>0){
                  data_in=serial->readAll();
                  do anything...
                  }

                  by the way, you can use the signal-slot without MainWindow, it is work well:
                  class SerialMng: public QThread{
                  signal:
                  void gotData(QByteArray);
                  protect:
                  void run(){
                  while(1){
                  int bytes=serial->bytesAvailable();
                  if(bytes>0){
                  QByteArray dat=serial->readAll();
                  emit gotData(dat);
                  }
                  };
                  }
                  };
                  class SerialData{
                  slots:
                  void procData(QByteArray dat){
                  do anything...
                  }
                  };
                  in main:
                  SerialMng *mng=new SerialMng();
                  mng->start();

                  SerialData *dat=new SerialData();
                  connect(mng,SIGNAL(gotData(QByteArray)),dat,SLOT(procData(QByteArray));

                  SGaist 1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion @RunThiner last edited by

                    @RunThiner There's no need for such a loop. QSerialPort already provides an asynchronous API and if you really want to wait for data to be available, there's a blocking API for that as shown in the QSerialPort details.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 2
                    • First post
                      Last post