Qt serial communication does not work



  • i wrote the code Qt serial communication qt with device through UART.
    but if i preess the "connect" button, noting happened .
    qt show the message
    ///Debugging starts
    No connected device found///

    How can i connect the device ? I connected cable correctly

    #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();
    
    protected:
        void sendMsg(const QString &msg);
    
    public slots:
        void recvMsg();
    
    private slots:
        //void readData();  //plus
        void readSerialData();
    
        void on_pushButton_connect_clicked();
    
        void on_pushButton_disconnect_clicked();
    
    
        void on_pushButton_send_clicked();
    
    private:
        Ui::MainWindow *ui;
        QSerialPort *mSerialport;
    
    };
    #endif // MAINWINDOW_H
    
    
    #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();
    }
    
    
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort/QSerialPort>
    #include <QSerialPortInfo>
    #include <QMessageBox>
    #include <QObject>
    #include <QIODevice>
    #include <QDebug>
    #include <QPlainTextEdit>
    #include <QDateTime>
    QSerialPort serial;
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow),
        mSerialport{new QSerialPort}
    {
        ui->setupUi(this);
        mSerialport->setPortName("/dev/ttyUSB0");
        mSerialport->setBaudRate(QSerialPort::Baud19200);
        mSerialport->setDataBits(QSerialPort::Data8);
        mSerialport->setParity(QSerialPort::NoParity);
        mSerialport->setStopBits(QSerialPort::OneStop);
        mSerialport->setFlowControl(QSerialPort::NoFlowControl);
        //mSerialport->open(QIODevice::ReadWrite);
    
        connect(this->mSerialport, SIGNAL(readyRead()), this, SLOT(readSerialData()));
        connect(this->mSerialport, SIGNAL(readyRead()), this, SLOT(recvMsg()));
        connect(ui->pushButton_send, &QPushButton::clicked, [=](){
        sendMsg(ui->textEdit->toPlainText());
        });
        //connect(serial,QSerialPort::readyRead,this,MainWindow::readData());
    }
    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() == "/dev/ttyUSB0") { //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()
    {
    
        QMessageBox::information(this, tr("disconnect"), "serial communication end");
        mSerialport->close();
    }
    
    void MainWindow::on_pushButton_send_clicked()
    {
        mSerialport->open(QSerialPort::ReadWrite);
        mSerialport->write(ui->textEdit->toPlainText().toLatin1());
    
    }
    
    
    void MainWindow::sendMsg(const QString &msg){
          this->mSerialport->write(QByteArray::fromHex(msg.toLatin1()));
          ui->comLog->insertPlainText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") + " [send] " + msg + "\n");
    }
    
    void MainWindow::recvMsg(){
          QByteArray msg = this->mSerialport->readAll();
            //do something
          ui->comLog->insertPlainText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") + " [recieve] " + msg.toHex().data() + "\n");
    }
    
    
    

    [Moved to General and Desktop ~kshegunov]
    [Locked as duplicate of https://forum.qt.io/topic/83084/qt-serial-communication-using-uart ~kshegunov]


Log in to reply
 

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