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. qextserialport write and progress bar
Forum Updated to NodeBB v4.3 + New Features

qextserialport write and progress bar

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 6 Posters 4.5k Views 2 Watching
  • 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.
  • A another_one

    @SGaist

    Thank you!
    I'm trying doing in this way but when I connect my signal the program exit and closed automatically :

    My class:

    class qextserpw : public QObject
    {
        Q_OBJECT
    public:
       // explicit qextserpw(QObject *parent = nullptr);
        explicit qextserpw(QextSerialPort  *serialPort, QObject *parent = nullptr);
            void write(const QByteArray &writeData);
    
    //private slots:
     public slots:
            void handleBytesWritten(qint64 bytes);
    
    
    private:
        QextSerialPort *m_serialPort = nullptr;
        QByteArray m_writeData;
    
        qint64 m_bytesWritten = 0;
    signals:
        void inprogress(unsigned long progr);
    };
    
    #endif // QEXTSERPW_H
    
    
    
    
    #include <QtDebug>
    #include <QtGlobal>
    #include <QtCore>
    #include <QProgressDialog>
    
    //qextserpw::qextserpw(QObject *parent) : QObject(parent)
      qextserpw::qextserpw(QextSerialPort *serialPort, QObject *parent)
    
    {
    
    }
      void qextserpw::handleBytesWritten(qint64 bytes)
      {
    
    
      }
    
    

    And these connection gots failed launch of program
    :

    qextserpw *qextser;
        connect(qextser, SIGNAL(inprogress(int progr)), bar, SLOT(setValue(progr)));
    
    
    // or
    
      connect(qextser, SIGNAL( inprogress(int) ), bar, SLOT( setValue(int) ) );
    
    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #22

    @another_one said in qextserialport write and progress bar:

    qextserpw *qextser;

    Don't you think this variable should contain a valid pointer before it's used?

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

    A 1 Reply Last reply
    1
    • Christian EhrlicherC Christian Ehrlicher

      @another_one said in qextserialport write and progress bar:

      qextserpw *qextser;

      Don't you think this variable should contain a valid pointer before it's used?

      A Offline
      A Offline
      another_one
      wrote on last edited by
      #23

      @Christian-Ehrlicher

      Unfortunally it doesn't help

      qextserpw  *qextser = new qextserpw(port);
      connect(port, SIGNAL(bytesWritten(qint64)), this, SLOT(writeData(qint64)));
      connect(qextser, SIGNAL( inprogress(int) ), bar, SLOT( setValue(int) ) );
      
      A 1 Reply Last reply
      0
      • A another_one

        @Christian-Ehrlicher

        Unfortunally it doesn't help

        qextserpw  *qextser = new qextserpw(port);
        connect(port, SIGNAL(bytesWritten(qint64)), this, SLOT(writeData(qint64)));
        connect(qextser, SIGNAL( inprogress(int) ), bar, SLOT( setValue(int) ) );
        
        A Offline
        A Offline
        another_one
        wrote on last edited by
        #24

        @another_one

        sorry, I forgot about bar initialization too
        Now program can launch but I have got the same problem with progress bar...

        JonBJ 1 Reply Last reply
        0
        • A another_one

          @another_one

          sorry, I forgot about bar initialization too
          Now program can launch but I have got the same problem with progress bar...

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #25

          @another_one What exact "same problem with progress bar..."?

          A 1 Reply Last reply
          0
          • JonBJ JonB

            @another_one What exact "same problem with progress bar..."?

            A Offline
            A Offline
            another_one
            wrote on last edited by
            #26

            @JonB
            I can't parallelize read from qexteserialport, write to qextserialport and setValue to progress bar while writing
            To do what I create qextserpw class:

            qextserpw.h

            
            #ifndef QEXTSERPW_H
            #define QEXTSERPW_H
            
            #include <QObject>
            #include <QtCore/QIODevice>
            #include <qextserialport.h>
            
            #include <QDebug>
            #include "terminal.h"
            
            
            class qextserpw : public QObject
            {
                Q_OBJECT
            public:
                explicit qextserpw();
                void write(const QByteArray &writeData);
                QextSerialPort *port = new QextSerialPort (QextSerialPort::EventDriven);
                bool m_open(QIODevice::OpenMode mode);
                void m_port_close();
                bool check_open();
                bool status_port;
            public slots:
                //void handleBytesWritten(qint64 bytes);
            	void m_readAll();
            private:
                QextSerialPort *m_serialPort = nullptr;
                //QByteArray m_writeData;
                //qint64 m_bytesWritten = 0;
            signals:
                void inprogress(QByteArray data);
                //void to_store();
                void towrite();
            };
            
            #endif // QEXTSERPW_H
            

            qextserpw.c

            
            #include "qextserpw.h"
            #include "monmk.h"
            #include "terminal.h"
            #include <QDebug>
            #include <QtGlobal>
            #include <QtCore>
            #include <QProgressDialog>
            #include <QtCore/QIODevice>
            
             qextserpw::qextserpw()
            {
                port = new QextSerialPort (QextSerialPort::EventDriven); // настройка работы порта в асинхронном режиме чтения и записи
                port->setTimeout(1);
                connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));
                qDebug() << "another class created";
             }
            
            void qextserpw::handleBytesWritten(qint64 bytes)
            {
            
            }
            
            void qextserpw::write(const QByteArray &writeData)
            {
                port->write(writeData);
            }
            
            bool qextserpw::m_open(QIODevice::OpenMode mode)
            {	
            	bool rv=false;
                rv = port->open(mode);
                qDebug() << "port is open from another class" ;
                return rv;
            }
            bool qextserpw::check_open()
            {	
            	bool rv=false;
            	rv = port->isOpen();
            	return rv;
            }
            void qextserpw::m_readAll()
            { 
            	QByteArray temp;
                temp = port->readAll();
                emit inprogress(temp);
                qDebug() << "read from another thread!";
            }
            void qextserpw::m_port_close()
            {
                port->flush();
                port->close();
            }
            
            

            This class is necessary to do 2 threads - read data from qextserial and write data to qextserial

            Third class to work with bar is the main class of mainwindow and called monMK:

            monMK.h

            
            #ifndef MONMK_H
            #define MONMK_H
            #include "qextserialport.h"
            #include "qextserialport_p.h"
            #include <QFileDialog>
            #include <QFile>
            #include <QDir>
            #include <QMainWindow>
            #include <QTimer>
            #include "terminal.h"
            #include "json.h"
            #include "choicepropertiseport.h"
            #include <QMainWindow>
            #include <QProgressDialog>
            #include <QMutex>
            #include "qexception.h"
            #include "qextserpw.h"
            #include "terminal.h"
            QT_BEGIN_NAMESPACE
            namespace Ui { class monMK; }
            QT_END_NAMESPACE
            
            class monMK : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit monMK(QWidget *parent = 0);
                ~monMK();
                unsigned long fls = 0 ;
                QString dir_c_g;
                QByteArray qbag = "{ \
                                     \"PORT\": \"COM3\", \
                                     \"RATE\": \"115200\", \
                                     \"PARITY\": \"0\", \
                                     \"STOPBIT\": \"2\", \
                                     \"BITS\": \"8\", \
                                     \"LD_ADDR\": \"FFFFFFFFB5000020\", \
                                     \"ST_ADDR\": \"FFFFFFFFB5000020\", \
                                     \"MEM_TYPE\": \"LWord\", \
                                     \"MEM_ADDR\": \"FFFFFFFFB5000000\", \
                                     \"FILE_LOAD\": \"C:\" \
                                     }";
                ChoicePropertisePort *dialogPort = new ChoicePropertisePort;
                JSON *fs2 = new JSON();
                QProgressDialog *bar = new QProgressDialog(this);
                char mu;
                int div ;
                int mod ;
                QByteArray y;     
                QString g_LD_ADDR;
                QString g_ST_ADDR ;
                QString g_FILE_LOAD ;
                QString g_PORT ;
                QString g_RATE ;
                QString g_PARITY;
                QString g_STOPBIT;
                QString temps;
                DataBitsType g_BITS ;
                
            qextserpw  *qextser = new qextserpw();
                unsigned char glb = 0;
                unsigned char  globalf ;
                QByteArray charBuffer;
                quint32 crc_32;
                QString g_portname;
                void CreateProgresswin();
                Terminal *textTerminal = new Terminal();
            ;
            QByteArray arr;
                //
                typedef struct {
                    uint8_t prefix = 0xFF;
                    uint8_t code = 0x00;
                } restart_t;
            
                //
                typedef struct {
                    uint8_t prefix = 0xFF;
                    uint8_t code = 0x01;
                    uint64_t start_addr = 0xFFFFFFFFB5000020;
                } start_t;
                //
                typedef struct {
                    uint8_t prefix = 0xFF;
                    uint8_t code = 0x02;
                    uint64_t load_addr = 0xFFFFFFFFB5000020;
                    uint32_t data_len = 0x00000000;
                    uint8_t* data;
                } write_RAM_t;
              start_t start;
              write_RAM_t writeRAM;
            protected:
            void closeEvent(QCloseEvent *event);    
            public slots:
            void readData(QByteArray data);
            
            private slots:
                void on_pB_propertiseCOM_clicked();
                void on_open_folder_clicked();
                void on_start_clicked();
                void on_load_clicked();
                void on_loadnstart_clicked();
                void on_restart_clicked();
                void writeData(qint64);
                void JFS(int value);
                void on_pushButton_6_clicked();
                void on_lineEdit_3_cursorPositionChanged(int arg1, int arg2);
                void on_lineEdit_2_cursorPositionChanged(int arg1, int arg2);
            
            private:
                Ui::monMK *ui;
            };
            #endif // MONMK_H
            

            monMK.c

            #include "monmk.h"
            #include "ui_monmk.h"
            #include "choicepropertiseport.h"
            #include <QtDebug>
            #include <QDesktopWidget>
            #include <QDateTime>
            #include <QMessageBox>
            #include <QLabel>
            #include <QtGlobal>
            #include <QtCore>
            #include <QtEndian>
            #include <QProgressDialog>
            #include <QJsonDocument>
            #include <QDir>
            #include <QFileDialog>
            #include <json.h>
            #include "crc32.h"
            #include <QString>
            #include <QByteArray>
            #include <qextserialport_p.h>
            #include <QDebug>
            #include <qextserpw.h>
            
            monMK::monMK(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::monMK)
            {
                ui->setupUi(this);
                this->setFixedSize(this->geometry().width(),this->geometry().height()); // set fixed size
                start_t start_st;
                ui->label_4->setPixmap(QPixmap(":/images/title.png"));
                textTerminal->setWindowTitle("PortTerminal");
                textTerminal->setGeometry(1156, 341, 382, 282);
                textTerminal->show();
                ui->pushButton_5->setEnabled(false);
                connect(qextser, SIGNAL(towrite()), this, SLOT(writeData())); 						// for writing 
                connect(qextser, SIGNAL( inprogress(QByteArray data) ), this, SLOT( readData(QByteArray  data))); 	// for reading
            	
              ....
              ...
              ..
            
            }
            void monMK::writeData()
            {
                //bar->setValue(ii);
            	unsigned long ii;
                bar->setRange(1, fls-1); // 
                for(ii=0;ii<fls;ii++)
                {
                  // bar->setValue(ii);
                  y = arr.mid(ii, 1 );     //
                  //port->write(y,1);
                  qextser->write(y);//,1);
                  bar->setValue(ii);
                  qDebug() << "ii progress bar value" << ii;
            
            }
            
            void monMK::readData(QByteArray data)
            {
                qDebug() << "ready read";
            	textTerminal->insert_Text(data);//addText(data);
                qDebug() << "working";
            }
            
            
            
            void monMK::writeButton_clicked()
            {
                QByteArray y;     // y == "pine"
                QByteArray z;
                if (!file.open(QIODevice::ReadOnly))	
                {
                    QMessageBox::information(this, tr("can't open the file"),
                    file.errorString());
                    return;
                }
                else
                 {
            		qDebug() << "opening file";
            		fls = file.size();
            		arr.resize(fls);
            		qDebug() << "size of" << fls;
            		for(unsigned long i; i<fls;i++)
            		{arr = 0;}
            		if( arr.isEmpty())
            		qDebug() << "at first array is empty";
            		else
            		qDebug() <<  "at first array isn't empty";
            		arr = file.readAll();
            		div = fls / 100;
            		mod = fls % 100;
            		file.close();
            		QString str3 = "";
            		str3.append(arr);
            		textTerminal->insert_Text(str3);//addText(data);
            		qDebug() << "closing file";
            		qDebug() << "array from file" ;
            		QString str2 = "";
            		str2.append(arr);
            		if( arr.isEmpty())
            		qDebug() << "array is empty";
            		else
            		qDebug() <<  "array isn't empty";
                 }
            
            
                    if(qextser->status_port)
                     //if(port->isOpen())
                     {
                       	emit qextser->towrite();
                       }
            
                     
            
            }
            
            
            
            

            Also I have got another window for terminal(serial data printing) - textTerminal

            The problem in new version is I even can read data now
            In debug I see whese lines :
            "
            another class created
            read from another thread!
            "
            But in readData I do not run

            I suppose what is something is wrong in trasnferring signal from one class to another
            But It seems be right

            Please help

            Pl45m4P 1 Reply Last reply
            0
            • A another_one

              @JonB
              I can't parallelize read from qexteserialport, write to qextserialport and setValue to progress bar while writing
              To do what I create qextserpw class:

              qextserpw.h

              
              #ifndef QEXTSERPW_H
              #define QEXTSERPW_H
              
              #include <QObject>
              #include <QtCore/QIODevice>
              #include <qextserialport.h>
              
              #include <QDebug>
              #include "terminal.h"
              
              
              class qextserpw : public QObject
              {
                  Q_OBJECT
              public:
                  explicit qextserpw();
                  void write(const QByteArray &writeData);
                  QextSerialPort *port = new QextSerialPort (QextSerialPort::EventDriven);
                  bool m_open(QIODevice::OpenMode mode);
                  void m_port_close();
                  bool check_open();
                  bool status_port;
              public slots:
                  //void handleBytesWritten(qint64 bytes);
              	void m_readAll();
              private:
                  QextSerialPort *m_serialPort = nullptr;
                  //QByteArray m_writeData;
                  //qint64 m_bytesWritten = 0;
              signals:
                  void inprogress(QByteArray data);
                  //void to_store();
                  void towrite();
              };
              
              #endif // QEXTSERPW_H
              

              qextserpw.c

              
              #include "qextserpw.h"
              #include "monmk.h"
              #include "terminal.h"
              #include <QDebug>
              #include <QtGlobal>
              #include <QtCore>
              #include <QProgressDialog>
              #include <QtCore/QIODevice>
              
               qextserpw::qextserpw()
              {
                  port = new QextSerialPort (QextSerialPort::EventDriven); // настройка работы порта в асинхронном режиме чтения и записи
                  port->setTimeout(1);
                  connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));
                  qDebug() << "another class created";
               }
              
              void qextserpw::handleBytesWritten(qint64 bytes)
              {
              
              }
              
              void qextserpw::write(const QByteArray &writeData)
              {
                  port->write(writeData);
              }
              
              bool qextserpw::m_open(QIODevice::OpenMode mode)
              {	
              	bool rv=false;
                  rv = port->open(mode);
                  qDebug() << "port is open from another class" ;
                  return rv;
              }
              bool qextserpw::check_open()
              {	
              	bool rv=false;
              	rv = port->isOpen();
              	return rv;
              }
              void qextserpw::m_readAll()
              { 
              	QByteArray temp;
                  temp = port->readAll();
                  emit inprogress(temp);
                  qDebug() << "read from another thread!";
              }
              void qextserpw::m_port_close()
              {
                  port->flush();
                  port->close();
              }
              
              

              This class is necessary to do 2 threads - read data from qextserial and write data to qextserial

              Third class to work with bar is the main class of mainwindow and called monMK:

              monMK.h

              
              #ifndef MONMK_H
              #define MONMK_H
              #include "qextserialport.h"
              #include "qextserialport_p.h"
              #include <QFileDialog>
              #include <QFile>
              #include <QDir>
              #include <QMainWindow>
              #include <QTimer>
              #include "terminal.h"
              #include "json.h"
              #include "choicepropertiseport.h"
              #include <QMainWindow>
              #include <QProgressDialog>
              #include <QMutex>
              #include "qexception.h"
              #include "qextserpw.h"
              #include "terminal.h"
              QT_BEGIN_NAMESPACE
              namespace Ui { class monMK; }
              QT_END_NAMESPACE
              
              class monMK : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  explicit monMK(QWidget *parent = 0);
                  ~monMK();
                  unsigned long fls = 0 ;
                  QString dir_c_g;
                  QByteArray qbag = "{ \
                                       \"PORT\": \"COM3\", \
                                       \"RATE\": \"115200\", \
                                       \"PARITY\": \"0\", \
                                       \"STOPBIT\": \"2\", \
                                       \"BITS\": \"8\", \
                                       \"LD_ADDR\": \"FFFFFFFFB5000020\", \
                                       \"ST_ADDR\": \"FFFFFFFFB5000020\", \
                                       \"MEM_TYPE\": \"LWord\", \
                                       \"MEM_ADDR\": \"FFFFFFFFB5000000\", \
                                       \"FILE_LOAD\": \"C:\" \
                                       }";
                  ChoicePropertisePort *dialogPort = new ChoicePropertisePort;
                  JSON *fs2 = new JSON();
                  QProgressDialog *bar = new QProgressDialog(this);
                  char mu;
                  int div ;
                  int mod ;
                  QByteArray y;     
                  QString g_LD_ADDR;
                  QString g_ST_ADDR ;
                  QString g_FILE_LOAD ;
                  QString g_PORT ;
                  QString g_RATE ;
                  QString g_PARITY;
                  QString g_STOPBIT;
                  QString temps;
                  DataBitsType g_BITS ;
                  
              qextserpw  *qextser = new qextserpw();
                  unsigned char glb = 0;
                  unsigned char  globalf ;
                  QByteArray charBuffer;
                  quint32 crc_32;
                  QString g_portname;
                  void CreateProgresswin();
                  Terminal *textTerminal = new Terminal();
              ;
              QByteArray arr;
                  //
                  typedef struct {
                      uint8_t prefix = 0xFF;
                      uint8_t code = 0x00;
                  } restart_t;
              
                  //
                  typedef struct {
                      uint8_t prefix = 0xFF;
                      uint8_t code = 0x01;
                      uint64_t start_addr = 0xFFFFFFFFB5000020;
                  } start_t;
                  //
                  typedef struct {
                      uint8_t prefix = 0xFF;
                      uint8_t code = 0x02;
                      uint64_t load_addr = 0xFFFFFFFFB5000020;
                      uint32_t data_len = 0x00000000;
                      uint8_t* data;
                  } write_RAM_t;
                start_t start;
                write_RAM_t writeRAM;
              protected:
              void closeEvent(QCloseEvent *event);    
              public slots:
              void readData(QByteArray data);
              
              private slots:
                  void on_pB_propertiseCOM_clicked();
                  void on_open_folder_clicked();
                  void on_start_clicked();
                  void on_load_clicked();
                  void on_loadnstart_clicked();
                  void on_restart_clicked();
                  void writeData(qint64);
                  void JFS(int value);
                  void on_pushButton_6_clicked();
                  void on_lineEdit_3_cursorPositionChanged(int arg1, int arg2);
                  void on_lineEdit_2_cursorPositionChanged(int arg1, int arg2);
              
              private:
                  Ui::monMK *ui;
              };
              #endif // MONMK_H
              

              monMK.c

              #include "monmk.h"
              #include "ui_monmk.h"
              #include "choicepropertiseport.h"
              #include <QtDebug>
              #include <QDesktopWidget>
              #include <QDateTime>
              #include <QMessageBox>
              #include <QLabel>
              #include <QtGlobal>
              #include <QtCore>
              #include <QtEndian>
              #include <QProgressDialog>
              #include <QJsonDocument>
              #include <QDir>
              #include <QFileDialog>
              #include <json.h>
              #include "crc32.h"
              #include <QString>
              #include <QByteArray>
              #include <qextserialport_p.h>
              #include <QDebug>
              #include <qextserpw.h>
              
              monMK::monMK(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::monMK)
              {
                  ui->setupUi(this);
                  this->setFixedSize(this->geometry().width(),this->geometry().height()); // set fixed size
                  start_t start_st;
                  ui->label_4->setPixmap(QPixmap(":/images/title.png"));
                  textTerminal->setWindowTitle("PortTerminal");
                  textTerminal->setGeometry(1156, 341, 382, 282);
                  textTerminal->show();
                  ui->pushButton_5->setEnabled(false);
                  connect(qextser, SIGNAL(towrite()), this, SLOT(writeData())); 						// for writing 
                  connect(qextser, SIGNAL( inprogress(QByteArray data) ), this, SLOT( readData(QByteArray  data))); 	// for reading
              	
                ....
                ...
                ..
              
              }
              void monMK::writeData()
              {
                  //bar->setValue(ii);
              	unsigned long ii;
                  bar->setRange(1, fls-1); // 
                  for(ii=0;ii<fls;ii++)
                  {
                    // bar->setValue(ii);
                    y = arr.mid(ii, 1 );     //
                    //port->write(y,1);
                    qextser->write(y);//,1);
                    bar->setValue(ii);
                    qDebug() << "ii progress bar value" << ii;
              
              }
              
              void monMK::readData(QByteArray data)
              {
                  qDebug() << "ready read";
              	textTerminal->insert_Text(data);//addText(data);
                  qDebug() << "working";
              }
              
              
              
              void monMK::writeButton_clicked()
              {
                  QByteArray y;     // y == "pine"
                  QByteArray z;
                  if (!file.open(QIODevice::ReadOnly))	
                  {
                      QMessageBox::information(this, tr("can't open the file"),
                      file.errorString());
                      return;
                  }
                  else
                   {
              		qDebug() << "opening file";
              		fls = file.size();
              		arr.resize(fls);
              		qDebug() << "size of" << fls;
              		for(unsigned long i; i<fls;i++)
              		{arr = 0;}
              		if( arr.isEmpty())
              		qDebug() << "at first array is empty";
              		else
              		qDebug() <<  "at first array isn't empty";
              		arr = file.readAll();
              		div = fls / 100;
              		mod = fls % 100;
              		file.close();
              		QString str3 = "";
              		str3.append(arr);
              		textTerminal->insert_Text(str3);//addText(data);
              		qDebug() << "closing file";
              		qDebug() << "array from file" ;
              		QString str2 = "";
              		str2.append(arr);
              		if( arr.isEmpty())
              		qDebug() << "array is empty";
              		else
              		qDebug() <<  "array isn't empty";
                   }
              
              
                      if(qextser->status_port)
                       //if(port->isOpen())
                       {
                         	emit qextser->towrite();
                         }
              
                       
              
              }
              
              
              
              

              Also I have got another window for terminal(serial data printing) - textTerminal

              The problem in new version is I even can read data now
              In debug I see whese lines :
              "
              another class created
              read from another thread!
              "
              But in readData I do not run

              I suppose what is something is wrong in trasnferring signal from one class to another
              But It seems be right

              Please help

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #27

              @another_one said in qextserialport write and progress bar:

              connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));

              Was about to write something about that line, then I realized, it's not wrong (at least technically).
              Please, do not name your slots m_xxxxx, it will confuse everyone who's reading your code and eventually trying to help.
              After "name conventions" m_<someName> is for class member variables. Better dont name a function in a way like this.

              Then, unless you're using Qt4, which I hope you dont, move to the Qt5+ connect syntax, where you use function pointers instead of string-based signal/slot keywords.

              // old
              connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));
              
              // better, for example:
              connect(port, &QextSerialPort::readyRead, this, &qextserpw::onPortReadReady); // onPortReadReady instead of "m_readAll"
              

              For your ProgressBar:

              Have a look at this again, and at what @SGaist has written here


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              A 1 Reply Last reply
              2
              • Pl45m4P Pl45m4

                @another_one said in qextserialport write and progress bar:

                connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));

                Was about to write something about that line, then I realized, it's not wrong (at least technically).
                Please, do not name your slots m_xxxxx, it will confuse everyone who's reading your code and eventually trying to help.
                After "name conventions" m_<someName> is for class member variables. Better dont name a function in a way like this.

                Then, unless you're using Qt4, which I hope you dont, move to the Qt5+ connect syntax, where you use function pointers instead of string-based signal/slot keywords.

                // old
                connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));
                
                // better, for example:
                connect(port, &QextSerialPort::readyRead, this, &qextserpw::onPortReadReady); // onPortReadReady instead of "m_readAll"
                

                For your ProgressBar:

                Have a look at this again, and at what @SGaist has written here

                A Offline
                A Offline
                another_one
                wrote on last edited by
                #28

                Thanks to all!

                I think I took into account all recomendations, except one - relates to events
                Unfortunally I haven't got any excperiance in Qt event loop, QCoreApplication::processEvents etc

                And I asking help in how to do in thread-safe manner this case of writing with progress bar

                Here is my code which still is black screen in CentOS only:

                qextserpw.h

                #ifndef QEXTSERPW_H
                #define QEXTSERPW_H
                
                #include <QObject>
                #include <QtCore/QIODevice>
                #include <qextserialport.h>
                
                #include <QDebug>
                #include "terminal.h"
                
                
                class qextserpw : public QObject
                {
                    Q_OBJECT
                public:
                    explicit qextserpw();
                    void write(const QByteArray &writeData);
                    QextSerialPort *port = new QextSerialPort (QextSerialPort::EventDriven);
                    QByteArray* global_data;
                    bool m_open(QIODevice::OpenMode mode);
                   	bool status_port;
                signals:
                    void inprogress(long data);
                    void to_store();
                    void to_indi(QByteArray data);
                 public slots:
                    //void handleBytesWritten(qint64 bytes);
                	void m_readAll();
                private:
                    //QextSerialPort *m_serialPort = nullptr;
                    //QByteArray m_writeData;
                    //qint64 m_bytesWritten = 0;
                
                };
                
                #endif // QEXTSERPW_H
                

                qextserpw.c

                
                #include "qextserpw.h"
                #include "monmk.h"
                #include "terminal.h"
                #include <QDebug>
                #include <QtGlobal>
                #include <QtCore>
                #include <QProgressDialog>
                #include <QtCore/QIODevice>
                
                 qextserpw::qextserpw()
                {
                    port = new QextSerialPort (QextSerialPort::EventDriven);  
                    port->setTimeout(1);
                    connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));
                    qDebug() << "another class created";
                 }
                
                void qextserpw::handleBytesWritten(qint64 bytes)
                {
                
                }
                
                void qextserpw::write(const QByteArray &writeData)
                {
                    port->write(writeData);
                }
                
                bool qextserpw::m_open(QIODevice::OpenMode mode)
                {bool rv=false;
                    rv = port->open(mode);
                     qDebug() << "port is open from another class" ;
                    return rv;
                }
                bool qextserpw::check_open()
                {bool rv=false;
                 rv = port->isOpen();
                return rv;
                }
                void qextserpw::m_readAll()
                { QByteArray temp;
                
                    temp = port->readAll();
                	global_data = &temp;
                	emit to_indi(*global_data);
                    qDebug() << "read from another thread!";
                }
                void qextserpw::m_port_close()
                {
                    port->flush();
                    port->close();
                }
                

                monMK.h

                
                #ifndef MONMK_H
                #define MONMK_H
                #include "qextserialport.h"
                #include "qextserialport_p.h"
                #include <QFileDialog>
                #include <QFile>
                #include <QDir>
                #include <QMainWindow>
                #include <QTimer>
                #include "terminal.h"
                #include "json.h"
                #include "choicepropertiseport.h"
                #include <QMainWindow>
                #include <QProgressDialog>
                #include <QMutex>
                #include "qexception.h"
                #include "qextserpw.h"
                #include "terminal.h"
                QT_BEGIN_NAMESPACE
                namespace Ui { class monMK; }
                QT_END_NAMESPACE
                
                class monMK : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit monMK(QWidget *parent = 0);
                    ~monMK();
                    unsigned long fls = 0 ;
                    QString dir_c_g;
                    QByteArray qbag = "{ \
                                         \"PORT\": \"COM3\", \
                                         \"RATE\": \"115200\", \
                                         \"PARITY\": \"0\", \
                                         \"STOPBIT\": \"2\", \
                                         \"BITS\": \"8\", \
                                         \"LD_ADDR\": \"FFFFFFFFB5000020\", \
                                         \"ST_ADDR\": \"FFFFFFFFB5000020\", \
                                         \"MEM_TYPE\": \"LWord\", \
                                         \"MEM_ADDR\": \"FFFFFFFFB5000000\", \
                                         \"FILE_LOAD\": \"C:\" \
                                         }";
                
                
                    ChoicePropertisePort *dialogPort = new ChoicePropertisePort;
                    JSON *fs2 = new JSON();
                    QProgressDialog *bar = new QProgressDialog(this);
                    char mu;
                          int div ;
                          int mod ;
                          QByteArray y;     // y ==
                	qextserpw  *qextser = new qextserpw;
                    QString temps;
                    unsigned char glb = 0;
                    unsigned char  globalf ;
                    QByteArray charBuffer;
                    Terminal *textTerminal = new Terminal();
                	QByteArray arr;
                protected:
                	void closeEvent(QCloseEvent *event);    /
                public slots:
                    void readData(QByteArray data);
                	void load_pr(int ii);
                	void writeData();
                
                private slots:
                    void on_load_clicked();
                
                private:
                    Ui::monMK *ui;
                signals:
                    void to_store();
                };
                #endif // MONMK_H
                

                monMK.c

                
                #include "monmk.h"
                #include "ui_monmk.h"
                #include "choicepropertiseport.h"
                #include <QtDebug>
                #include <QDesktopWidget>
                #include <QDateTime>
                #include <QMessageBox>
                #include <QLabel>
                #include <QtGlobal>
                #include <QtCore>
                #include <QtEndian>
                #include <QProgressDialog>
                #include <QJsonDocument>
                #include <QDir>
                #include <QFileDialog>
                #include <json.h>
                #include "crc32.h"
                #include <QString>
                #include <QByteArray>
                #include <qextserialport_p.h>
                #include <QDebug>
                #include <qextserpw.h>
                
                monMK::monMK(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::monMK)
                {
                    ui->setupUi(this);
                    this->setFixedSize(this->geometry().width(),this->geometry().height()); // set fixed size
                    start_t start_st;
                    ui->label_4->setPixmap(QPixmap(":/images/progress.png"));
                    textTerminal->setWindowTitle("PortTerminal");
                    textTerminal->setGeometry(1156, 341, 382, 282);
                    textTerminal->show();
                    ui->pushButton_5->setEnabled(false);
                    connect(fs2, SIGNAL(valueChanged(int)), this, SLOT(JFS(int)));
                    connect(qextser, &qextserpw::inprogress, this, &monMK::load_pr);
                    connect(qextser, &qextserpw::to_indi, this, &monMK::readData); // onPortReadReady instead of "m_readAll
                	connect(this, &monMK::to_store, this, &monMK::writeData); // onPortReadReady instead of "m_readAll
                   
                    qextser->status_port =   qextser->m_open(QIODevice::ReadWrite| QIODevice::Unbuffered);
                    bool check_open = false;
                        if (!(qextser->status_port)) textTerminal->addText(tr("%1 poer is not initialized\r\n").arg(g_portname));
                        else
                        {
                            ....
                        }
                
                }
                
                
                void monMK::load_pr(int ii)
                {
                    bar->setValue(ii);
                    qDebug() << "ii progress bar value" << ii;
                }
                
                void monMK::writeData()
                {
                    unsigned long ii;           
                	bar->setRange(1, fls-1); 
                	bar->setValue(1);
                	for(ii=0;ii<fls;ii++)
                	{
                		y = arr.mid(ii, 1 );     
                		qextser->write(y);//,1);
                		emit load_pr(ii);             
                		qDebug() << "interation = " << ii;
                	}              
                }
                
                void monMK::readData(QByteArray data)
                {
                    qDebug() << "ready read";
                	textTerminal->insert_Text((QString)(data));
                    qDebug() << "working";
                }
                
                
                
                void monMK::writeButton_clicked()
                {
                    QFile file;
                    if (!file.open(QIODevice::ReadOnly))	
                    {
                        QMessageBox::information(this, tr("Can't open the file"),
                        file.errorString());
                        return;
                    }
                    else
                     {
                                    qDebug() << "opening file";
                                    fls = file.size();
                                    arr.resize(fls);
                                    qDebug() << "size of" << fls;
                                    for(unsigned long i; i<fls;i++)
                                    {arr = 0;}
                                    if( arr.isEmpty())
                						qDebug() << "at first array is empty";
                                    else
                						qDebug() <<  "at first array isn't empty";
                                    arr = file.readAll();
                                    file.close();
                                    qDebug() << "closing file";
                                   
                
                     }
                
                        if(qextser->status_port)
                         //if(port->isOpen())
                         {
                           QString str = "";
                           str.append(arr);
                           bar = new QProgressDialog(this);
                           qextser->write(dataArr4);
                           bar->setLabelText(tr("Download..."));
                           bar->setCancelButtonText(tr("&Cancel"));
                           bar->setMinimumDuration(0); 
                           bar->setWindowTitle(tr("Download"));
                           bar->setVisible(1);
                           emit to_store();
                          
                         }
                
                }
                
                
                
                

                main.cpp

                #include "monmk.h"
                #include "qextserpw.h"
                #include <QApplication>
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    monMK w;
                    w.show();
                    qextserpw();
                    return a.exec();
                }
                
                
                SGaistS 1 Reply Last reply
                0
                • A another_one

                  Thanks to all!

                  I think I took into account all recomendations, except one - relates to events
                  Unfortunally I haven't got any excperiance in Qt event loop, QCoreApplication::processEvents etc

                  And I asking help in how to do in thread-safe manner this case of writing with progress bar

                  Here is my code which still is black screen in CentOS only:

                  qextserpw.h

                  #ifndef QEXTSERPW_H
                  #define QEXTSERPW_H
                  
                  #include <QObject>
                  #include <QtCore/QIODevice>
                  #include <qextserialport.h>
                  
                  #include <QDebug>
                  #include "terminal.h"
                  
                  
                  class qextserpw : public QObject
                  {
                      Q_OBJECT
                  public:
                      explicit qextserpw();
                      void write(const QByteArray &writeData);
                      QextSerialPort *port = new QextSerialPort (QextSerialPort::EventDriven);
                      QByteArray* global_data;
                      bool m_open(QIODevice::OpenMode mode);
                     	bool status_port;
                  signals:
                      void inprogress(long data);
                      void to_store();
                      void to_indi(QByteArray data);
                   public slots:
                      //void handleBytesWritten(qint64 bytes);
                  	void m_readAll();
                  private:
                      //QextSerialPort *m_serialPort = nullptr;
                      //QByteArray m_writeData;
                      //qint64 m_bytesWritten = 0;
                  
                  };
                  
                  #endif // QEXTSERPW_H
                  

                  qextserpw.c

                  
                  #include "qextserpw.h"
                  #include "monmk.h"
                  #include "terminal.h"
                  #include <QDebug>
                  #include <QtGlobal>
                  #include <QtCore>
                  #include <QProgressDialog>
                  #include <QtCore/QIODevice>
                  
                   qextserpw::qextserpw()
                  {
                      port = new QextSerialPort (QextSerialPort::EventDriven);  
                      port->setTimeout(1);
                      connect(port, SIGNAL(readyRead()), this, SLOT(m_readAll()));
                      qDebug() << "another class created";
                   }
                  
                  void qextserpw::handleBytesWritten(qint64 bytes)
                  {
                  
                  }
                  
                  void qextserpw::write(const QByteArray &writeData)
                  {
                      port->write(writeData);
                  }
                  
                  bool qextserpw::m_open(QIODevice::OpenMode mode)
                  {bool rv=false;
                      rv = port->open(mode);
                       qDebug() << "port is open from another class" ;
                      return rv;
                  }
                  bool qextserpw::check_open()
                  {bool rv=false;
                   rv = port->isOpen();
                  return rv;
                  }
                  void qextserpw::m_readAll()
                  { QByteArray temp;
                  
                      temp = port->readAll();
                  	global_data = &temp;
                  	emit to_indi(*global_data);
                      qDebug() << "read from another thread!";
                  }
                  void qextserpw::m_port_close()
                  {
                      port->flush();
                      port->close();
                  }
                  

                  monMK.h

                  
                  #ifndef MONMK_H
                  #define MONMK_H
                  #include "qextserialport.h"
                  #include "qextserialport_p.h"
                  #include <QFileDialog>
                  #include <QFile>
                  #include <QDir>
                  #include <QMainWindow>
                  #include <QTimer>
                  #include "terminal.h"
                  #include "json.h"
                  #include "choicepropertiseport.h"
                  #include <QMainWindow>
                  #include <QProgressDialog>
                  #include <QMutex>
                  #include "qexception.h"
                  #include "qextserpw.h"
                  #include "terminal.h"
                  QT_BEGIN_NAMESPACE
                  namespace Ui { class monMK; }
                  QT_END_NAMESPACE
                  
                  class monMK : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit monMK(QWidget *parent = 0);
                      ~monMK();
                      unsigned long fls = 0 ;
                      QString dir_c_g;
                      QByteArray qbag = "{ \
                                           \"PORT\": \"COM3\", \
                                           \"RATE\": \"115200\", \
                                           \"PARITY\": \"0\", \
                                           \"STOPBIT\": \"2\", \
                                           \"BITS\": \"8\", \
                                           \"LD_ADDR\": \"FFFFFFFFB5000020\", \
                                           \"ST_ADDR\": \"FFFFFFFFB5000020\", \
                                           \"MEM_TYPE\": \"LWord\", \
                                           \"MEM_ADDR\": \"FFFFFFFFB5000000\", \
                                           \"FILE_LOAD\": \"C:\" \
                                           }";
                  
                  
                      ChoicePropertisePort *dialogPort = new ChoicePropertisePort;
                      JSON *fs2 = new JSON();
                      QProgressDialog *bar = new QProgressDialog(this);
                      char mu;
                            int div ;
                            int mod ;
                            QByteArray y;     // y ==
                  	qextserpw  *qextser = new qextserpw;
                      QString temps;
                      unsigned char glb = 0;
                      unsigned char  globalf ;
                      QByteArray charBuffer;
                      Terminal *textTerminal = new Terminal();
                  	QByteArray arr;
                  protected:
                  	void closeEvent(QCloseEvent *event);    /
                  public slots:
                      void readData(QByteArray data);
                  	void load_pr(int ii);
                  	void writeData();
                  
                  private slots:
                      void on_load_clicked();
                  
                  private:
                      Ui::monMK *ui;
                  signals:
                      void to_store();
                  };
                  #endif // MONMK_H
                  

                  monMK.c

                  
                  #include "monmk.h"
                  #include "ui_monmk.h"
                  #include "choicepropertiseport.h"
                  #include <QtDebug>
                  #include <QDesktopWidget>
                  #include <QDateTime>
                  #include <QMessageBox>
                  #include <QLabel>
                  #include <QtGlobal>
                  #include <QtCore>
                  #include <QtEndian>
                  #include <QProgressDialog>
                  #include <QJsonDocument>
                  #include <QDir>
                  #include <QFileDialog>
                  #include <json.h>
                  #include "crc32.h"
                  #include <QString>
                  #include <QByteArray>
                  #include <qextserialport_p.h>
                  #include <QDebug>
                  #include <qextserpw.h>
                  
                  monMK::monMK(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::monMK)
                  {
                      ui->setupUi(this);
                      this->setFixedSize(this->geometry().width(),this->geometry().height()); // set fixed size
                      start_t start_st;
                      ui->label_4->setPixmap(QPixmap(":/images/progress.png"));
                      textTerminal->setWindowTitle("PortTerminal");
                      textTerminal->setGeometry(1156, 341, 382, 282);
                      textTerminal->show();
                      ui->pushButton_5->setEnabled(false);
                      connect(fs2, SIGNAL(valueChanged(int)), this, SLOT(JFS(int)));
                      connect(qextser, &qextserpw::inprogress, this, &monMK::load_pr);
                      connect(qextser, &qextserpw::to_indi, this, &monMK::readData); // onPortReadReady instead of "m_readAll
                  	connect(this, &monMK::to_store, this, &monMK::writeData); // onPortReadReady instead of "m_readAll
                     
                      qextser->status_port =   qextser->m_open(QIODevice::ReadWrite| QIODevice::Unbuffered);
                      bool check_open = false;
                          if (!(qextser->status_port)) textTerminal->addText(tr("%1 poer is not initialized\r\n").arg(g_portname));
                          else
                          {
                              ....
                          }
                  
                  }
                  
                  
                  void monMK::load_pr(int ii)
                  {
                      bar->setValue(ii);
                      qDebug() << "ii progress bar value" << ii;
                  }
                  
                  void monMK::writeData()
                  {
                      unsigned long ii;           
                  	bar->setRange(1, fls-1); 
                  	bar->setValue(1);
                  	for(ii=0;ii<fls;ii++)
                  	{
                  		y = arr.mid(ii, 1 );     
                  		qextser->write(y);//,1);
                  		emit load_pr(ii);             
                  		qDebug() << "interation = " << ii;
                  	}              
                  }
                  
                  void monMK::readData(QByteArray data)
                  {
                      qDebug() << "ready read";
                  	textTerminal->insert_Text((QString)(data));
                      qDebug() << "working";
                  }
                  
                  
                  
                  void monMK::writeButton_clicked()
                  {
                      QFile file;
                      if (!file.open(QIODevice::ReadOnly))	
                      {
                          QMessageBox::information(this, tr("Can't open the file"),
                          file.errorString());
                          return;
                      }
                      else
                       {
                                      qDebug() << "opening file";
                                      fls = file.size();
                                      arr.resize(fls);
                                      qDebug() << "size of" << fls;
                                      for(unsigned long i; i<fls;i++)
                                      {arr = 0;}
                                      if( arr.isEmpty())
                  						qDebug() << "at first array is empty";
                                      else
                  						qDebug() <<  "at first array isn't empty";
                                      arr = file.readAll();
                                      file.close();
                                      qDebug() << "closing file";
                                     
                  
                       }
                  
                          if(qextser->status_port)
                           //if(port->isOpen())
                           {
                             QString str = "";
                             str.append(arr);
                             bar = new QProgressDialog(this);
                             qextser->write(dataArr4);
                             bar->setLabelText(tr("Download..."));
                             bar->setCancelButtonText(tr("&Cancel"));
                             bar->setMinimumDuration(0); 
                             bar->setWindowTitle(tr("Download"));
                             bar->setVisible(1);
                             emit to_store();
                            
                           }
                  
                  }
                  
                  
                  
                  

                  main.cpp

                  #include "monmk.h"
                  #include "qextserpw.h"
                  #include <QApplication>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      monMK w;
                      w.show();
                      qextserpw();
                      return a.exec();
                  }
                  
                  
                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #29

                  You are leaking QextSerialPort objects in your qextserpw class.

                  For some reason, you are storing the address of a local QByteArray in a class member variable which is wrong.

                  In your widget class you are updating qextserpw member variables using other member variables.

                  You are also creating an instance of qextserpw that will never exist in your main function.

                  I don't want to sound rude but this code is going to be more of a nightmare to maintain and get working than a good starting point to achieve your goal.

                  If I may, you should start with a really minimal application that properly sends the data through your serial port and just print the progress. Then add the progress bar to replace your print. Once you have that cleanly and properly working you can build your full GUI.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  A 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    You are leaking QextSerialPort objects in your qextserpw class.

                    For some reason, you are storing the address of a local QByteArray in a class member variable which is wrong.

                    In your widget class you are updating qextserpw member variables using other member variables.

                    You are also creating an instance of qextserpw that will never exist in your main function.

                    I don't want to sound rude but this code is going to be more of a nightmare to maintain and get working than a good starting point to achieve your goal.

                    If I may, you should start with a really minimal application that properly sends the data through your serial port and just print the progress. Then add the progress bar to replace your print. Once you have that cleanly and properly working you can build your full GUI.

                    A Offline
                    A Offline
                    another_one
                    wrote on last edited by
                    #30

                    @SGaist
                    Thank you for your replay!
                    I don't understand, if it's so bad construction of my code why it's worked on Windows(include progress bar)
                    At first when I heard about qt I heard about cross platform library first
                    But in practice It requires some construction (not only signals and slots) to be cross platform
                    And I still couldn't get that even I asking in specialized forum

                    A 1 Reply Last reply
                    0
                    • A another_one

                      @SGaist
                      Thank you for your replay!
                      I don't understand, if it's so bad construction of my code why it's worked on Windows(include progress bar)
                      At first when I heard about qt I heard about cross platform library first
                      But in practice It requires some construction (not only signals and slots) to be cross platform
                      And I still couldn't get that even I asking in specialized forum

                      A Offline
                      A Offline
                      another_one
                      wrote on last edited by another_one
                      #31

                      @another_one

                      Thanks to all again!
                      Finally I heard and do what
                      I have chenged the following:
                      in class qextserpw

                      
                      public:
                       QByteArray global_data;
                      signals:
                          void inprogress(long data);
                          void to_store();
                          void to_indi(QByteArray data);
                      
                      void qextserpw::m_readAll()
                      { QByteArray temp;
                      
                          global_data = port->readAll();//&temp;
                          emit to_indi(global_data);
                          qDebug() << "read from another thread!";
                      }
                      
                      

                      in main

                      
                      #include "monmk.h"
                      #include "qextserpw.h"
                      #include <QApplication>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          monMK w;
                          w.show();
                      	a.processEvents();
                          return a.exec();
                      }
                      

                      in class monMK

                      void monMK::writeData()
                      {
                          unsigned long ii;
                          bar->setRange(1, fls-1); 
                          bar->setValue(1);
                          for(ii=0;ii<fls;ii++)
                          {
                      		y = arr.mid(ii, 1 );    
                      		qextser->write(y);
                      		if(ii % fls == 0)
                      		qApp->processEvents();
                      		emit load_pr(ii);
                      		QCoreApplication::processEvents();
                      		qDebug() << "interation = " << ii;
                          }
                      
                      }
                      

                      But in CentOS I still got some another problem which relates to form and widjets scale
                      Widgets can not fit form as well as in windows

                      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