Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Simple serial port not outputting data

    General and Desktop
    3
    7
    1007
    Loading More Posts
    • 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
      starfire151 last edited by

      Apologies to all if this is not the correct place to post this...

      I'm a very newbie to the Qt environment but I am excited about the possibilities. I've looked through most of the example serial port apps related to this and tried to make a very small app that just writes a test message to a serial port. I've just created a simple main window which has a combobox populated with the available serial ports and a send command button which starts the action of sending the data to the port. I tried to make this very small and streamlined (maybe I made it TOO small and streamlined??). I'm including the mainwindow.h here:

      @
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QMainWindow>
      #include <QtSerialPort/QtSerialPort>

      namespace Ui {
      class MainWindow;
      }

      class MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();

      private slots:
      void on_pbSendCmd_clicked();

      private:
      Ui::MainWindow *ui;
      QSerialPort *serial;
      };

      #endif // MAINWINDOW_H
      @

      The mainwindow.cpp is included here:

      @
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QComboBox>
      #include <QString>
      #include <QMessageBox>
      #include <QtSerialPort/QSerialPortInfo>

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);

      // change window title
      setWindowTitle("Widget Testbed");
      
      // do serial port combobox population
      foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
          ui->serialPortComboBox->addItem(info.portName());
      
      // create serial port
      serial = new QSerialPort(this);
      

      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      void MainWindow::on_pbSendCmd_clicked()
      {
      // set parameters for com port
      serial->setPortName(ui->serialPortComboBox->currentText());
      serial->setBaudRate(QSerialPort::Baud38400);
      serial->setDataBits(QSerialPort::Data8);
      serial->setParity(QSerialPort::NoParity);
      serial->setStopBits(QSerialPort::OneStop);
      serial->setFlowControl(QSerialPort::NoFlowControl);

      // open the selected com port
      if(!serial->open(QIODevice::ReadWrite)){
          QMessageBox::warning(this, "COM Error", "Can't open port");
          return;
      }
      
      // write data to port
      QByteArray data = "test message";
      serial->write(data);
      
      // close the selected com port
      serial->close();
      

      }
      @

      In addition, I've changed the .pro file line to greaterThan(QT_MAHOR_VERSION, 4) : QT += widgets serialport.
      The combobox populates correctly and there are no compilation errors. The port appears to open correctly but no data is written. What am I doing wrong?

      Thanks for any help.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        How do you know there's nothing sent ?

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

        1 Reply Last reply Reply Quote 0
        • S
          starfire151 last edited by

          I hooked up the output to an oscilloscope and looked for any data transitions... Nothing showed up.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Does it also happen if you use a standard Windows utility ?

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

            1 Reply Last reply Reply Quote 0
            • S
              starfire151 last edited by

              Hi again -

              I have confirmed that the serial port works correctly using a Tera Term session to an external device connected to the same serial port I'm trying to get the Qt app to work with.

              Thanks.

              1 Reply Last reply Reply Quote 0
              • K
                kuzulis Qt Champions 2020 last edited by

                [quote]
                @
                // write data to port
                QByteArray data = "test message";
                serial->write(data);

                // close the selected com port
                serial->close();
                

                @
                [/quote]

                This is wrong sequence. The serial port nothing to write in this case because the close() immediately does closing of device.

                You should or:

                @
                serial->write(data);
                serial->flush();
                serial->close();
                @

                or:

                @
                serial->write(data);
                serial->waitForBytesWritten();
                serial->close();
                @

                or to close the device after the bytesWritten(qint64) signal received.

                NOTE: All I/O in serialport is asynchronous!

                1 Reply Last reply Reply Quote 0
                • S
                  starfire151 last edited by

                  Thanks so much for responding! I tried the serial->flush() but still didn't see anything on the scope. I tried the serial->waitForBytesWritten(strlen(data)) and it worked like a charm!

                  I'm still learning about how the timing of everything works but this is a grat start.

                  Thanks, again!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post