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.
  • W Offline
    W Offline
    Will_Craig
    wrote on 17 Sept 2019, 04:26 last edited by
    #1

    I've been trying to open a connection between my qt app and an Arduino uno via serial port connection. I have all the correct header files included, ive looked at multiple examples and done exactly as others have and i still get errors for no apparent reason my main problem is with the QIODevice::open() function it will always give an error and i can't quite figure out if i'm being stupid or if i've missed something. Main file cpp, header file, and header files #included are shown in the screenshots:
    2_1568694365578_Screen Shot 2019-09-17 at 2.16.43 pm.png 1_1568694365577_Screen Shot 2019-09-17 at 2.17.01 pm.png 0_1568694365575_Screen Shot 2019-09-17 at 2.17.10 pm.png

    J 1 Reply Last reply 17 Sept 2019, 04:31
    0
    • W Will_Craig
      17 Sept 2019, 04:26

      I've been trying to open a connection between my qt app and an Arduino uno via serial port connection. I have all the correct header files included, ive looked at multiple examples and done exactly as others have and i still get errors for no apparent reason my main problem is with the QIODevice::open() function it will always give an error and i can't quite figure out if i'm being stupid or if i've missed something. Main file cpp, header file, and header files #included are shown in the screenshots:
      2_1568694365578_Screen Shot 2019-09-17 at 2.16.43 pm.png 1_1568694365577_Screen Shot 2019-09-17 at 2.17.01 pm.png 0_1568694365575_Screen Shot 2019-09-17 at 2.17.10 pm.png

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Sept 2019, 04:31 last edited by
      #2

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

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

      S W 2 Replies Last reply 17 Sept 2019, 04:46
      0
      • J jsulm
        17 Sept 2019, 04:31

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

        S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 17 Sept 2019, 04:46 last edited by
        #3

        @jsulm said in Problem with serial port opening:

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

        Won't tell him a thing if it does not compile ;-)

        @Will_Craig not sure but try importing QIODevice. Or use QSerialPort::ReadOnly instead of QIODevice::ReadOnly.

        (Z(:^

        J W 2 Replies Last reply 17 Sept 2019, 04:47
        0
        • S sierdzio
          17 Sept 2019, 04:46

          @jsulm said in Problem with serial port opening:

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

          Won't tell him a thing if it does not compile ;-)

          @Will_Craig not sure but try importing QIODevice. Or use QSerialPort::ReadOnly instead of QIODevice::ReadOnly.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 Sept 2019, 04:47 last edited by
          #4

          @sierdzio said in Problem with serial port opening:

          Won't tell him a thing if it does not compile ;-)

          It does - see the application output in the screen-shots. The error comes from QtCreators code model...

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

          1 Reply Last reply
          1
          • S sierdzio
            17 Sept 2019, 04:46

            @jsulm said in Problem with serial port opening:

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

            Won't tell him a thing if it does not compile ;-)

            @Will_Craig not sure but try importing QIODevice. Or use QSerialPort::ReadOnly instead of QIODevice::ReadOnly.

            W Offline
            W Offline
            Will_Craig
            wrote on 17 Sept 2019, 04:59 last edited by
            #5

            @sierdzio same error when including QIODevice and if i use QSerialPort::ReadOnly instead
            thanks for replying quick :)

            J 1 Reply Last reply 17 Sept 2019, 05:28
            0
            • W Will_Craig
              17 Sept 2019, 04:59

              @sierdzio same error when including QIODevice and if i use QSerialPort::ReadOnly instead
              thanks for replying quick :)

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 17 Sept 2019, 05:28 last edited by
              #6

              @will_craig What does https://doc.qt.io/qt-5/qserialport.html#error-prop tell you?

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

              1 Reply Last reply
              0
              • J jsulm
                17 Sept 2019, 04:31

                @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 17 Sept 2019, 06:48 last edited by
                #7

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

                J 1 Reply Last reply 17 Sept 2019, 07:27
                0
                • W Will_Craig
                  17 Sept 2019, 06:48

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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 17 Sept 2019, 07:27 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 18 Sept 2019, 04:32
                  2
                  • 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 Pablo J. RoginaP 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

                                    Pablo J. RoginaP Offline
                                    Pablo J. RoginaP 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
                                    • Pablo J. RoginaP 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

                                        4/19

                                        17 Sept 2019, 04:47

                                        topic:navigator.unread, 15
                                        • Login

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