Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Button to Send Message over serial port .

QML Button to Send Message over serial port .

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 974 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.
  • M Offline
    M Offline
    muhammadismail
    wrote on 18 Sept 2019, 18:41 last edited by
    #1

    i am new in qt i need a program with one button in Qt Quick and C++ to send hex like <0x00 , 0x22 , 0xAA> Message Over Serial port .
    i follow some tutorial and success to send the message with Qt Widget Application and C++ but i need to done with
    Qt Quick and C++ any help to modify or make new one !
    here my Qt Widget Application and C++ code :

    //SMSSerialTest.pro
    QT+= core gui serialport
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = SMSSerialtest
    TEMPLATE = app
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    
    CONFIG += c++11
    
    SOURCES += \
            main.cpp \
            mainwindow.cpp
    
    HEADERS += \
            mainwindow.h
    
    FORMS += \
            mainwindow.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    

    //mailnwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void serialReceived();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    //main.cpp
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    

    //mainwindwow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSerialPort>
    #include <QSerialPortInfo>
    
    QSerialPort *serial;
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        serial = new QSerialPort(this);
        serial->setPortName("COM3");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        serial->write(QByteArray::fromHex("2255AA"));
        serial->waitForBytesWritten(20);
        serial->write(QByteArray::fromHex("014480"));
        serial->waitForBytesWritten(100);
        serial->close();
    
        connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        serial->close();
    
    }
    
    void MainWindow::serialReceived()
    {
    
    
       ui->label->setText(serial->readAll());
    
    }
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Sept 2019, 18:45 last edited by
      #2

      Hi and welcome to devnet,

      You should start by reading the QML CPP Integration chapter of Qt's documentation.

      Then you can build a class that will handle the serial port communication and intersect with it from your QtQuick application.

      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
      2
      • M Offline
        M Offline
        muhammadismail
        wrote on 22 Sept 2019, 23:26 last edited by
        #3

        Thanks it was very helpful :)

        A 1 Reply Last reply 23 Sept 2019, 03:54
        1
        • M muhammadismail
          22 Sept 2019, 23:26

          Thanks it was very helpful :)

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 23 Sept 2019, 03:54 last edited by
          #4

          Hi @muhammadismail,

          if your problem is solved, so please mark this topic as SOLVED too.

          Thanks

          Qt has to stay free or it will die.

          1 Reply Last reply
          1

          1/4

          18 Sept 2019, 18:41

          • Login

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