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. [Solved] QSerialPort - error message: QIODevice::read: device not open

[Solved] QSerialPort - error message: QIODevice::read: device not open

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 16.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.
  • F Offline
    F Offline
    flek12345
    wrote on last edited by
    #1

    I have STM Discovery board. It sends some dummy data over virtual COM port. When I start terminal (Tera Term), I can choose "COM5: STMicroelectronics Virtual COM Port (COM5)". And I see dummy data displayed in terminal.

    Now, I want to create application which will read data from that serial port (later I will process that data and display it in some other format - but it is irrelevant for now).
    But, there is a problem with serial port. I got an error "QIODevice::read: device not open".
    Ofcourse: when I start my application, TeraTerm terminal is closed.
    I am using QtCreator (3.2.1) in Windows environment.

    I noticed that function "open" always return "false".

    This is a my code segment:
    @
    QSerialPort *serial1;
    char buffer[50];

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    serial1 = new QSerialPort(this);
    serial1->setPortName("COM5");
    
    if (serial1->open(QIODevice::ReadWrite))
    {
        qDebug("TRUE") ;
    
        serial1->setBaudRate(QSerialPort::Baud9600);
        serial1->setDataBits(QSerialPort::Data8);
        serial1->setParity(QSerialPort::NoParity);
        serial1->setStopBits(QSerialPort::OneStop);
        serial1->setFlowControl(QSerialPort::NoFlowControl);
    }
    else
    {
        qDebug("FALSE") ;
    }
    
    serial1->read(buffer, 50);
    
    ui->plainTextEdit->insertPlainText("QWERTY!\n");
    

    }
    @

    What am I doing wrong?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis
      Qt Champions 2020
      wrote on last edited by
      #2

      You should to know a reason why a port is not opened. So, for this need to print out an error string or an error code.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        flek12345
        wrote on last edited by
        #3

        This are error code and error string.
        They are the same regardles what I put as parameter in function "open". I tried: QIODevice::ReadWrite, QIODevice::ReadOnly, QIODevice::WriteOnly. I even tried QSerialPort::ReadWrite, QSerialPort::ReadOnly,QSerialPort::WriteOnly

        error code = 10
        error string = "The parameter is incorrect."

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vezprog
          wrote on last edited by
          #4

          kuzulis is correct, there are a couple class functions for getting the last error which would be your port error.

          Use errorString() and print it out to qDebug() or use error() and check the enumeration value which can be found in the documentation for QSerialPort.

          Edit:
          QSerialPort::UnsupportedOperationError - 10 - The requested device operation is not supported or prohibited by the running operating system.

          is QSerialPort *serial in your MainWindow header?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            flek12345
            wrote on last edited by
            #5

            I do not undestand this:
            is QSerialPort *serial in your MainWindow header?

            This is all code from mainwindow.h
            @
            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H

            #include <QMainWindow>

            namespace Ui {
            class MainWindow;
            }

            class MainWindow : public QMainWindow
            {
            Q_OBJECT

            public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();

            private:
            Ui::MainWindow *ui;
            };

            #endif // MAINWINDOW_H
            @

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vezprog
              wrote on last edited by
              #6

              You are using the allocated pointer as a static parameter. I am not sure if this is your problem, but try putting them as private members in your main window class header. So,

              @
              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();

              private:
              Ui::MainWindow *ui;
              QSerialPort *serial1;
              char buffer[50];
              };
              @

              1 Reply Last reply
              0
              • F Offline
                F Offline
                flek12345
                wrote on last edited by
                #7

                I put it as private members, but it did not helped.

                Same error as before:
                error code = 10
                error string = "The parameter is incorrect."

                Also, I did something with QSerialPortInfo, and I got this
                Number of serial ports: 3
                Name: "COM5"
                Description: "STMicroelectronics Virtual COM Port"
                Manufactures: "STMicroelectronics."
                Unable to open port, error code 10

                It seems virtual serial port exist, but I still can not open it...

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kuzulis
                  Qt Champions 2020
                  wrote on last edited by
                  #8

                  Probably that a port (a driver of port) does not support of some things at opening. You need to check yourself what is it.. E.g. you should to debug of QSerialPortPrivate::open() and QSerialPortPrivate::initialize() method. This problem can be resolve only with debugging on your side.

                  UPD: or, you can provide a team-viewer session and then I can try to help.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    flek12345
                    wrote on last edited by
                    #9

                    I will try to debug those 2 functions.

                    I am new in using Qt, so - it will take me some time...

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      flek12345
                      wrote on last edited by
                      #10

                      I was unable to debug those functions (QSerialPortPrivate::open() and QSerialPortPrivate::initialize()). To be honest, I did not see them. I see only QSerialPort::open(). Without this "Private" part.

                      In debug mode, when I use "step into" during execution of "open" I can get only assembler code. And it is quite difficult to understand and debug...

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kuzulis
                        Qt Champions 2020
                        wrote on last edited by
                        #11

                        You should do debugging from the QtSerialPort sources.

                        Just try to:

                        • download a sources
                        • open a project from the QtCreator
                        • choose the "debug" target
                        • add/write to the make step options the "all" text (this allow to build the examples)
                        • try to rebuild and be convinced that an examples also has been compiled
                        • choose from the QtCreator the "terminal" example to run
                        • setup a breack point on QSerialPortPrivate::open()
                        • run the "terminal" application (from QtCreator)
                        • open in the "terminal" your serial port and to click on "connect" button
                        • go to break point..
                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          flek12345
                          wrote on last edited by
                          #12

                          I do not know what happened...
                          Suddenly, code is working without any problem.
                          There was no major change in code...

                          @
                          #include "mainwindow.h"
                          #include "ui_mainwindow.h"
                          #include <QSerialPort>
                          #include <QDebug>

                          MainWindow::MainWindow(QWidget *parent) :
                          QMainWindow(parent),
                          ui(new Ui::MainWindow)
                          {
                          ui->setupUi(this);

                          initComPort();
                          connect(serial1, SIGNAL(readyRead()), this, SLOT(readDataFromDiscovery()));
                          

                          }

                          MainWindow::~MainWindow()
                          {
                          delete ui;
                          }

                          void MainWindow::readDataFromDiscovery()
                          {
                          serial1->readLine(buffer,50);
                          ui->plainTextEdit->insertPlainText(buffer);
                          }

                          void MainWindow::initComPort(void)
                          {
                          serial1 = new QSerialPort(this);
                          serial1->setPortName("COM5");

                          if (serial1->open(QSerialPort::ReadOnly))
                          {
                              qDebug("SERIAL PORT - OPENED") ;
                              serial1->setBaudRate(QSerialPort::Baud9600);
                              serial1->setDataBits(QSerialPort::Data8);
                              serial1->setParity(QSerialPort::NoParity);
                              serial1->setStopBits(QSerialPort::OneStop);
                              serial1->setFlowControl(QSerialPort::NoFlowControl);
                          }
                          else
                          {
                              qDebug("SERIAL PORT - NOT OPENED") ;
                              qDebug() << "error code = " << serial1->error();
                              qDebug() << "error string = " << serial1->errorString();
                          }
                          

                          }@

                          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