QtGui Serial Port & Arduino Serial Flow [SOLVED]



  • Hi All,

    I am new on Qt, and I would like to build a simple project.
    This project consists to READ the serial flow of an Arduino, which send two float values.
    I would like to read these values through two LCD provided by Qt Creator and that's it !

    I suppose it is possible...I read tons of informations, but I am unable to write the code.
    Could you help me please ?

    Thank you for your time and help.

    Kind regards and cheers for this new year to you all.

    Chris



  • Use QtSerialPort, read documentation, see examples.



  • Thanks Kuzulis for your reply.

    I read the docs and examples, but it doesn't work for my case...
    Anyway, I will try one more time to do the job.

    Thanks again.

    Regards,


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why doesn't it work for you case ?

    Can you describe your setup ?



  • Hi SGait,

    Thanks for your reply.

    I don't really have problems, because today I am just learning Qt5.
    It's a fantastic tool, but I am just a little bit disapointed when I saw all we need to do to just to print on a widget a serial flow...
    It's amazing !

    Well, I follow my search to see how to set my *.pro and the QSerial lib.

    The main goal for me is pretty simple:

    Sending some flow on the serial port /dev/tty (Linux) and show them through the widget LCD of Qt, and that's it :-)

    Cheers,


  • Lifetime Qt Champion

    /dev/tty is generally not a serial port but the default console.

    Then you didn't try to write it 100% natively ;)

    I must say that it doesn't require that much:
    QSerialPort + QLCDNumber and a slot for processing the data coming from your Arduino



  • Hi,

    Thank you a lot, I will try it.

    Have a nice day.

    Cheers,



  • Hi,

    Ok, I did some test, and this my files:

    .pro

    @
    QT += core gui serialport

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = Pitot
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui
    @

    This is the header file:
    @#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 on_lcdNumber_overflow();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    And the mainwindows.cpp:
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSerialPort>
    #include <QDebug>

    QSerialPort serial;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    serial= new QSerialPort(this);
    serial->setPortName("/dev/ttyACM0");
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->open(QIODevice::ReadOnly);
    connect(serial, SIGNAL(readyRead()),this,SLOT(serialReceived()));

    }

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

    void MainWindow::on_lcdNumber_overflow()
    {
    QByteArray qb;
    qb=serial->readAll();
    ui->lcdNumber->setDecMode(qb);
    qDebug() << qb;
    }
    @

    and the compilator says:

    !file:///home/tux/Capture du 2014-12-26 10:18:28.png()!



  • Could you help me to solve this problem ?

    Thanks in advance for your time and support.

    Regards,



  • Ok, don't take in account my last post.
    I solve a problem due to a pointer.

    So, the only problem I have is the next, and this is the mainwindows.cpp:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSerialPort>
    #include <QDebug>

    QSerialPort *serial;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    serial=new QSerialPort(this);
    serial->setPortName("/dev/ttyACM0");
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->open(QIODevice::ReadOnly);
    connect(serial, SIGNAL(readyRead()),this,SLOT(serialReceived()));

    }

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

    void MainWindow::on_lcdNumber_overflow()
    {
    QByteArray qb;
    qb=serial->readAll();
    ui->lcdNumber->setDecMode(qb);
    qDebug() << qb;
    }
    @

    So everything is fine but a line 35, I have a compilator problem.
    It says:

    /mainwindow.cpp:35: erreur : no matching function for call to 'QLCDNumber::setDecMode(QByteArray&)'
    ui->lcdNumber->setDecMode(qb);
    ^
    Could you tell me why it doesn't compile ?
    I really don't understand.

    Thanks for your help.

    Regards,



  • Hi all,

    Finally, I finished the job ! ;-)
    The reason why I had some problems was the ByteArray, but now it works fine.
    The modifications I did was:

    void MainWindow::serialReceived()
    {
    QByteArray valeur;
    valeur = serial->readAll();
    float nb = valeur.toFloat();
    ui->lcdNumber->display(nb);
    }

    And everythings lokk fine :-)

    So thanks for your support.

    Regards,


  • Lifetime Qt Champion

    Nice !

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

    Happy coding !


Log in to reply
 

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