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. QT5.5 serial/terminal example how to send data
Forum Updated to NodeBB v4.3 + New Features

QT5.5 serial/terminal example how to send data

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.3k Views 1 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.
  • B Offline
    B Offline
    Barney
    wrote on last edited by
    #1

    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

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

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

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Barney
        wrote on last edited by
        #3

        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

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

          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.

          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