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
QtWS25 Last Chance

Receive Data QserialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt creatorc++qserialportreadyread
10 Posts 4 Posters 1.1k 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 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;
    }

    jsulmJ 1 Reply Last reply
    0
    • M manel.sam

      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;
      }

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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 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

        JonBJ 1 Reply Last reply
        0
        • M manel.sam

          @jsulm
          Thanks for your feedback,

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #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
          3
          • JonBJ JonB

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

            jsulmJ 1 Reply Last reply
            0
            • M manel.sam

              @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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              2
              • jsulmJ jsulm

                @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 last edited by
                #7

                it is done, and there is no change.

                mrjjM JonBJ 2 Replies Last reply
                0
                • M manel.sam

                  it is done, and there is no change.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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

                    it is done, and there is no change.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #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
                    1
                    • JonBJ JonB

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

                      • Login

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