0

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

1 Answer 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");
Sign up to request clarification or add additional context in comments.

15 Comments

thank you for your reply . As you have adviced, i wrote the code . but if i preess the "connect" button, noting happed . How can i know device connect or not ? And the last you said "mSerialPort->write("Some command");" how implement ? I wrote the code and command one by one?
When you press connect it just starts connection with device and is trying to read data from serial port. If no data is emitted from device you will see nothing. If you want to check if device is connected just call isOpen() (mSerialPort->isOpen()).
To send data to device create new button, for instance send, so you will have slot void MainWindow::on_pushButton_send_clicked() {mSerialPort->write("Some command");}
thank you for your reply. i add the code and excute . But "No connected device found " in Application Output . I think qt with device no connection. how can i solve the problem??
if (item.manufacturer() == "Your product") - what is you product? If you don't know, just add qDebug() << item.manufacturer(); after foreach to see all connected devices. One of that is yours
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.