Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Tried to qdebug the output while the serial port is not open



  • I have tried to qdebug like "SERIAL PORT IS NOT OPEN" like this... i have tried in the code but i cant get a output .

    I am tried with same laptop by connecting the two ports , ttyUSB0 and ttyUSB1 .
    Here i am removing the ttyUSB1 .. and i tried to send the data . If the serial port (ttyUSB1) is unplugged So, here the serial port is not open ..
    so i want to qdebug like "THE PORT IS NOT CONNECTED "
    i have tried on inside the function but its not works ..

     void MainWindow ::on_send_clicked()
    

    I have tried like something like this i have attached my code below

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QList>
    #include <QComboBox>
    #include <QString>
    #include <QTextEdit>
    #include <QTime>
    #include <QDebug>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QtSerialPort>
    #include <QFile>
    #include <QList>
    
    QString line ;
    QString txt3;
    QSerialPort *serial;
    QStringList m_lines;
    //QByteArray rawData;
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
          ui->setupUi(this);
            QList<QSerialPortInfo> list;
        list = QSerialPortInfo::availablePorts();
        for (int i= 0; i < list.length(); i++)
        {
            ui->comboBox->addItem(list[i].portName());
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        serial->close();
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        serial = new QSerialPort (this) ;
        serial->setPortName(ui->comboBox->currentText());
        serial->setBaudRate (QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity (QSerialPort::NoParity);
        serial->setStopBits (QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived()));
        ui->progressBar->setValue(100);
    //    ui->receive->clear();
    }
    
    void MainWindow::checkAndSendData()
    {
        if(!m_lines.isEmpty()){
            QByteArray baToSend = m_lines.takeFirst().toUtf8();
            serial->write(baToSend);
            ui->aknowmnt->setText("Sent Message -->" "\n"+  QString(baToSend) + "\n");
        } else {
            ui->aknowmnt->setText("Receiving ");
        }
    }
    
    void MainWindow ::on_send_clicked()
    {
               if(!serial->open(QIODevice::ReadWrite| QIODevice::Unbuffered)) // Here i have declared ...
                {
                    qDebug()<<"error: can't open com port";
                    ui->aknowmnt->setText("Port is not opening ");
                }     
    
            line = ui->sender->toPlainText();
            QString txt1 = ui->receive->toPlainText();
            /*QString*/ txt3 = ui->aknowmnt->toPlainText();
             if(m_lines.isEmpty()){
                QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                inputFile.open(QIODevice::ReadOnly);
                if (!inputFile.isOpen())
                    return;
    
                QTextStream stream(&inputFile);
                while(!stream.atEnd()){
                    QString line = stream.readLine();
                    m_lines.append(line + QString("\r\n"));
                }
            }
    
            checkAndSendData();
    }
    
    
    void MainWindow::serialReceived()
    {
        QString glen = serial->readAll();
        if (glen.length()>0){
        /*QString*/ line = ui->receive->toPlainText();
        ui->receive->setText(ui->receive->toPlainText()+line+"\n");
        ui->receive->setText(line + "\r\n" + glen + "\r\n");
       // line = txt3;
        }
    
        checkAndSendData();
    }
    


  • Are you using Linux? Then you need to check the permissions of ttyUSB0, or generate an appropriate udev rule



  • @maxwell31 YEs i am using Linux only ... But i want to qdebug the port which is "port is not opening ".. while clicking the send button . I write some code for that in the send button . please check it my code is it correct or not thats only i want



  • @sankarapandiyan

    When the device is unplugged, it maybe emits an error signal, so you could handle this signal by connecting a slot on it.

    connect(serial, SIGNAL(error(QSerialPort::SerialPortError), this, SLOT(your_slot_function(QSerialPort::SerialPortError));
    
    

    List of possible results of SerialPortError enum:
    https://doc.qt.io/qt-5/qserialport.html#SerialPortError-enum

    The type of error that is most similar to the behavior you are trying to achieve is QSerialPort::ResourceError

    void MainWindow::your_slot_function(QSerialPort::SerialPortError error)
    {
        if (error == QSerialPort::ResourceError)
        {
            qDebug() << "Resource becomes unavailable";
            serial->close(); // close the serial
        }
    }
    

    As an addition, you could check if the serial is already opened instead of try to open it again inside of on_send_clicked function.

        if(!serial->isOpen()){ // if is not open then try to open
                if(!serial->open(QIODevice::ReadWrite| QIODevice::Unbuffered)){
                    qDebug()<<"error: can't open com port";
                    ui->aknowmnt->setText("Port is not opening ");
                }   
        }


  • @KillerSmath Yes i tried with this but i did not get it ..
    this is my code
    the code flow is right or wrong please suggest me ...,

    QString line ;
    QString txt3;
    QSerialPort *serial;
    QStringList m_lines;
    //QByteArray rawData;
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
    
          ui->setupUi(this);
    
          QString filename1="/home/adx-soft1/Shankar/serial/myfile1.txt";
          QFile file(filename1);
          if(!file.exists()){
              qDebug() << "NO FILE "<<filename1;
          }else{
              qDebug() << filename1<<" ...";
          }
          QString line1;
          ui->sender->clear();
          if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ ui->sender->setText(file.readAll()); }
    
        QList<QSerialPortInfo> list;
        list = QSerialPortInfo::availablePorts();
    
        for (int i= 0; i < list.length(); i++)
        {
            ui->comboBox->addItem(list[i].portName());
    
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        serial->close();
    }
    void MainWindow::your_slot_function(QSerialPort::SerialPortError error)
    {
        if (error == QSerialPort::ResourceError)
        {
            qDebug() << "Resource becomes unavailable";
            ui->aknowmnt->setText("PORT IS NOT OPENING");
            serial->close(); // close the serial
        }
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        serial = new QSerialPort (this) ;
        serial->setPortName(ui->comboBox->currentText());
        serial->setBaudRate (QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity (QSerialPort::NoParity);
        serial->setStopBits (QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
       connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived()));
       connect(serial,SIGNAL(QSerialPort::SerialPortError), this, SLOT(your_slot_function(QSerialPort::SerialPortError)));
    
    //    if(!serial->open(QIODevice::ReadWrite| QIODevice::Unbuffered))
    //    {
    //        qDebug()<<"error: can't open com port";
    //        ui->aknowmnt->setText("Port is not opening ");
    //    }
    
       ui->progressBar->setValue(100);
    //    ui->receive->clear();
    }
    void MainWindow::checkAndSendData()
    {
        if (!serial->isOpen())
         {
            qDebug()<<"error: can't open com port";
            ui->aknowmnt->setText("device is not open  ");
             //Port not open
    
         }
        if(!m_lines.isEmpty()){
            QByteArray baToSend = m_lines.takeFirst().toUtf8();
            serial->write(baToSend);
            ui->aknowmnt->setText("Sent Message -->" "\n"+  QString(baToSend) + "\n");
        }
        else
        {
            ui->aknowmnt->setText("Receiving ");
        }
    
    }
    
    
    void MainWindow ::on_send_clicked()
    {
            line = ui->sender->toPlainText();
            QString txt1 = ui->receive->toPlainText();
            /*QString*/ txt3 = ui->aknowmnt->toPlainText();
             if(m_lines.isEmpty()){
                QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt"));
                inputFile.open(QIODevice::ReadOnly);
                if (!inputFile.isOpen())
                    return;
    
                QTextStream stream(&inputFile);
                while(!stream.atEnd()){
                    QString line = stream.readLine();
                    m_lines.append(line + QString("\r\n"));
                }
            }
    
            checkAndSendData();
    }
    
    
    void MainWindow::serialReceived()
    {
        QString glen = serial->readAll();
        if (glen.length()>0){
        /*QString*/ line = ui->receive->toPlainText();
        ui->receive->setText(ui->receive->toPlainText()+line+"\n");
        ui->receive->setText(line + "\r\n" + glen + "\r\n");
       // line = txt3;
        }
    
        checkAndSendData();
    }
    


  • What I want to say: As a user you don't have access to ttyUSB0. You should either chmod 777 /dev/ttyUSB0 (just for checking, after a reboot it will be reset), create a udev rule for usb (see https://askubuntu.com/questions/15570/configure-udev-to-change-permissions-on-usb-hid-device) or for quick testing if permissions are the reason, you can also start your program with sudo.


  • Lifetime Qt Champion

    @maxwell31 said in Tried to qdebug the output while the serial port is not open:

    As a user you don't have access to ttyUSB0

    Why? You just have to be in the group dialout. Using chmod or running as root is a hack.



  • @Christian-Ehrlicher said in Tried to qdebug the output while the serial port is not open:

    You just have to be in the group dialout.

    Yes i have done like this only.



  • @maxwell31 ok i will try


Log in to reply