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. problem with writing on serial port
Qt 6.11 is out! See what's new in the release blog

problem with writing on serial port

Scheduled Pinned Locked Moved Unsolved General and Desktop
36 Posts 4 Posters 5.3k 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.
  • N Offline
    N Offline
    nanor
    wrote on last edited by nanor
    #1
    This post is deleted!
    jsulmJ 1 Reply Last reply
    0
    • N nanor

      This post is deleted!

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @nanor said in problem with writing on serial port:

      while(1)
      {
      serial->write("ok");

                }
      

      Please don't do such things in event driven applications! You block Qt event loop!
      Write once, connect a slot to https://doc.qt.io/qt-5/qiodevice.html#bytesWritten and write again in that slot if you need to write "OK" more than once.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      2
      • jsulmJ jsulm

        @nanor said in problem with writing on serial port:

        while(1)
        {
        serial->write("ok");

                  }
        

        Please don't do such things in event driven applications! You block Qt event loop!
        Write once, connect a slot to https://doc.qt.io/qt-5/qiodevice.html#bytesWritten and write again in that slot if you need to write "OK" more than once.

        N Offline
        N Offline
        nanor
        wrote on last edited by
        #3
        This post is deleted!
        jsulmJ 1 Reply Last reply
        0
        • N nanor

          This post is deleted!

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @nanor Please show your code. The warning actually tells you something.
          Also, please post warnings/errors as text, not screen-shots.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          N 1 Reply Last reply
          0
          • jsulmJ jsulm

            @nanor Please show your code. The warning actually tells you something.
            Also, please post warnings/errors as text, not screen-shots.

            N Offline
            N Offline
            nanor
            wrote on last edited by
            #5

            @jsulm This is the updated code:

            widget.cpp:

            #include "widget.h"
            #include "ui_widget.h"
            #include <QDebug>
            #include <QtSerialPort/QSerialPort>
            #include <QSerialPortInfo>
            
            Widget::Widget(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::Widget)
            {
                    ui->setupUi(this);
            
                    serial = new QSerialPort(this);
            
                    serial->setPortName("COM1");
            
                    for(const auto &serialPortInfo : QSerialPortInfo::availablePorts())
                    {
                            qDebug() << "find serial port: " << serialPortInfo.portName() ;
                    }
            
                    serial->open(QIODevice::ReadWrite);
            
                    if(serial->isOpen())
                    {
                        serial->setBaudRate(QSerialPort::Baud9600);
                        serial->setDataBits(QSerialPort::Data8);
                        serial->setParity(QSerialPort::NoParity);
                        serial->setStopBits(QSerialPort::OneStop);
                        serial->setFlowControl(QSerialPort::NoFlowControl);
            
            
                       serial->write("ok");
                       connect(serial, SIGNAL(bytesWritten(qint64)), this, SLOT(Write(qint64)));
                        /*while(1)
                          {
                             serial->write("ok");
            
                          }*/
            
            
            
                    }
            
                    else
                    {
                      qDebug() << "can't open the port";
                    }
            
            }
            
            Widget::~Widget()
            {
                delete ui;
                serial->close();
            }
            
            void Widget::on_pushButton_clicked()
            {
                qDebug() << "salam";
            }
            
            void Widget::Write(qint64)
            {
                while(1)
                {
                    serial->write("ok");
                }
            }
            
            

            this is the error:

            07:22:37: Starting C:\Users\nanor\OneDrive\Desktop\qt codes\build-SerialPortReading-Desktop_Qt_5_12_2_MinGW_32_bit-Debug\debug\SerialPortReading.exe...
            find serial port: "COM1"
            find serial port: "COM6"
            terminate called after throwing an instance of 'std::bad_alloc'
            what(): std::bad_alloc
            07:23:15: C:/Users/nanor/OneDrive/Desktop/qt codes/build-SerialPortReading-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/SerialPortReading.exe exited with code 3

            jsulmJ 1 Reply Last reply
            0
            • N nanor

              @jsulm This is the updated code:

              widget.cpp:

              #include "widget.h"
              #include "ui_widget.h"
              #include <QDebug>
              #include <QtSerialPort/QSerialPort>
              #include <QSerialPortInfo>
              
              Widget::Widget(QWidget *parent) :
                  QWidget(parent),
                  ui(new Ui::Widget)
              {
                      ui->setupUi(this);
              
                      serial = new QSerialPort(this);
              
                      serial->setPortName("COM1");
              
                      for(const auto &serialPortInfo : QSerialPortInfo::availablePorts())
                      {
                              qDebug() << "find serial port: " << serialPortInfo.portName() ;
                      }
              
                      serial->open(QIODevice::ReadWrite);
              
                      if(serial->isOpen())
                      {
                          serial->setBaudRate(QSerialPort::Baud9600);
                          serial->setDataBits(QSerialPort::Data8);
                          serial->setParity(QSerialPort::NoParity);
                          serial->setStopBits(QSerialPort::OneStop);
                          serial->setFlowControl(QSerialPort::NoFlowControl);
              
              
                         serial->write("ok");
                         connect(serial, SIGNAL(bytesWritten(qint64)), this, SLOT(Write(qint64)));
                          /*while(1)
                            {
                               serial->write("ok");
              
                            }*/
              
              
              
                      }
              
                      else
                      {
                        qDebug() << "can't open the port";
                      }
              
              }
              
              Widget::~Widget()
              {
                  delete ui;
                  serial->close();
              }
              
              void Widget::on_pushButton_clicked()
              {
                  qDebug() << "salam";
              }
              
              void Widget::Write(qint64)
              {
                  while(1)
                  {
                      serial->write("ok");
                  }
              }
              
              

              this is the error:

              07:22:37: Starting C:\Users\nanor\OneDrive\Desktop\qt codes\build-SerialPortReading-Desktop_Qt_5_12_2_MinGW_32_bit-Debug\debug\SerialPortReading.exe...
              find serial port: "COM1"
              find serial port: "COM6"
              terminate called after throwing an instance of 'std::bad_alloc'
              what(): std::bad_alloc
              07:23:15: C:/Users/nanor/OneDrive/Desktop/qt codes/build-SerialPortReading-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/SerialPortReading.exe exited with code 3

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @nanor said in problem with writing on serial port:

              while(1)
              {
              serial->write("ok");
              }

              I already told you to not to block the event loop! Why do you do it again? Why do you think you need this while(1) loop?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              N 2 Replies Last reply
              3
              • jsulmJ jsulm

                @nanor said in problem with writing on serial port:

                while(1)
                {
                serial->write("ok");
                }

                I already told you to not to block the event loop! Why do you do it again? Why do you think you need this while(1) loop?

                N Offline
                N Offline
                nanor
                wrote on last edited by
                #7

                @jsulm I was asked to do this. I was asked to send a text in while(1) loop.

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @nanor said in problem with writing on serial port:

                  while(1)
                  {
                  serial->write("ok");
                  }

                  I already told you to not to block the event loop! Why do you do it again? Why do you think you need this while(1) loop?

                  N Offline
                  N Offline
                  nanor
                  wrote on last edited by
                  #8

                  @jsulm In the last uploaded code, I have commented the while loop inside the constructor and move this while loop to the slot

                  jsulmJ 1 Reply Last reply
                  0
                  • N nanor

                    @jsulm In the last uploaded code, I have commented the while loop inside the constructor and move this while loop to the slot

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @nanor said in problem with writing on serial port:

                    I have commented the while loop inside the constructor and move this while loop to the slot

                    So, you're again blocking the event loop.
                    Is this an exercise, or who asked you to use a loop?
                    If you have to do so, then you will need to move your serial port code to a thread.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    N 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @nanor said in problem with writing on serial port:

                      I have commented the while loop inside the constructor and move this while loop to the slot

                      So, you're again blocking the event loop.
                      Is this an exercise, or who asked you to use a loop?
                      If you have to do so, then you will need to move your serial port code to a thread.

                      N Offline
                      N Offline
                      nanor
                      wrote on last edited by
                      #10

                      @jsulm Yes this is an exercise. Thank you so much for your suggestion.

                      jsulmJ 1 Reply Last reply
                      0
                      • N nanor

                        @jsulm Yes this is an exercise. Thank you so much for your suggestion.

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #11

                        @nanor You could keep your loop and call https://doc.qt.io/qt-5/qcoreapplication.html#processEvents inside the loop. But warning: this is dirty work-around and probably not what the teacher wants to see :-)

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        N 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @nanor You could keep your loop and call https://doc.qt.io/qt-5/qcoreapplication.html#processEvents inside the loop. But warning: this is dirty work-around and probably not what the teacher wants to see :-)

                          N Offline
                          N Offline
                          nanor
                          wrote on last edited by
                          #12

                          @jsulm Thank you. You mentioned that if I want to do write my code in a while loop, I have to use thread. I have read about qthread class, but I don't know how to use this class in my project. Could you please give suggestions about the steps I have to take?
                          The question is:
                          read data continuously and byte by byte from a serial port and if data equals to something special that is defined in code (like"hi"), then write to serial port something.
                          I would appreciate if you guide me how to use threads in solving this question

                          jsulmJ 1 Reply Last reply
                          0
                          • N nanor

                            @jsulm Thank you. You mentioned that if I want to do write my code in a while loop, I have to use thread. I have read about qthread class, but I don't know how to use this class in my project. Could you please give suggestions about the steps I have to take?
                            The question is:
                            read data continuously and byte by byte from a serial port and if data equals to something special that is defined in code (like"hi"), then write to serial port something.
                            I would appreciate if you guide me how to use threads in solving this question

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @nanor This example does exactly what you need: https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html
                            It uses blocking QSerialPort API in a worker thread.
                            Take a look.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            N 1 Reply Last reply
                            2
                            • jsulmJ jsulm

                              @nanor This example does exactly what you need: https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html
                              It uses blocking QSerialPort API in a worker thread.
                              Take a look.

                              N Offline
                              N Offline
                              nanor
                              wrote on last edited by
                              #14

                              @jsulm Thank you so much.
                              For the firt step (I want to continuesly read data byte by byte) I have written the following code. But it crashes. Could you please guide me how to fix this?

                              mainwindow.h:

                              #ifndef MAINWINDOW_H
                              #define MAINWINDOW_H
                              
                              #include <QMainWindow>
                              #include <QtSerialPort/QSerialPort>
                              #include "mythread.h"
                              
                              namespace Ui {
                              class MainWindow;
                              }
                              
                              class MainWindow : public QMainWindow
                              {
                                  Q_OBJECT
                              
                              public:
                                  explicit MainWindow(QWidget *parent = nullptr);
                                  ~MainWindow();
                              
                                  QSerialPort *serial;
                              
                                  QThread *myThread;
                              
                              
                              private slots:
                                  void on_pushButton_clicked();
                              
                              private:
                                  Ui::MainWindow *ui;
                              };
                              
                              #endif // MAINWINDOW_H
                              

                              mythread.h:

                              #ifndef MYTHREAD_H
                              #define MYTHREAD_H
                              
                              #include <QObject>
                              #include <QThread>
                              #include <QtSerialPort/QSerialPort>
                              #include <QtSerialPort/QSerialPortInfo>
                              #include <QDebug>
                              
                              class MyThread : public QThread
                              {
                                  Q_OBJECT
                              public:
                                  explicit MyThread(QObject *parent = nullptr);
                              
                                  void run();
                              
                                  QSerialPort *serial;
                              
                              signals:
                              
                              public slots:
                              };
                              
                              #endif // MYTHREAD_H
                              
                              

                              mainwindow.cpp:

                              #include "mainwindow.h"
                              #include "ui_mainwindow.h"
                              #include <QtSerialPort/QSerialPort>
                              #include <QtSerialPort/QSerialPortInfo>
                              #include <QDebug>
                              
                              MainWindow::MainWindow(QWidget *parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                                  myThread->start();
                              
                              }
                              
                              MainWindow::~MainWindow()
                              {
                                  delete ui;
                              }
                              
                              void MainWindow::on_pushButton_clicked()
                              {
                                  qDebug() << "salam";
                              }
                              

                              mythread.cpp:

                              #include "mythread.h"
                              
                              MyThread::MyThread(QObject *parent) : QThread(parent)
                              {
                              
                              }
                              
                              void MyThread::run()
                              {
                                  serial = new QSerialPort(this);
                                  serial->setPortName("COM1");
                              
                                  for(const auto &serialPortInfo : QSerialPortInfo::availablePorts())
                                  {
                                     qDebug() << "find serial port: " << serialPortInfo.portName() ;
                                  }
                              
                                  serial->open(QIODevice::ReadWrite);
                              
                              
                                  if(serial->isOpen())
                                  {
                                      serial->setBaudRate(QSerialPort::Baud9600);
                                      serial->setDataBits(QSerialPort::Data8);
                                      serial->setParity(QSerialPort::NoParity);
                                      serial->setStopBits(QSerialPort::OneStop);
                                      serial->setFlowControl(QSerialPort::NoFlowControl);
                              
                                       while(1)
                                      {
                                          serial->read(1);
                              
                                     }
                              
                                  }
                              
                                  else
                                  {
                                    qDebug() << "can't open the port";
                                  }
                              
                              }
                              
                              

                              the error:

                              10:33:50: Starting C:\Users\nanor\OneDrive\Desktop\qt codes\build-serialportreading3-Desktop_Qt_5_12_2_MinGW_32_bit-Debug\debug\serialportreading3.exe...
                              ASSERT: "timeout >= 0" in file thread\qmutex.cpp, line 582
                              10:33:52: The program has unexpectedly finished.
                              10:33:52: The process was ended forcefully.
                              10:33:52: C:/Users/nanor/OneDrive/Desktop/qt codes/build-serialportreading3-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/serialportreading3.exe crashed.

                              jsulmJ 1 Reply Last reply
                              0
                              • N nanor

                                @jsulm Thank you so much.
                                For the firt step (I want to continuesly read data byte by byte) I have written the following code. But it crashes. Could you please guide me how to fix this?

                                mainwindow.h:

                                #ifndef MAINWINDOW_H
                                #define MAINWINDOW_H
                                
                                #include <QMainWindow>
                                #include <QtSerialPort/QSerialPort>
                                #include "mythread.h"
                                
                                namespace Ui {
                                class MainWindow;
                                }
                                
                                class MainWindow : public QMainWindow
                                {
                                    Q_OBJECT
                                
                                public:
                                    explicit MainWindow(QWidget *parent = nullptr);
                                    ~MainWindow();
                                
                                    QSerialPort *serial;
                                
                                    QThread *myThread;
                                
                                
                                private slots:
                                    void on_pushButton_clicked();
                                
                                private:
                                    Ui::MainWindow *ui;
                                };
                                
                                #endif // MAINWINDOW_H
                                

                                mythread.h:

                                #ifndef MYTHREAD_H
                                #define MYTHREAD_H
                                
                                #include <QObject>
                                #include <QThread>
                                #include <QtSerialPort/QSerialPort>
                                #include <QtSerialPort/QSerialPortInfo>
                                #include <QDebug>
                                
                                class MyThread : public QThread
                                {
                                    Q_OBJECT
                                public:
                                    explicit MyThread(QObject *parent = nullptr);
                                
                                    void run();
                                
                                    QSerialPort *serial;
                                
                                signals:
                                
                                public slots:
                                };
                                
                                #endif // MYTHREAD_H
                                
                                

                                mainwindow.cpp:

                                #include "mainwindow.h"
                                #include "ui_mainwindow.h"
                                #include <QtSerialPort/QSerialPort>
                                #include <QtSerialPort/QSerialPortInfo>
                                #include <QDebug>
                                
                                MainWindow::MainWindow(QWidget *parent) :
                                    QMainWindow(parent),
                                    ui(new Ui::MainWindow)
                                {
                                    ui->setupUi(this);
                                    myThread->start();
                                
                                }
                                
                                MainWindow::~MainWindow()
                                {
                                    delete ui;
                                }
                                
                                void MainWindow::on_pushButton_clicked()
                                {
                                    qDebug() << "salam";
                                }
                                

                                mythread.cpp:

                                #include "mythread.h"
                                
                                MyThread::MyThread(QObject *parent) : QThread(parent)
                                {
                                
                                }
                                
                                void MyThread::run()
                                {
                                    serial = new QSerialPort(this);
                                    serial->setPortName("COM1");
                                
                                    for(const auto &serialPortInfo : QSerialPortInfo::availablePorts())
                                    {
                                       qDebug() << "find serial port: " << serialPortInfo.portName() ;
                                    }
                                
                                    serial->open(QIODevice::ReadWrite);
                                
                                
                                    if(serial->isOpen())
                                    {
                                        serial->setBaudRate(QSerialPort::Baud9600);
                                        serial->setDataBits(QSerialPort::Data8);
                                        serial->setParity(QSerialPort::NoParity);
                                        serial->setStopBits(QSerialPort::OneStop);
                                        serial->setFlowControl(QSerialPort::NoFlowControl);
                                
                                         while(1)
                                        {
                                            serial->read(1);
                                
                                       }
                                
                                    }
                                
                                    else
                                    {
                                      qDebug() << "can't open the port";
                                    }
                                
                                }
                                
                                

                                the error:

                                10:33:50: Starting C:\Users\nanor\OneDrive\Desktop\qt codes\build-serialportreading3-Desktop_Qt_5_12_2_MinGW_32_bit-Debug\debug\serialportreading3.exe...
                                ASSERT: "timeout >= 0" in file thread\qmutex.cpp, line 582
                                10:33:52: The program has unexpectedly finished.
                                10:33:52: The process was ended forcefully.
                                10:33:52: C:/Users/nanor/OneDrive/Desktop/qt codes/build-serialportreading3-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/serialportreading3.exe crashed.

                                jsulmJ Online
                                jsulmJ Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @nanor said in problem with writing on serial port:

                                serial = new QSerialPort(this);

                                Remove "this".
                                If it is still crashing then please use the debugger to see where exactly and post the stack trace after crash.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                N 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @nanor said in problem with writing on serial port:

                                  serial = new QSerialPort(this);

                                  Remove "this".
                                  If it is still crashing then please use the debugger to see where exactly and post the stack trace after crash.

                                  N Offline
                                  N Offline
                                  nanor
                                  wrote on last edited by
                                  #16
                                  This post is deleted!
                                  jsulmJ 1 Reply Last reply
                                  0
                                  • N nanor

                                    This post is deleted!

                                    jsulmJ Online
                                    jsulmJ Online
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @nanor Please build in debug mode before running in debugger!

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    N 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @nanor Please build in debug mode before running in debugger!

                                      N Offline
                                      N Offline
                                      nanor
                                      wrote on last edited by nanor
                                      #18
                                      This post is deleted!
                                      jsulmJ 1 Reply Last reply
                                      0
                                      • N nanor

                                        This post is deleted!

                                        jsulmJ Online
                                        jsulmJ Online
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @nanor Please post the stack trace (as text). It's in the middle of your screen-shot, in "Debugger" section.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        N 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @nanor Please post the stack trace (as text). It's in the middle of your screen-shot, in "Debugger" section.

                                          N Offline
                                          N Offline
                                          nanor
                                          wrote on last edited by
                                          #20
                                          This post is deleted!
                                          jsulmJ 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