Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML Button to Send Message over serial port .



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

  • Lifetime Qt Champion

    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.



  • Thanks it was very helpful :)


  • Lifetime Qt Champion

    Hi @muhammadismail,

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

    Thanks


Log in to reply