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) :
    QDialog(parent),
    ui(new Ui::AM3x)
    {
    ui->setupUi(this);
    }

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

    void AM3x::on_pushButton_clicked()
    {

    }

    THANKS


  • Qt Champions 2016

    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
    like

    signals:
      void writeData(const QByteArray &data);
    

    connect that signal to the main window slot of same name

    and then in
    on_pushButton_clicked()
    emit writeData(somebytearray);



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

    #ifndef AM3X_H
    #define AM3X_H

    #include <QDialog>

    namespace Ui {
    class AM3x;
    }

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

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

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::AM3x *ui;
    };

    #endif // AM3X_H

    mainwindow.h

    /****************************************************************************
    **
    ** Copyright (C) 2012 Denis Shienkov denis.shienkov@gmail.com
    ** Copyright (C) 2012 Laszlo Papp lpapp@kde.org
    ** Contact: http://www.qt-project.org/legal
    **
    ** This file is part of the QtSerialPort module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:LGPL21$
    ** 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 http://qt.digia.com/licensing. For further information
    ** use the contact form at http://qt.digia.com/contact-us.
    **
    ** 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: https://www.gnu.org/licenses/lgpl.html and
    ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    **
    ** 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.
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtCore/QtGlobal>

    #include <QMainWindow>

    #include <QtSerialPort/QSerialPort>

    #include "am3x.h"

    #include "cisco.h"

    QT_BEGIN_NAMESPACE

    namespace Ui {
    class MainWindow;
    }

    QT_END_NAMESPACE

    class Console;
    class SettingsDialog;

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    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();
    

    private:
    void initActionsConnections();

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

    };

    #endif // MAINWINDOW_H


  • Qt Champions 2016

    hi
    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
    //connect
    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.