Qt first project


  • Lifetime Qt Champion

    Did you check that the settings of the serial ports are really what you need to use with your device ?



  • @SGaist

    1)How do I check if the serial opened succefully?
    2)I disabled write.
    3)Do you mean to make it private member of MainWindow?


  • Lifetime Qt Champion

    1. Please read the documentation, the open function returns a boolean value saying whether it succeeded or not
    2. Where did you put the call to write ?
    3. Yes


  • @SGaist

    1)Ok, I will try it.
    2)I just commented it.

    1. I wrote QSerialPort *serial; under private and it shows error

  • Lifetime Qt Champion

    What error ?

    If you comment the call to write then don't expect anything particular to happen.



  • @SGaist

    C:\Users\geo\Documents\serialGui\mainwindow.h:27: error: 'QSerialPort' does not name a type
    QSerialPort *serial;
    ^

    I don't want to write something in COM from Qt, I want to "listen" from Arduino the sequence 232323... and print on the Qt application


  • Lifetime Qt Champion

    It's nothing Qt related, that's basic C++. You really should grab a good book on the matter.

    Search for "Forward declaration".



  • I know C, not C++. Are they so different?


  • Lifetime Qt Champion

    There are some key concepts that are pretty different yes.



  • And I should learn C++ for just this little error?

    Can someone help me to make it work?



  • $$$$$$$$$$$$$$$$$$$$$serialGui.pro$$$$$$$$$$$$$$$$$$$$$$$$$$

    #-------------------------------------------------

    Project created by QtCreator 2016-12-06T15:42:10

    #-------------------------------------------------

    QT += core gui serialport

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = serialGui
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    $$$$$$$$$$$$$$$$$ mainwindow.h $$$$$$$$$$$$$$$$$$$$$$$$$$

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private slots:
    void serialReceived();

    void on_pushButton_released();
    

    private:
    Ui::MainWindow *ui;

    QSerialPort *serial;
    

    };

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

    }

    $$$$$$$$$$$$$$$$$$$$$$ mainwindow.cpp $$$$$$$$$$$$$$$$$$$$$$$$

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort>
    #include <QDebug>

    QSerialPort *serial;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    if (serial->open(QIODevice::ReadWrite))
    {
    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("hello");
    serial->close();
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }
    else
    {
    QMessageBox::critical(this, tr("Error"), serial->errorString());
    showStatusMessage(tr("Open error"));
    }
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    serial->close();
    }

    void MainWindow::serialReceived()
    {
    QByteArray ba;
    ba=serial->readAll();
    ui->label->setText(ba);
    //qDebug()<<ba;
    }

    $$$$$$$$$$$$$$$$$$$ mainwindow.ui $$$$$$$$$$$$$$$$$$$$$$$$$$

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MainWindow</class>
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>MainWindow</string>
    </property>
    <widget class="QWidget" name="centralWidget">
    <widget class="QLabel" name="label">
    <property name="geometry">
    <rect>
    <x>50</x>
    <y>60</y>
    <width>121</width>
    <height>41</height>
    </rect>
    </property>
    <property name="text">
    <string>TextLabel</string>
    </property>
    </widget>
    </widget>
    <widget class="QMenuBar" name="menuBar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>21</height>
    </rect>
    </property>
    </widget>
    <widget class="QToolBar" name="mainToolBar">
    <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
    </attribute>
    <attribute name="toolBarBreak">
    <bool>false</bool>
    </attribute>
    </widget>
    <widget class="QStatusBar" name="statusBar"/>
    </widget>
    <layoutdefault spacing="6" margin="11"/>
    <resources/>
    <connections/>
    </ui>


  • Moderators

    @nick784512 said in Qt first project:

    And I should learn C++ for just this little error?

    You should learn C++ to use a C++ toolkit.

    It feels like a "little error" now, but you will encounter this in most (if not all) of your C++ projects.

    Can someone help me to make it work?

    @SGaist already gave you the answer: You need to add a Forward Declaration for QSerialPort in mainwindow.h.

    Also remember to #include <QSerialPort> in mainwindow.cpp



  • Hello!

    I managed to make it work and send symbols over the serial. The problem now is that when I send from the arduino (to Qt application) 2 or 3 symbols it prints them OK! But when I want to send the sentence "Hello World!" it breaks it into pieces and prints them, as you can see in the photo.

    http://www.picpaste.com/pics/serial_problem-V7LlwzUC.1483458098.jpg

    (Can the moderator attach the photo to this message?)


  • Lifetime Qt Champion

    That' why you usually have a protocol to know when you received all the data that are part of a "frame" and only then parse them for further processing.



  • How I do this? I think of using a "flag" that when the Qt application reads it, then prints the sentence. Right? For example "Hello World!#" and when it reads the "#", it prints the "Hello World!". Am I correct?


  • Lifetime Qt Champion

    More or less, yes.

    You buffer the data you can read when readyRead is emitted and once you find your # you take the amount of data needed from the buffer and do whatever you want with it.



  • For buffer is better to use an array or a data structure? List for example and each node to save a letter? Is there a source code to study?


  • Lifetime Qt Champion

    Don't over-engineer it, just use a QByteArray.



  • I use an array with a fixed length that when it become full I read it, or an array with dynamic length?


  • Qt Champions 2017

    @nick784512
    Hi
    a ByteArray ( dynamic ) would be the best. :)

    void SerialPortReader::handleReadyRead()
    {
    m_readData.append(m_serialPort->readAll());
    ...


Log in to reply
 

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