Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Receive Data QserialPort
Forum Update on Monday, May 27th 2025

Receive Data QserialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt creatorc++qserialportreadyread
10 Posts 4 Posters 1.2k Views
  • 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.
  • M Offline
    M Offline
    manel.sam
    wrote on 8 Nov 2022, 12:41 last edited by
    #1

    Hello,

    I'm trying to recieve data from a GPS; here is my code below., but i receive anything

    I don't know where the problem lies

    Thank you.

    serialPort = new QSerialPort(devPath,this);
    serialPort->setBaudRate(115200);
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);
    if (!serialPort->open(QIODevice::ReadWrite))
    {
        log_warning("sensors","failed to open device file \"%s\", GPS measures will be unavailable",qPrintable(serialPort->portName()));
        return;
    }
    
    QObject::connect(this->serialPort,SIGNAL(readyRead()),this,SLOT(readUbxNavPos()));
    QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Gps::handleError);
    pollingTimer->start(200);
    

    }
    void Gps::handleError(QSerialPort::SerialPortError error)
    {
    if (error == QSerialPort::ResourceError){
    qDebug()<<"Handle Error"<<error;
    serialPort->close();
    }
    }
    void Gps::readUbxNavPos()
    {
    rcvData.append(serialPort->readAll());
    qDebug()<<"DataGps"<<rcvData;
    }

    J 1 Reply Last reply 8 Nov 2022, 12:53
    0
    • M manel.sam
      8 Nov 2022, 12:41

      Hello,

      I'm trying to recieve data from a GPS; here is my code below., but i receive anything

      I don't know where the problem lies

      Thank you.

      serialPort = new QSerialPort(devPath,this);
      serialPort->setBaudRate(115200);
      serialPort->setDataBits(QSerialPort::Data8);
      serialPort->setParity(QSerialPort::NoParity);
      serialPort->setStopBits(QSerialPort::OneStop);
      serialPort->setFlowControl(QSerialPort::NoFlowControl);
      if (!serialPort->open(QIODevice::ReadWrite))
      {
          log_warning("sensors","failed to open device file \"%s\", GPS measures will be unavailable",qPrintable(serialPort->portName()));
          return;
      }
      
      QObject::connect(this->serialPort,SIGNAL(readyRead()),this,SLOT(readUbxNavPos()));
      QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Gps::handleError);
      pollingTimer->start(200);
      

      }
      void Gps::handleError(QSerialPort::SerialPortError error)
      {
      if (error == QSerialPort::ResourceError){
      qDebug()<<"Handle Error"<<error;
      serialPort->close();
      }
      }
      void Gps::readUbxNavPos()
      {
      rcvData.append(serialPort->readAll());
      qDebug()<<"DataGps"<<rcvData;
      }

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 8 Nov 2022, 12:53 last edited by
      #2

      @manel-sam said in Receive Data QserialPort:

      void Gps::handleError(QSerialPort::SerialPortError error)
      {
      if (error == QSerialPort::ResourceError){
      qDebug()<<"Handle Error"<<error;
      serialPort->close();
      }

      Do you get any other error here?

      Also, why do you use the old connect style for readyRead()?

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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        manel.sam
        wrote on 8 Nov 2022, 13:07 last edited by
        #3

        @jsulm
        Thanks for your feedback,

        No, I don't have any errors displayed.
        So, Could you please give me the new style of Redyread

        J 1 Reply Last reply 8 Nov 2022, 13:25
        0
        • M manel.sam
          8 Nov 2022, 13:07

          @jsulm
          Thanks for your feedback,

          No, I don't have any errors displayed.
          So, Could you please give me the new style of Redyread

          J Offline
          J Offline
          JonB
          wrote on 8 Nov 2022, 13:25 last edited by JonB 11 Aug 2022, 13:30
          #4

          @manel-sam said in Receive Data QserialPort:

          So, Could you please give me the new style of Redyread

          One of many references is https://wiki.qt.io/New_Signal_Slot_Syntax
          The "new" style is already ten-odd years old, it's really time all these examples on the web with old style got removed/updated, but I guess it's never going to happen... :(

          QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Gps::handleError);

          So either you do know about "new style", or you have added this to your code. I am surprised this line as-is compiles OK, but maybe it does.

          No, I don't have any errors displayed.

          @jsulm asked you "Do you get any other error here?", because your code as shown would not display anything other than a ResourceError, so how would you know?

          M 1 Reply Last reply 8 Nov 2022, 14:07
          3
          • J JonB
            8 Nov 2022, 13:25

            @manel-sam said in Receive Data QserialPort:

            So, Could you please give me the new style of Redyread

            One of many references is https://wiki.qt.io/New_Signal_Slot_Syntax
            The "new" style is already ten-odd years old, it's really time all these examples on the web with old style got removed/updated, but I guess it's never going to happen... :(

            QObject::connect(serialPort, &::QSerialPort::errorOccurred,this,&Gps::handleError);

            So either you do know about "new style", or you have added this to your code. I am surprised this line as-is compiles OK, but maybe it does.

            No, I don't have any errors displayed.

            @jsulm asked you "Do you get any other error here?", because your code as shown would not display anything other than a ResourceError, so how would you know?

            M Offline
            M Offline
            manel.sam
            wrote on 8 Nov 2022, 14:07 last edited by
            #5

            @JonB @jsulm I used the new style of Single , Slot and I checked if the port is yesvert and it is but I still receive nothing and no error is displayed

            J 1 Reply Last reply 8 Nov 2022, 14:55
            0
            • M manel.sam
              8 Nov 2022, 14:07

              @JonB @jsulm I used the new style of Single , Slot and I checked if the port is yesvert and it is but I still receive nothing and no error is displayed

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 8 Nov 2022, 14:55 last edited by
              #6

              @manel-sam said in Receive Data QserialPort:

              and no error is displayed

              You're only checking for one of possible errors here:

              void Gps::handleError(QSerialPort::SerialPortError error)
              {
                  if (error == QSerialPort::ResourceError){
                      qDebug()<<"Handle Error"<<error;
                      serialPort->close();
                  }
              }
              

              That's why I asked whether you get any other errors. Can you change it to:

              void Gps::handleError(QSerialPort::SerialPortError error)
              {
                  qDebug() << "Error occured" << error;
                  if (error == QSerialPort::ResourceError){
                      qDebug()<<"Handle Error"<<error;
                      serialPort->close();
                  }
              }
              

              ?

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

              M 1 Reply Last reply 8 Nov 2022, 15:03
              2
              • J jsulm
                8 Nov 2022, 14:55

                @manel-sam said in Receive Data QserialPort:

                and no error is displayed

                You're only checking for one of possible errors here:

                void Gps::handleError(QSerialPort::SerialPortError error)
                {
                    if (error == QSerialPort::ResourceError){
                        qDebug()<<"Handle Error"<<error;
                        serialPort->close();
                    }
                }
                

                That's why I asked whether you get any other errors. Can you change it to:

                void Gps::handleError(QSerialPort::SerialPortError error)
                {
                    qDebug() << "Error occured" << error;
                    if (error == QSerialPort::ResourceError){
                        qDebug()<<"Handle Error"<<error;
                        serialPort->close();
                    }
                }
                

                ?

                M Offline
                M Offline
                manel.sam
                wrote on 8 Nov 2022, 15:03 last edited by
                #7

                it is done, and there is no change.

                mrjjM J 2 Replies Last reply 8 Nov 2022, 16:01
                0
                • M manel.sam
                  8 Nov 2022, 15:03

                  it is done, and there is no change.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 8 Nov 2022, 16:01 last edited by
                  #8

                  @manel-sam
                  Can it be you need to send it something for the GPS to reply ?

                  1 Reply Last reply
                  1
                  • M manel.sam
                    8 Nov 2022, 15:03

                    it is done, and there is no change.

                    J Offline
                    J Offline
                    JonB
                    wrote on 8 Nov 2022, 16:03 last edited by JonB 11 Aug 2022, 16:07
                    #9

                    @manel-sam
                    Like @mrjj has said, I wonder whether you are actually being sent anything at all over the serial port. It looks like you are not...

                    M 1 Reply Last reply 8 Nov 2022, 16:35
                    1
                    • J JonB
                      8 Nov 2022, 16:03

                      @manel-sam
                      Like @mrjj has said, I wonder whether you are actually being sent anything at all over the serial port. It looks like you are not...

                      M Offline
                      M Offline
                      manel.sam
                      wrote on 8 Nov 2022, 16:35 last edited by
                      #10

                      @JonB @mrjj

                      It's coming back to me. I once had the same problem with an IMU sensor that I could not solve under windows.

                      But once under linux with the same code, I receive the data perfectly.

                      Same thing, I just tested under linux for the GPS, I receive the data but not under Windows.

                      To this day I don't know where the problem comes from

                      1 Reply Last reply
                      0

                      1/10

                      8 Nov 2022, 12:41

                      • Login

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