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. Simple serial port not outputting data

Simple serial port not outputting data

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.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.
  • S Offline
    S Offline
    starfire151
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        starfire151
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            starfire151
            wrote on last edited by
            #5

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

              [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
              0
              • S Offline
                S Offline
                starfire151
                wrote on last edited by
                #7

                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
                0

                • Login

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