'Qt serial communication using UART RS232C

I want to connect qt and a device using UART cable (RS232C) in linux.

I´m writing code, making ui and operating, but it does not work.

I want to connect when i click some button(ui) device turn on and connect.

Also i want to make a function that if i enter some command device will recognize and execute.

Below is my code , someone help me please.

<mainwindow.cpp>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>
#include <QObject>
#include <QIODevice>
#include <QDebug>
QSerialPort serial;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
     QSerialPort*port=new QSerialPort();
    port->setPortName("/dev/ttyUSB0");
    port->setBaudRate(QSerialPort::Baud19200);
    port->setDataBits(QSerialPort::Data8);
    port->setParity(QSerialPort::NoParity);
    port->setStopBits(QSerialPort::OneStop);
    port->setFlowControl(QSerialPort::NoFlowControl);
    port->open(QIODevice::ReadWrite);
    ui->setupUi(this);

    serial = new QSerialPort(this);

}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_connect_clicked()
{
    port=new QSerialPort();

 QObject::connect(port,SIGNAL(readyRead()),this,
SLOT(on_pushButton_connect_clicked()));

    if(!port->open(QIODevice::ReadWrite)){
        QMessageBox::information(this, tr("connect"), 
           "serialcommunication start");

    }
    else
    {
        QMessageBox::critical(this, tr("fail"), serial-
              >errorString());

    }
}


void MainWindow::on_pushButton_disconnect_clicked()
{
    port->close();
 QMessageBox::information(this, tr("disconnect"), "serial 
  communication end");
         }

<mainwindow.h>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSerialPort/QSerialPort>
#include <QMessageBox>
#include <QIODevice>
#include <QDebug>
namespace Ui {
class MainWindow;
 }
class MainWindow : public QMainWindow
 {
     Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    QSerialPort*serial; //plus
    QSerialPort*port;
    QWidget*main_widget;
    void readData();
    ~MainWindow();
private slots:

    void on_pushButton_connect_clicked();

    void on_pushButton_disconnect_clicked();


private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H


<main.cpp>
#include "mainwindow.h"
#include <QApplication>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
#include <QMessageBox>
#include <QIODevice>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    foreach(const QSerialPortInfo 
         &info,QSerialPortInfo::availablePorts()){

        QSerialPort serial;
         serial.setPort(info);
         if (serial.open(QIODevice::ReadWrite))
             serial.close();
    }

     MainWindow w;
      w.show();

        return a.exec();
     }


Solution 1:[1]

First of all it is not guaranteed that your device will be always connected to /dev/ttyUSB0 so you'l better search for your device by QSerialPortInfo with parameter QString manufacturer() const or quint16 productIdentifier() const or QString serialNumber() const.

Also you are creating too many QSerialPort and don't handle it. Create just one.

Here is sample code:

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.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QSerialPort;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    //! Receives all the data from serial port
    void readSerialData();

    void on_pushButton_connect_clicked();

    void on_pushButton_disconnect_clicked();

private:
    Ui::MainWindow *ui;
    QSerialPort *mSerialPort;
};

#endif // MAINWINDOW_H

Next check your Your product manufacturer or serial number and set here.
Also you need separate handler for received data like I created readSerialData
mainwindows.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mSerialPort{new QSerialPort}
{
    ui->setupUi(this);

    mSerialPort->setBaudRate(QSerialPort::Baud19200);
    mSerialPort->setDataBits(QSerialPort::Data8);
    mSerialPort->setParity(QSerialPort::NoParity);
    mSerialPort->setStopBits(QSerialPort::OneStop);
    mSerialPort->setFlowControl(QSerialPort::NoFlowControl);

    connect(mSerialPort, SIGNAL(readyRead()), this, SLOT(readSerialData()));
}

MainWindow::~MainWindow()
{
    delete mSerialPort;
    delete ui;
}

void MainWindow::readSerialData()
{
    QByteArray lTmpBA;
    lTmpBA = mSerialPort->readAll();

    qDebug() << "Received data: " << lTmpBA;
}

void MainWindow::on_pushButton_connect_clicked()
{
    foreach(QSerialPortInfo item, QSerialPortInfo::availablePorts()) {
        if (item.manufacturer() == "Your product") { //past your manufacturer here
            mSerialPort->setPort(item);
            if(!mSerialPort->open(QIODevice::ReadWrite)){
                QMessageBox::information(this, tr("connect"),
                                         "serialcommunication start");
            } else {
                QMessageBox::critical(this, tr("fail"), mSerialPort->errorString());
            }
        } else {
            qDebug() << "No connected device found";
        }
    }
}

void MainWindow::on_pushButton_disconnect_clicked()
{
    mSerialPort->close();
}

latter if you want to send some data to your UART device just implemente slot and call method:

mSerialPort->write("Some command");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1