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. Problem with serial port opening
QtWS25 Last Chance

Problem with serial port opening

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 5.3k 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.
  • J jsulm
    17 Sept 2019, 07:27

    @will_craig You call it

    qDebug() << arduino->error();
    

    after calling open()

    W Offline
    W Offline
    Will_Craig
    wrote on 18 Sept 2019, 04:32 last edited by
    #9

    @jsulm sorry it was coming up with an error when i first tried entering that heres the screenshot of after running the applicaiton it gives no error? real wierd0_1568781167312_Screen Shot 2019-09-18 at 2.32.23 pm.png

    A 1 Reply Last reply 18 Sept 2019, 05:36
    0
    • W Will_Craig
      18 Sept 2019, 04:32

      @jsulm sorry it was coming up with an error when i first tried entering that heres the screenshot of after running the applicaiton it gives no error? real wierd0_1568781167312_Screen Shot 2019-09-18 at 2.32.23 pm.png

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 18 Sept 2019, 05:36 last edited by aha_1980
      #10

      @will_craig Please post code as text, not as picture. That makes it easier for us to copy parts of the code for commenting.

      What do you think should QSerialPort::OpenMode() return? It is not even related to your arduino variable.

      The correct way is:

      if (!arduino->open(QIODevice::ReadWrite)) {
        // error handling
      } else {
        // port successfully opened
      }
      

      As a side note: What should ReadOnly do with a serial port? I'm not even sure that is supported on all platforms. Better use ReadWrite (you don't need to write if you don't want to).

      Regards

      Qt has to stay free or it will die.

      W 1 Reply Last reply 18 Sept 2019, 06:02
      6
      • A aha_1980
        18 Sept 2019, 05:36

        @will_craig Please post code as text, not as picture. That makes it easier for us to copy parts of the code for commenting.

        What do you think should QSerialPort::OpenMode() return? It is not even related to your arduino variable.

        The correct way is:

        if (!arduino->open(QIODevice::ReadWrite)) {
          // error handling
        } else {
          // port successfully opened
        }
        

        As a side note: What should ReadOnly do with a serial port? I'm not even sure that is supported on all platforms. Better use ReadWrite (you don't need to write if you don't want to).

        Regards

        W Offline
        W Offline
        Will_Craig
        wrote on 18 Sept 2019, 06:02 last edited by aha_1980
        #11

        @aha_1980 Sorry haven't used forums that much before but heres the code with the fix you told me. The output reaches the successful section of the if statement and i'm still getting the "No viable conversion" error on line 70 (above) and as noted in code below. The port still recognises that it is "NotOpen" which confuses me.

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            ui->TempSensor->display("----");
            ui->PHSensor->display("----");
            ui->MoistureSensor->display("----");
        
            arduino = new QSerialPort(this);
            arduinoPortName = "cu.usbmodem14201";
            openSerial();
            //readSerial();
        }
        
        MainWindow::~MainWindow()
        {
            if(arduino->isOpen()){arduino->close();}
            delete ui;
        }
        
        void MainWindow::openSerial()
        {
                    arduinoIsAvailable = true;
                    // open and configure the serialport
                    qDebug() << "Found the arduino port...\n" << "Port name used: " << arduinoPortName << "\n";
                    arduino->setPortName(arduinoPortName);
                    arduino->setBaudRate(QSerialPort::Baud9600, QSerialPort::Directions());
                    arduino->setDataBits(QSerialPort::Data8);
                    arduino->setFlowControl(QSerialPort::NoFlowControl);
                    arduino->setParity(QSerialPort::NoParity);
                    arduino->setStopBits(QSerialPort::OneStop);
                    connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
        
                    if(!arduino->open(QIODevice::ReadWrite)){               //error is here
                        qDebug() << "Error Handling Section Reached";
                    }else{
                        qDebug() << "Port opened succesfully...\n";
                    }
                    qDebug() << arduino->error();
                    qDebug() << QSerialPort::OpenMode();
                    /*
                    // give error message if not available
                    QMessageBox MsgBx;
                    MsgBx.setText("Error: Null Connection");
                    MsgBx.exec();
                    */
        }
        

        output:

        Found the arduino port...
         Port name used:  "cu.usbmodem14201" 
        
        Port opened succesfully...
        
        QSerialPort::NoError
        OpenMode( "NotOpen" )
        

        thankyou for you help

        A 1 Reply Last reply 18 Sept 2019, 06:52
        0
        • W Will_Craig
          18 Sept 2019, 06:02

          @aha_1980 Sorry haven't used forums that much before but heres the code with the fix you told me. The output reaches the successful section of the if statement and i'm still getting the "No viable conversion" error on line 70 (above) and as noted in code below. The port still recognises that it is "NotOpen" which confuses me.

          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              ui->TempSensor->display("----");
              ui->PHSensor->display("----");
              ui->MoistureSensor->display("----");
          
              arduino = new QSerialPort(this);
              arduinoPortName = "cu.usbmodem14201";
              openSerial();
              //readSerial();
          }
          
          MainWindow::~MainWindow()
          {
              if(arduino->isOpen()){arduino->close();}
              delete ui;
          }
          
          void MainWindow::openSerial()
          {
                      arduinoIsAvailable = true;
                      // open and configure the serialport
                      qDebug() << "Found the arduino port...\n" << "Port name used: " << arduinoPortName << "\n";
                      arduino->setPortName(arduinoPortName);
                      arduino->setBaudRate(QSerialPort::Baud9600, QSerialPort::Directions());
                      arduino->setDataBits(QSerialPort::Data8);
                      arduino->setFlowControl(QSerialPort::NoFlowControl);
                      arduino->setParity(QSerialPort::NoParity);
                      arduino->setStopBits(QSerialPort::OneStop);
                      connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
          
                      if(!arduino->open(QIODevice::ReadWrite)){               //error is here
                          qDebug() << "Error Handling Section Reached";
                      }else{
                          qDebug() << "Port opened succesfully...\n";
                      }
                      qDebug() << arduino->error();
                      qDebug() << QSerialPort::OpenMode();
                      /*
                      // give error message if not available
                      QMessageBox MsgBx;
                      MsgBx.setText("Error: Null Connection");
                      MsgBx.exec();
                      */
          }
          

          output:

          Found the arduino port...
           Port name used:  "cu.usbmodem14201" 
          
          Port opened succesfully...
          
          QSerialPort::NoError
          OpenMode( "NotOpen" )
          

          thankyou for you help

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 18 Sept 2019, 06:52 last edited by aha_1980
          #12

          Hi @will_craig,

          No problem. We all started some day :)

          f statement and im still getting the "No viable conversion" error on line 70 (above) and as noted in code below.

          That is a Code Model Error, please ignore it in the first place. If your code compiles, all is fine. Have you included <QIODevice> ?

          if(arduino->open(QIODevice::ReadWrite)){  
              qDebug() << "Error Handling Section Reached";
          }else{
              qDebug() << "Port opened succesfully...\n";
          }
          

          You didn't carefully copy my code!

          I wrote ! in the if condition, you completely inverted the branches so it will not work.

          And please put your code in code tags, I'll add them for you now.

          Regards

          Edit: and once again: qDebug() << QSerialPort::OpenMode(); is completely pointless. Remove that line!

          Qt has to stay free or it will die.

          W 1 Reply Last reply 18 Sept 2019, 07:11
          4
          • A aha_1980
            18 Sept 2019, 06:52

            Hi @will_craig,

            No problem. We all started some day :)

            f statement and im still getting the "No viable conversion" error on line 70 (above) and as noted in code below.

            That is a Code Model Error, please ignore it in the first place. If your code compiles, all is fine. Have you included <QIODevice> ?

            if(arduino->open(QIODevice::ReadWrite)){  
                qDebug() << "Error Handling Section Reached";
            }else{
                qDebug() << "Port opened succesfully...\n";
            }
            

            You didn't carefully copy my code!

            I wrote ! in the if condition, you completely inverted the branches so it will not work.

            And please put your code in code tags, I'll add them for you now.

            Regards

            Edit: and once again: qDebug() << QSerialPort::OpenMode(); is completely pointless. Remove that line!

            W Offline
            W Offline
            Will_Craig
            wrote on 18 Sept 2019, 07:11 last edited by Will_Craig
            #13

            @aha_1980 I've added the ! and the code goes to the correct section of the successful opening, i have used the header <QIODevice>, <QSerialPort> and <QSerialPortInfo>.
            So you're saying that even though it gives me the error before compiling as long as it still compiles there is no problem? cause i have been assuming that if that error has come up then the code will not work properly. thanks again and sorry about not using code tags.
            My code now looks like this:

             if(!arduino->open(QIODevice::ReadWrite)){
                            qDebug() << "Error Handling Section Reached";
                        }else{
                            qDebug() << "Port opened succesfully...\n";
                        }
                        qDebug() << arduino->error();
            

            and this is my debug output:

            Found the arduino port...
             Port name used:  "cu.usbmodem14201" 
            
            Port opened succesfully...
            QSerialPort::NoError
            

            Is this correct?, thanks again :)

            A 1 Reply Last reply 18 Sept 2019, 07:15
            2
            • W Will_Craig
              18 Sept 2019, 07:11

              @aha_1980 I've added the ! and the code goes to the correct section of the successful opening, i have used the header <QIODevice>, <QSerialPort> and <QSerialPortInfo>.
              So you're saying that even though it gives me the error before compiling as long as it still compiles there is no problem? cause i have been assuming that if that error has come up then the code will not work properly. thanks again and sorry about not using code tags.
              My code now looks like this:

               if(!arduino->open(QIODevice::ReadWrite)){
                              qDebug() << "Error Handling Section Reached";
                          }else{
                              qDebug() << "Port opened succesfully...\n";
                          }
                          qDebug() << arduino->error();
              

              and this is my debug output:

              Found the arduino port...
               Port name used:  "cu.usbmodem14201" 
              
              Port opened succesfully...
              QSerialPort::NoError
              

              Is this correct?, thanks again :)

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 18 Sept 2019, 07:15 last edited by
              #14

              Hi @will_craig,

              looks good now and should work!

              So you're saying that even though it gives me the error before compiling as long as it still compiles there is no problem?

              Yeah, the Clang Code Model is nice, but sometimes it goes crazy. If it annoys you very much, you can deactivate it by Help > About Plugins > Clang Code Model and restart Creator. Then you use the "old" code model, which has fewer features but works more solid.

              Regards

              Qt has to stay free or it will die.

              W 1 Reply Last reply 18 Sept 2019, 07:28
              3
              • A aha_1980
                18 Sept 2019, 07:15

                Hi @will_craig,

                looks good now and should work!

                So you're saying that even though it gives me the error before compiling as long as it still compiles there is no problem?

                Yeah, the Clang Code Model is nice, but sometimes it goes crazy. If it annoys you very much, you can deactivate it by Help > About Plugins > Clang Code Model and restart Creator. Then you use the "old" code model, which has fewer features but works more solid.

                Regards

                W Offline
                W Offline
                Will_Craig
                wrote on 18 Sept 2019, 07:28 last edited by
                #15

                @aha_1980 thank you so much for the help you're a lagend

                A P 2 Replies Last reply 18 Sept 2019, 07:49
                1
                • W Will_Craig
                  18 Sept 2019, 07:28

                  @aha_1980 thank you so much for the help you're a lagend

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 18 Sept 2019, 07:49 last edited by
                  #16

                  @will_craig You're welcome :)

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0
                  • W Will_Craig
                    18 Sept 2019, 07:28

                    @aha_1980 thank you so much for the help you're a lagend

                    P Offline
                    P Offline
                    Pablo J. Rogina
                    wrote on 19 Sept 2019, 11:51 last edited by
                    #17

                    @will_craig is your issue solved? Then please don't forget to mark your post as such. Thanks.

                    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

                    W 1 Reply Last reply 23 Sept 2019, 02:40
                    1
                    • P Pablo J. Rogina
                      19 Sept 2019, 11:51

                      @will_craig is your issue solved? Then please don't forget to mark your post as such. Thanks.

                      W Offline
                      W Offline
                      Will_Craig
                      wrote on 23 Sept 2019, 02:40 last edited by
                      #18

                      @Pablo-J-Rogina how do i do that?

                      A 1 Reply Last reply 23 Sept 2019, 03:53
                      0
                      • W Will_Craig
                        23 Sept 2019, 02:40

                        @Pablo-J-Rogina how do i do that?

                        A Offline
                        A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on 23 Sept 2019, 03:53 last edited by
                        #19

                        Hi @Will_Craig,

                        with the Topic Tools button, but it seems you already found that.

                        Greetings

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        0

                        18/19

                        23 Sept 2019, 02:40

                        • Login

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