Important: Please read the Qt Code of Conduct -

QT5.5 serial/terminal example how to send data

  • using the terminal example I have created another form that is activated from he tool bar of the main window
    It has one button on it at the moment
    How do i send data to the serial port when I click the button

    code for new form

    #include "am3x.h"
    #include "ui_am3x.h"

    AM3x::AM3x(QWidget *parent) :
    ui(new Ui::AM3x)

    delete ui;

    void AM3x::on_pushButton_clicked()



  • Lifetime Qt Champion

    well the mainwindow has
    QSerialPort *serial;
    and you need to do
    serial->write( "hello" );

    it also has slot to write data called
    void writeData(const QByteArray &data);

    so you could make signal in AM3x

      void writeData(const QByteArray &data);

    connect that signal to the main window slot of same name

    and then in
    emit writeData(somebytearray);

  • Thanks
    But how do I connect the the signal to the main window slot

    #ifndef AM3X_H
    #define AM3X_H

    #include <QDialog>

    namespace Ui {
    class AM3x;

    class AM3x : public QDialog
    void writeData(const QByteArray &data);

    explicit AM3x(QWidget *parent = 0);

    private slots:
    void on_pushButton_clicked();

    Ui::AM3x *ui;

    #endif // AM3X_H


    ** Copyright (C) 2012 Denis Shienkov
    ** Copyright (C) 2012 Laszlo Papp
    ** Contact:
    ** This file is part of the QtSerialPort module of the Qt Toolkit.
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and Digia. For licensing terms and
    ** conditions see For further information
    ** use the contact form at
    ** GNU Lesser General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU Lesser
    ** General Public License version 2.1 or version 3 as published by the Free
    ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
    ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
    ** following information to ensure the GNU Lesser General Public License
    ** requirements will be met: and
    ** In addition, as a special exception, Digia gives you certain additional
    ** rights. These rights are described in the Digia Qt LGPL Exception
    ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtCore/QtGlobal>

    #include <QMainWindow>

    #include <QtSerialPort/QSerialPort>

    #include "am3x.h"

    #include "cisco.h"


    namespace Ui {
    class MainWindow;


    class Console;
    class SettingsDialog;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    private slots:
    void openSerialPort();
    void closeSerialPort();
    void about();
    void writeData(const QByteArray &data);
    void readData();

    void handleError(QSerialPort::SerialPortError error);
    void on_actionAM3x_triggered();
    void on_actionCISCO_triggered();

    void initActionsConnections();

    Ui::MainWindow *ui;
    Console *console;
    SettingsDialog *settings;
    QSerialPort *serial;
    AM3x *am31;
    CISCO *cisco;


    #endif // MAINWINDOW_H

  • Lifetime Qt Champion

    in mainwindow
    you make an instance of AM3x
    i see you have the pointer
    so in mainwin constructor u make
    am31= new AM3x (); // for using later to show also
    bool res=connect(am31, SIGNAL(writeData( QByteArray ) ), this, SLOT(writeData( QByteArray ) );
    qDebug() << " conneted:" << res;

    Its best to do in constructor as if you do connect in button where u show AM3x, you will end up with multiple connections.

Log in to reply