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

Problem with serial port opening

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 5.4k 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.
  • jsulmJ jsulm

    @will_craig You should check the return value of open().
    Also, what does https://doc.qt.io/qt-5/qserialport.html#error-prop tell you?

    W Offline
    W Offline
    Will_Craig
    wrote on last edited by
    #7

    @jsulm how do i implement that function? not too familiar with it
    thank you

    jsulmJ 1 Reply Last reply
    0
    • W Will_Craig

      @jsulm how do i implement that function? not too familiar with it
      thank you

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

      @will_craig You call it

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

      after calling open()

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

      W 1 Reply Last reply
      2
      • jsulmJ jsulm

        @will_craig You call it

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

        after calling open()

        W Offline
        W Offline
        Will_Craig
        wrote on 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

        aha_1980A 1 Reply Last reply
        0
        • W Will_Craig

          @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

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 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
          6
          • aha_1980A aha_1980

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

            aha_1980A 1 Reply Last reply
            0
            • W Will_Craig

              @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

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 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
              4
              • aha_1980A aha_1980

                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 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 :)

                aha_1980A 1 Reply Last reply
                2
                • W Will_Craig

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

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 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
                  3
                  • aha_1980A aha_1980

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

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

                    aha_1980A Pablo J. RoginaP 2 Replies Last reply
                    1
                    • W Will_Craig

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

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 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

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

                        Pablo J. RoginaP Offline
                        Pablo J. RoginaP Offline
                        Pablo J. Rogina
                        wrote on 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
                        1
                        • Pablo J. RoginaP Pablo J. Rogina

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

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

                          aha_1980A 1 Reply Last reply
                          0
                          • W Will_Craig

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

                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on 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

                            • Login

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