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. QIODevice::write (QSerialPort): device not open
QtWS25 Last Chance

QIODevice::write (QSerialPort): device not open

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5serialportcom
5 Posts 4 Posters 3.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.
  • D Offline
    D Offline
    deleted286
    wrote on 22 Jan 2021, 11:32 last edited by
    #1

    When i debug, it says command aborted. I couldnt find my mistake

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSerialPort>
    #include <QDebug>
    
    QSerialPort *serial;
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        serial = new QSerialPort(this);
        serial->setPortName("com4");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        serial->write("its working");
        connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        serial->close();
    }
    
    void MainWindow::serialReceived()
    {
        QByteArray ba;
        ba=serial->readAll();
        ui->label->setText(ba);
        qDebug() << ba;
    }
    
    J J 2 Replies Last reply 22 Jan 2021, 11:42
    0
    • D deleted286
      22 Jan 2021, 11:32

      When i debug, it says command aborted. I couldnt find my mistake

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QSerialPort>
      #include <QDebug>
      
      QSerialPort *serial;
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          serial = new QSerialPort(this);
          serial->setPortName("com4");
          serial->setBaudRate(QSerialPort::Baud9600);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          serial->open(QIODevice::ReadWrite);
          serial->write("its working");
          connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
          serial->close();
      }
      
      void MainWindow::serialReceived()
      {
          QByteArray ba;
          ba=serial->readAll();
          ui->label->setText(ba);
          qDebug() << ba;
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 22 Jan 2021, 11:42 last edited by
      #2

      @suslucoder said in QIODevice::write (QSerialPort): device not open:

      serial->open(QIODevice::ReadWrite);

      Check whether open() succeeded.

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

      D 1 Reply Last reply 22 Jan 2021, 11:59
      5
      • J jsulm
        22 Jan 2021, 11:42

        @suslucoder said in QIODevice::write (QSerialPort): device not open:

        serial->open(QIODevice::ReadWrite);

        Check whether open() succeeded.

        D Offline
        D Offline
        deleted286
        wrote on 22 Jan 2021, 11:59 last edited by
        #3

        @jsulm I checked. Its not working. It returns false. Why should it be?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Jan 2021, 12:06 last edited by
          #4

          Hi,

          Port already opened in another application.
          Not enough user rights to access it.

          There's an error method that will give you more information.

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

          1 Reply Last reply
          4
          • D deleted286
            22 Jan 2021, 11:32

            When i debug, it says command aborted. I couldnt find my mistake

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QSerialPort>
            #include <QDebug>
            
            QSerialPort *serial;
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                serial = new QSerialPort(this);
                serial->setPortName("com4");
                serial->setBaudRate(QSerialPort::Baud9600);
                serial->setDataBits(QSerialPort::Data8);
                serial->setParity(QSerialPort::NoParity);
                serial->setStopBits(QSerialPort::OneStop);
                serial->setFlowControl(QSerialPort::NoFlowControl);
                serial->open(QIODevice::ReadWrite);
                serial->write("its working");
                connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
            
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
                serial->close();
            }
            
            void MainWindow::serialReceived()
            {
                QByteArray ba;
                ba=serial->readAll();
                ui->label->setText(ba);
                qDebug() << ba;
            }
            
            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 22 Jan 2021, 12:31 last edited by J.Hilk
            #5

            @suslucoder are you sure com4 is the correct name ? something seems of, I would suggest using https://doc.qt.io/qt-5/qserialportinfo.html
            to query for the correct port and port name


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            4

            1/5

            22 Jan 2021, 11:32

            • Login

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