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. Cannot output a value using QSerialPort
Forum Updated to NodeBB v4.3 + New Features

Cannot output a value using QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
55 Posts 8 Posters 4.7k Views 4 Watching
  • 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.
  • S SGaist
    14 Mar 2020, 20:02

    Hi,

    One thing that is missing: you don't check that the open call is successful. You should add that.

    J Offline
    J Offline
    jude.bato
    wrote on 16 Mar 2020, 18:25 last edited by
    #21

    @SGaist I believe I do

    bool arduino_is_available = false;
        QString arduino_uno_port_name;
        foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
            if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()){
                if((serialPortInfo.productIdentifier() == arduino_uno_product_id) && (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id)){
                    arduino_is_available = true;
                    arduino_uno_port_name = serialPortInfo.portName();
                }
            }
        }
    
        if(arduino_is_available)
        {
            qDebug()<<"Found the port \n";
            arduino->setPortName(arduino_uno_port_name);
            arduino->open(QSerialPort::ReadOnly);
            arduino->setBaudRate(QSerialPort::Baud9600);
            arduino->setDataBits(QSerialPort::Data8);
            arduino->setFlowControl(QSerialPort::NoFlowControl);
            arduino->setParity(QSerialPort::NoParity);
            arduino->setStopBits(QSerialPort::OneStop);
            QObject::connect(arduino, SIGNAL(readyRead), this, SLOT(readSerial())); //I know this is wrong, this is what I'm trying to fix
        } else {
            qDebug()<<"Could not find the correct port \n";
            QMessageBox::information(this,"Serial Port Error", "Could not open the serial port");
        }
    }
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 16 Mar 2020, 18:30 last edited by
      #22

      @jude-bato said in Cannot output a value using QSerialPort:

      I believe I do

      No, you don't check the value of QSerialPoort::open(). And you should set the parameter before opening the device.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • P Pablo J. Rogina
        14 Mar 2020, 23:07

        @jude-bato said in Cannot output a value using QSerialPort:

        connect(*arduino, Dialog::readSerial(updateVoltage()),this,readSerial());

        Again, please read the documentation regarding signal and slots. You may want to try this article as well.

        Tip: a typical connect sentence has 4 elements, try to identify such elements within your code...

        Dialog::updateVoltage(parsed_data);

        Do you know why you're calling an static method here?

        J Offline
        J Offline
        jude.bato
        wrote on 16 Mar 2020, 18:38 last edited by
        #23

        @Pablo-J-Rogina I've already tried with the new syntax and I've tried with different elements and I keep getting the expected 1, have 0 output. As for the parsed_data I personally have never used it but I used Vannevar Morgan code as reference and it worked (https://github.com/vannevar-morgan/Qt-Temperature-Sensor/blob/master/DS18B20_Qt/dialog.cpp)

        @Christian-Ehrlicher Then I'm not sure how to go about doing so, I thought that mean to check if the port on the Arduino is open. When debugging I tested throughout my code and was open the Serial Port and read an open port on the Arduino, the only issue is I can't read anything from it

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 Mar 2020, 18:59 last edited by
          #24

          How did you test that ?

          As for the call to open: again, always check that it succeeded. You might be trying to use an already opened device or you don't have the rights to open said device. Therefore, add that check before going further.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 18 Mar 2020, 23:44
          2
          • S SGaist
            16 Mar 2020, 18:59

            How did you test that ?

            As for the call to open: again, always check that it succeeded. You might be trying to use an already opened device or you don't have the rights to open said device. Therefore, add that check before going further.

            J Offline
            J Offline
            jude.bato
            wrote on 18 Mar 2020, 23:44 last edited by
            #25

            @SGaist I have

            Dialog::~Dialog()
            {
                if(arduino->isOpen())
                {
                    arduino->close();
                }
                delete ui;
            }
            

            Would this satisfy the QSerialPort::open()?

            Also would I need to use connect? I change that line to just readSerial(); but I'm not able to implement void Dialog::updateVoltage(QString sensor_reading) into if(arduino_is_available)

            J 1 Reply Last reply 19 Mar 2020, 11:42
            0
            • J jude.bato
              18 Mar 2020, 23:44

              @SGaist I have

              Dialog::~Dialog()
              {
                  if(arduino->isOpen())
                  {
                      arduino->close();
                  }
                  delete ui;
              }
              

              Would this satisfy the QSerialPort::open()?

              Also would I need to use connect? I change that line to just readSerial(); but I'm not able to implement void Dialog::updateVoltage(QString sensor_reading) into if(arduino_is_available)

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 19 Mar 2020, 11:42 last edited by jsulm
              #26

              @jude-bato said in Cannot output a value using QSerialPort:

              Would this satisfy the QSerialPort::open()?

              Of course not, it does not have anything to do with open().

              if(!arduino->open(QSerialPort::ReadOnly)) {
                  qDebug() << "Opening serial port failed";
                  qDebug() << arduino->error() << arduino->errorString();
              }
              

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

              J 1 Reply Last reply 19 Mar 2020, 15:16
              5
              • J jsulm
                19 Mar 2020, 11:42

                @jude-bato said in Cannot output a value using QSerialPort:

                Would this satisfy the QSerialPort::open()?

                Of course not, it does not have anything to do with open().

                if(!arduino->open(QSerialPort::ReadOnly)) {
                    qDebug() << "Opening serial port failed";
                    qDebug() << arduino->error() << arduino->errorString();
                }
                
                J Offline
                J Offline
                jude.bato
                wrote on 19 Mar 2020, 15:16 last edited by
                #27

                @jsulm Thank you for your suggestion, I put in your code and I ran it again. This is what I got:1.png I'm so confused why my program isn't working correctly. I have a feeling that my problem has to do with reading from or something with making a connection with Arduino rather than the Serial Port itself.

                P J 2 Replies Last reply 19 Mar 2020, 15:19
                0
                • J jude.bato
                  19 Mar 2020, 15:16

                  @jsulm Thank you for your suggestion, I put in your code and I ran it again. This is what I got:1.png I'm so confused why my program isn't working correctly. I have a feeling that my problem has to do with reading from or something with making a connection with Arduino rather than the Serial Port itself.

                  P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 19 Mar 2020, 15:19 last edited by
                  #28

                  @jude-bato

                  First: please don't post screenshots, paste the text itself. It's easier for others to copy/paste/reply to that!

                  Device is already open

                  Have you seen this message?
                  What do you think about that? My bet is that you also have other program (i.e. Arduino IDE) connected to your device...

                  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

                  J 1 Reply Last reply 20 Mar 2020, 15:13
                  4
                  • J jude.bato
                    19 Mar 2020, 15:16

                    @jsulm Thank you for your suggestion, I put in your code and I ran it again. This is what I got:1.png I'm so confused why my program isn't working correctly. I have a feeling that my problem has to do with reading from or something with making a connection with Arduino rather than the Serial Port itself.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 19 Mar 2020, 15:33 last edited by
                    #29

                    @jude-bato Exactly what @SGaist was suggesting

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

                    1 Reply Last reply
                    2
                    • P Pablo J. Rogina
                      19 Mar 2020, 15:19

                      @jude-bato

                      First: please don't post screenshots, paste the text itself. It's easier for others to copy/paste/reply to that!

                      Device is already open

                      Have you seen this message?
                      What do you think about that? My bet is that you also have other program (i.e. Arduino IDE) connected to your device...

                      J Offline
                      J Offline
                      jude.bato
                      wrote on 20 Mar 2020, 15:13 last edited by
                      #30

                      @Pablo-J-Rogina I have an Arduino Uno connected via USB, that's my original point in making the forum post is because I am unable to capture a voltage in Qt from it. I have it stated in my original post.

                      P 1 Reply Last reply 20 Mar 2020, 15:28
                      0
                      • J jude.bato
                        20 Mar 2020, 15:13

                        @Pablo-J-Rogina I have an Arduino Uno connected via USB, that's my original point in making the forum post is because I am unable to capture a voltage in Qt from it. I have it stated in my original post.

                        P Offline
                        P Offline
                        Pablo J. Rogina
                        wrote on 20 Mar 2020, 15:28 last edited by
                        #31

                        @jude-bato said in Cannot output a value using QSerialPort:

                        I have an Arduino Uno connected via USB,

                        Yes, I assumed that. What you should know, if not aware yet, is that you cannot have two applications (i.e. your Qt app and the Arduino IDE) using the same port simultaneously

                        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

                        J 1 Reply Last reply 21 Mar 2020, 15:20
                        3
                        • P Pablo J. Rogina
                          20 Mar 2020, 15:28

                          @jude-bato said in Cannot output a value using QSerialPort:

                          I have an Arduino Uno connected via USB,

                          Yes, I assumed that. What you should know, if not aware yet, is that you cannot have two applications (i.e. your Qt app and the Arduino IDE) using the same port simultaneously

                          J Offline
                          J Offline
                          jude.bato
                          wrote on 21 Mar 2020, 15:20 last edited by
                          #32

                          @Pablo-J-Rogina So how can I just read voltage from my Arduino Uno in Qt

                          P 1 Reply Last reply 21 Mar 2020, 16:13
                          0
                          • J jude.bato
                            21 Mar 2020, 15:20

                            @Pablo-J-Rogina So how can I just read voltage from my Arduino Uno in Qt

                            P Offline
                            P Offline
                            Pablo J. Rogina
                            wrote on 21 Mar 2020, 16:13 last edited by
                            #33

                            @jude-bato said in Cannot output a value using QSerialPort:

                            So how can I just read voltage from my Arduino Uno in Qt

                            Since you haven't describe your environment/use case so far, I'll assume that you have a program running on the Arduino board, and that program reads some sensor and it outputs such readings via serial.

                            So with that assumption, I expect you to go through something like this:

                            1. Write the Arduino code, and via Arduino IDE deploy it on your Arduino device
                            2. Close the Arduino IDE
                            3. Start Qt Creator and write a Qt application that will read the serial port and display the readings on screen
                            4. Run your Qt application
                            5. Success!

                            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

                            J 1 Reply Last reply 21 Mar 2020, 18:40
                            2
                            • P Pablo J. Rogina
                              21 Mar 2020, 16:13

                              @jude-bato said in Cannot output a value using QSerialPort:

                              So how can I just read voltage from my Arduino Uno in Qt

                              Since you haven't describe your environment/use case so far, I'll assume that you have a program running on the Arduino board, and that program reads some sensor and it outputs such readings via serial.

                              So with that assumption, I expect you to go through something like this:

                              1. Write the Arduino code, and via Arduino IDE deploy it on your Arduino device
                              2. Close the Arduino IDE
                              3. Start Qt Creator and write a Qt application that will read the serial port and display the readings on screen
                              4. Run your Qt application
                              5. Success!
                              J Offline
                              J Offline
                              jude.bato
                              wrote on 21 Mar 2020, 18:40 last edited by jude.bato
                              #34

                              @Pablo-J-Rogina Thats exactly what I'm trying to do. I read upon this article: https://forum.qt.io/topic/64696/sending-a-data-to-arduino-through-serial-port-using-qt and it is a similar problem to the one I'm having, for some reason Qt is not reading from the Arduino at all. It recognizes it but it's not reading from it. Here is my Arduino IDE code:

                              int offset = 20;
                              
                              void setup() 
                              {
                                Serial.begin(9600);
                              }
                              
                              void loop()
                              {
                                int volt = analogRead(A0);
                                double voltage = map(volt, 0, 1023, 0, 2500) + offset;
                              
                                voltage /= 100;
                                Serial.print("Voltage: ");
                                Serial.print(voltage);
                                Serial.println("V");
                              
                                delay(500);
                              }
                              

                              I've tried doing you recommendation but I'm having difficulties with Qt and grabbing information off Arduino.
                              Picture of Arduino working properly

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                jude.bato
                                wrote on 21 Mar 2020, 18:53 last edited by
                                #35

                                I am testing the hardware and I see that when I use the Arduino IDE or the Qt code they both work the same way and allow for a reading to happen I believe it is something to do with the GUI that I have designed that would allow for the values to be shown I will try and change it up and see if anything happens. I've been reading many many forum posts and it seems that my code is fine because it compiles and it works the same way when running the board through the IDE.

                                P 1 Reply Last reply 21 Mar 2020, 19:16
                                0
                                • J jude.bato
                                  21 Mar 2020, 18:53

                                  I am testing the hardware and I see that when I use the Arduino IDE or the Qt code they both work the same way and allow for a reading to happen I believe it is something to do with the GUI that I have designed that would allow for the values to be shown I will try and change it up and see if anything happens. I've been reading many many forum posts and it seems that my code is fine because it compiles and it works the same way when running the board through the IDE.

                                  P Offline
                                  P Offline
                                  Pablo J. Rogina
                                  wrote on 21 Mar 2020, 19:16 last edited by
                                  #36

                                  @jude-bato I don't know why I didn't yet suggested to check with some Qt examples, anyway. What if you try building and running:
                                  Command Line Reader Async Example (no GUI, just to test you are able to receive data from Arduino...)
                                  Terminal "Terminal shows how to create a terminal for a simple serial interface by using Qt Serial Port."

                                  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

                                  J 1 Reply Last reply 21 Mar 2020, 20:08
                                  1
                                  • P Pablo J. Rogina
                                    21 Mar 2020, 19:16

                                    @jude-bato I don't know why I didn't yet suggested to check with some Qt examples, anyway. What if you try building and running:
                                    Command Line Reader Async Example (no GUI, just to test you are able to receive data from Arduino...)
                                    Terminal "Terminal shows how to create a terminal for a simple serial interface by using Qt Serial Port."

                                    J Offline
                                    J Offline
                                    jude.bato
                                    wrote on 21 Mar 2020, 20:08 last edited by
                                    #37

                                    @Pablo-J-Rogina I ran the example and the Application Output I got was:

                                    16:00:11: Running steps for project creaderasync...
                                    16:00:11: Configuration unchanged, skipping qmake step.
                                    16:00:11: Starting: "C:\Qt\Tools\mingw730_32\bin\mingw32-make.exe" -j8
                                    C:/Qt/Tools/mingw730_32/bin/mingw32-make -f Makefile.Debug
                                    mingw32-make[1]: Entering directory 'C:/Qt/Examples/Qt-5.12.6/serialport/build-creaderasync-Desktop_Qt_5_12_6_MinGW_32_bit-Debug'
                                    mingw32-make[1]: Nothing to be done for 'first'.
                                    mingw32-make[1]: Leaving directory 'C:/Qt/Examples/Qt-5.12.6/serialport/build-creaderasync-Desktop_Qt_5_12_6_MinGW_32_bit-Debug'
                                    16:00:12: The process "C:\Qt\Tools\mingw730_32\bin\mingw32-make.exe" exited normally.
                                    16:00:12: Elapsed time: 00:01.
                                    

                                    Example Output
                                    I'm not sure of the result this is a little foreign to me.

                                    Looking back on my code I figured something out. When I have

                                     if(arduino_is_available)
                                        {
                                            qDebug()<<"Found the port \n";
                                            arduino->setPortName(arduino_uno_port_name);
                                            arduino->open(QSerialPort::ReadOnly);
                                            arduino->setBaudRate(QSerialPort::Baud9600);
                                            arduino->setDataBits(QSerialPort::Data8);
                                            arduino->setFlowControl(QSerialPort::NoFlowControl);
                                            arduino->setParity(QSerialPort::NoParity);
                                            arduino->setStopBits(QSerialPort::OneStop);
                                            QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial);
                                        } else {
                                            qDebug()<<"Could not find the correct port \n";
                                            QMessageBox::information(this,"Serial Port Error", "Could not open the serial port");
                                        }
                                    

                                    I also have:

                                    void Dialog::updateVoltage(QString sensor_reading)
                                    {
                                        ui->voltagelcdNumber->display(sensor_reading);
                                    }
                                    

                                    which is never called. And when I tried to call it in the previous code I get the error:

                                    too few arguments to function call, expected 1, have 0
                                    

                                    If I am able to implement the update voltage into my previous code I believe it will work. My issue now is how am I supposed to get around this error. When I hover over it, it says it requires a QString, I have tried everything to try and get around this error.

                                    P 1 Reply Last reply 21 Mar 2020, 20:57
                                    0
                                    • J jude.bato
                                      21 Mar 2020, 20:08

                                      @Pablo-J-Rogina I ran the example and the Application Output I got was:

                                      16:00:11: Running steps for project creaderasync...
                                      16:00:11: Configuration unchanged, skipping qmake step.
                                      16:00:11: Starting: "C:\Qt\Tools\mingw730_32\bin\mingw32-make.exe" -j8
                                      C:/Qt/Tools/mingw730_32/bin/mingw32-make -f Makefile.Debug
                                      mingw32-make[1]: Entering directory 'C:/Qt/Examples/Qt-5.12.6/serialport/build-creaderasync-Desktop_Qt_5_12_6_MinGW_32_bit-Debug'
                                      mingw32-make[1]: Nothing to be done for 'first'.
                                      mingw32-make[1]: Leaving directory 'C:/Qt/Examples/Qt-5.12.6/serialport/build-creaderasync-Desktop_Qt_5_12_6_MinGW_32_bit-Debug'
                                      16:00:12: The process "C:\Qt\Tools\mingw730_32\bin\mingw32-make.exe" exited normally.
                                      16:00:12: Elapsed time: 00:01.
                                      

                                      Example Output
                                      I'm not sure of the result this is a little foreign to me.

                                      Looking back on my code I figured something out. When I have

                                       if(arduino_is_available)
                                          {
                                              qDebug()<<"Found the port \n";
                                              arduino->setPortName(arduino_uno_port_name);
                                              arduino->open(QSerialPort::ReadOnly);
                                              arduino->setBaudRate(QSerialPort::Baud9600);
                                              arduino->setDataBits(QSerialPort::Data8);
                                              arduino->setFlowControl(QSerialPort::NoFlowControl);
                                              arduino->setParity(QSerialPort::NoParity);
                                              arduino->setStopBits(QSerialPort::OneStop);
                                              QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial);
                                          } else {
                                              qDebug()<<"Could not find the correct port \n";
                                              QMessageBox::information(this,"Serial Port Error", "Could not open the serial port");
                                          }
                                      

                                      I also have:

                                      void Dialog::updateVoltage(QString sensor_reading)
                                      {
                                          ui->voltagelcdNumber->display(sensor_reading);
                                      }
                                      

                                      which is never called. And when I tried to call it in the previous code I get the error:

                                      too few arguments to function call, expected 1, have 0
                                      

                                      If I am able to implement the update voltage into my previous code I believe it will work. My issue now is how am I supposed to get around this error. When I hover over it, it says it requires a QString, I have tried everything to try and get around this error.

                                      P Offline
                                      P Offline
                                      Pablo J. Rogina
                                      wrote on 21 Mar 2020, 20:57 last edited by
                                      #38

                                      @jude-bato said in Cannot output a value using QSerialPort:

                                      I'm not sure of the result this is a little foreign to me.

                                      Sorry but it's evident that you don't read the documentation...
                                      When running the command line example, please check the arguments it needs...

                                      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

                                      1 Reply Last reply
                                      2
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 21 Mar 2020, 22:13 last edited by
                                        #39

                                        Hi
                                        Why not try the same
                                        https://doc.qt.io/qt-5/qtserialport-terminal-example.html
                                        as @Pablo-J-Rogina linked to ?
                                        Its a littel serial comm GUI that can easy open a serial port and
                                        read and write to it.
                                        Very good for testing out if the Arduino responds as expected.

                                        J 1 Reply Last reply 21 Mar 2020, 23:22
                                        1
                                        • mrjjM mrjj
                                          21 Mar 2020, 22:13

                                          Hi
                                          Why not try the same
                                          https://doc.qt.io/qt-5/qtserialport-terminal-example.html
                                          as @Pablo-J-Rogina linked to ?
                                          Its a littel serial comm GUI that can easy open a serial port and
                                          read and write to it.
                                          Very good for testing out if the Arduino responds as expected.

                                          J Offline
                                          J Offline
                                          jude.bato
                                          wrote on 21 Mar 2020, 23:22 last edited by
                                          #40

                                          @mrjj @Pablo-J-Rogina I ran both examples and since this is my first Qt project I'm not exactly sure what I'm looking for. I have attached my results.
                                          Simple Terminal
                                          I know that my Arduino works already because I have tested it in the Arduino IDE as well as when I am debugging my code. I already know that everything works, my issue is just being able to display it. That second example is similar to what I want to do expect having my own GUI. Many thanks for all the responses thus far!

                                          P 1 Reply Last reply 21 Mar 2020, 23:31
                                          0

                                          21/55

                                          16 Mar 2020, 18:25

                                          • Login

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