Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Tried to qdebug the output while the serial port is not open
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 596 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sankarapandiyanS Offline
    sankarapandiyanS Offline
    sankarapandiyan
    wrote on last edited by sankarapandiyan
    #1

    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();
    }
    
    KillerSmathK 1 Reply Last reply
    0
    • M Offline
      M Offline
      maxwell31
      wrote on last edited by
      #2

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

      sankarapandiyanS 1 Reply Last reply
      1
      • M maxwell31

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

        sankarapandiyanS Offline
        sankarapandiyanS Offline
        sankarapandiyan
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • sankarapandiyanS sankarapandiyan

          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();
          }
          
          KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by
          #4

          @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 ");
                      }   
              }

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          sankarapandiyanS 1 Reply Last reply
          1
          • KillerSmathK KillerSmath

            @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 ");
                        }   
                }
            sankarapandiyanS Offline
            sankarapandiyanS Offline
            sankarapandiyan
            wrote on last edited by
            #5

            @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();
            }
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxwell31
              wrote on last edited by
              #6

              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.

              sankarapandiyanS 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                sankarapandiyanS 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

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

                  sankarapandiyanS Offline
                  sankarapandiyanS Offline
                  sankarapandiyan
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • M maxwell31

                    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.

                    sankarapandiyanS Offline
                    sankarapandiyanS Offline
                    sankarapandiyan
                    wrote on last edited by
                    #9

                    @maxwell31 ok i will try

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved