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
Qt 6.11 is out! See what's new in the release blog

Cannot output a value using QSerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
55 Posts 8 Posters 44.9k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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.

    ? 1 Reply Last reply
    1
    • mrjjM mrjj

      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.

      ? Offline
      ? Offline
      A Former User
      wrote on 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!

      Pablo J. RoginaP 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #41

        Hi
        Ok super. so it works.
        You are not showing complete code so hard to guess.
        QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial);
        This looks fine

        So in
        Dialog::readSerial

        you call updateVoltage( yourbuffer )

        Or when is that called ?

        Im not sure where "too few arguments to function call, expected 1, have 0" comes
        but it sounds like you called updateVoltage without its qstring.

        ? 1 Reply Last reply
        1
        • ? A Former User

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

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #42

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

          I'm not exactly sure what I'm looking for

          If guessing, to write a Qt app that can connect to your Arduino device and display information sent from it via the serial port.

          So your next steps would be to use the Simple Terminal example as a reference to check what you need to do in order to open the port, check for errors, how to connect signal and slots, etc.

          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 mrjj

            Hi
            Ok super. so it works.
            You are not showing complete code so hard to guess.
            QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial);
            This looks fine

            So in
            Dialog::readSerial

            you call updateVoltage( yourbuffer )

            Or when is that called ?

            Im not sure where "too few arguments to function call, expected 1, have 0" comes
            but it sounds like you called updateVoltage without its qstring.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #43

            @mrjj This is the full code:

            #include "dialog.h"
            #include "ui_dialog.h"
            #include <iostream>
            #include <QSerialPort>
            #include <QSerialPortInfo>
            #include <string>
            #include <QDebug>
            #include <QMessageBox>
            
            Dialog::Dialog(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::Dialog)
            {
                ui->setupUi(this);
                ui->voltagelcdNumber->display("0.00");
                arduino = new QSerialPort(this);
                serialBuffer = "";
                parsed_data = "";
                voltage_value = 0.0;
            
                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);
                    QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial);
                    //ui->voltagelcdNumber->display(sensor_reading); Here is where I'm haveing trouble
                } else {
                    qDebug()<<"Could not find the correct port \n";
                    QMessageBox::information(this,"Serial Port Error", "Could not open the serial port");
                }
            }
            
            Dialog::~Dialog()
            {
                if(arduino->isOpen())
                {
                    arduino->close();
                }
                delete ui;
            }
            
            void Dialog::readSerial()
            {
                QStringList buffer_split = serialBuffer.split(",");
                if(buffer_split.length() < 3)
                {
                    serialData = arduino->readAll();
                    serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
                    serialData.clear();
                } else {
                    serialBuffer = "";
                    qDebug() << buffer_split << "\n";
                    parsed_data = buffer_split[1];
                    voltage_value = (parsed_data.toDouble()) - 0.1;
                    qDebug() << "Voltage: " << voltage_value << "\n";
                    parsed_data = QString::number(voltage_value,'g',4);
                    Dialog::updateVoltage(parsed_data);
                }
            }
            
            void Dialog::updateVoltage(QString sensor_reading)
            {
                ui->voltagelcdNumber->display(sensor_reading);
            }
            

            sensor_reading is private and initialized in QString

            @Pablo-J-Rogina I have tested it in the simple terminal and it works, as well as working in the Arduino IDE

            1 Reply Last reply
            -1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #44

              Hi

              Some notes

              You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.
              must be before open!
              All of them. Open goes last.
              Actually you can :)

              You do not check if
              arduino->open(QSerialPort::ReadOnly);
              fails.
              like
              if ( ! arduino->open(QSerialPort::ReadOnly) ) {
              QMessageBox::information(this,...report error);
              return;
              }
              see @jsulm post !

              So does it ever enter into void Dialog::readSerial() ?

              1 Reply Last reply
              2
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #45

                @mrjj said in Cannot output a value using QSerialPort:

                You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.

                I already said this some days ago: https://forum.qt.io/topic/112346/cannot-output-a-value-using-qserialport/23 but it is simply ignored...

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

                mrjjM 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @mrjj said in Cannot output a value using QSerialPort:

                  You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.

                  I already said this some days ago: https://forum.qt.io/topic/112346/cannot-output-a-value-using-qserialport/23 but it is simply ignored...

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #46

                  @Christian-Ehrlicher
                  Indeed and also @jsulm also noted to check open which seems to be forgotten also.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kuzulis
                    Qt Champions 2020
                    wrote on last edited by
                    #47

                    @mrjj said in Cannot output a value using QSerialPort:

                    You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.
                    must be before open!

                    This is not true. You can configure QSP in any time.

                    mrjjM Pablo J. RoginaP 2 Replies Last reply
                    1
                    • K kuzulis

                      @mrjj said in Cannot output a value using QSerialPort:

                      You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.
                      must be before open!

                      This is not true. You can configure QSP in any time.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #48

                      @kuzulis
                      Ok
                      thank you for clarifying that.
                      Over the years I have not had huge success with that without close and reopen the port but its
                      good to know QSerialPort does support it :)

                      1 Reply Last reply
                      0
                      • K kuzulis

                        @mrjj said in Cannot output a value using QSerialPort:

                        You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.
                        must be before open!

                        This is not true. You can configure QSP in any time.

                        Pablo J. RoginaP Offline
                        Pablo J. RoginaP Offline
                        Pablo J. Rogina
                        wrote on last edited by
                        #49

                        @kuzulis said in Cannot output a value using QSerialPort:

                        This is not true. You can configure QSP in any time.

                        Are you saying that you can change the settings once a connection has been established?

                        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

                        mrjjM 1 Reply Last reply
                        0
                        • Pablo J. RoginaP Pablo J. Rogina

                          @kuzulis said in Cannot output a value using QSerialPort:

                          This is not true. You can configure QSP in any time.

                          Are you saying that you can change the settings once a connection has been established?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #50

                          @Pablo-J-Rogina

                          Yes seems so and he should know as he wrote it :)

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kuzulis
                            Qt Champions 2020
                            wrote on last edited by
                            #51

                            @Pablo-J-Rogina said in Cannot output a value using QSerialPort:

                            Are you saying that you can change the settings once a connection has been established?

                            Of course, after open() you can do what you need.

                            Pablo J. RoginaP 1 Reply Last reply
                            0
                            • K kuzulis

                              @Pablo-J-Rogina said in Cannot output a value using QSerialPort:

                              Are you saying that you can change the settings once a connection has been established?

                              Of course, after open() you can do what you need.

                              Pablo J. RoginaP Offline
                              Pablo J. RoginaP Offline
                              Pablo J. Rogina
                              wrote on last edited by
                              #52

                              @kuzulis but changing the settings once you open the port, does impact on the ongoing connection?
                              I mean, does the other side need to update the settings as well? And if so, won't the connection be interrupted?

                              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
                              0
                              • K Offline
                                K Offline
                                kuzulis
                                Qt Champions 2020
                                wrote on last edited by kuzulis
                                #53

                                @Pablo-J-Rogina said in Cannot output a value using QSerialPort:

                                @kuzulis but changing the settings once you open the port, does impact on the ongoing connection?
                                I mean, does the other side need to update the settings as well? And if so, won't the connection be interrupted?

                                The serial communication has no "connection" things. About what "connection" do you speak?

                                PS: In reality (on a low level), any device configurations always changes after a device has been opened! This belongs to any device.

                                Pablo J. RoginaP 1 Reply Last reply
                                0
                                • K kuzulis

                                  @Pablo-J-Rogina said in Cannot output a value using QSerialPort:

                                  @kuzulis but changing the settings once you open the port, does impact on the ongoing connection?
                                  I mean, does the other side need to update the settings as well? And if so, won't the connection be interrupted?

                                  The serial communication has no "connection" things. About what "connection" do you speak?

                                  PS: In reality (on a low level), any device configurations always changes after a device has been opened! This belongs to any device.

                                  Pablo J. RoginaP Offline
                                  Pablo J. RoginaP Offline
                                  Pablo J. Rogina
                                  wrote on last edited by
                                  #54

                                  @kuzulis said in Cannot output a value using QSerialPort:

                                  About what "connection" do you speak?

                                  An ongoing transmission

                                  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
                                  1
                                  • K Offline
                                    K Offline
                                    kuzulis
                                    Qt Champions 2020
                                    wrote on last edited by kuzulis
                                    #55

                                    @Pablo-J-Rogina said in Cannot output a value using QSerialPort:

                                    An ongoing transmission

                                    In this case an ongoing data stream just will be transferred with a new parameters (speed, parity ant etc).

                                    1 Reply Last reply
                                    2

                                    • Login

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