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. help solve QIODevice::write (QSerialPort): device not open
Forum Updated to NodeBB v4.3 + New Features

help solve QIODevice::write (QSerialPort): device not open

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.2k Views 1 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.
  • R Offline
    R Offline
    Ryuzo
    wrote on last edited by
    #1

    hello everyone
    I've been trying to write data sent by Arduino it says QIODevice::write (QSerialPort): device not open please help me.

    #include "mainwindow.h"
    #include <QPushButton>
    #include "ui_mainwindow.h"
    #include "form.h"
    #include <QtSerialPort/QSerialPort>
    #include <QDebug>
    #include <QList>
    #include <QSerialPortInfo>
    #include <QMessageBox>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    
    this->setStyleSheet("MainWindow {background-image:url(:/img/background.png)}");
    arduino = new QSerialPort(this);
    
    
    setWindowTitle("ชุดทดลองควบคุมมอเตอร์ด้วยภาษาC++");
    resize(500,400);
    
    QPushButton *btn1 = new QPushButton(this);
    btn1->setText("Start");
    btn1->setFont(QFont("Time", 14));
    btn1->setGeometry(90, 470, 111, 41);
    btn1->setStyleSheet("background-color: #a5a5a5");
    connect(btn1, SIGNAL(clicked()), this, SLOT(on_Start_clicked()));
    
    QPushButton *btn2 = new QPushButton(this);
    btn2->setText("Stop");
    btn2->setFont(QFont("Time", 14));
    btn2->setGeometry(240, 470, 111, 41);
    btn2->setStyleSheet("background-color: #a5a5a5");
    connect(btn2, SIGNAL(clicked()), this, SLOT(on_Stop_clicked()));
    
    QPushButton *btn3 = new QPushButton(this);
    btn3->setText("Properties");
    btn3->setFont(QFont("Time", 14));
    btn3->setGeometry(90, 540, 111, 41);
    btn3->setStyleSheet("background-color: #a5a5a5");
    btn3->show();
    connect(btn3, SIGNAL(clicked()), this, SLOT(on_pushbutton_clicked()));
    
    QPushButton *btn4 = new QPushButton(this);
    btn4->setText("Members");
    btn4->setFont(QFont("Time", 14));
    btn4->setGeometry(240, 540, 111, 41);
    btn4->setStyleSheet("background-color: #a5a5a5");
    btn4->show();
    connect(btn4, SIGNAL(clicked()), this, SLOT(on_pushbutton1_clicked()));
    
    spinbox = new QDoubleSpinBox(this);
    spinbox->setGeometry(170, 390, 111, 41);
    spinbox->setStyleSheet("background-color: #a5a5a5");
    spinbox->setMaximum(50);
    connect(spinbox, SIGNAL(valueChanged()), this, SLOT(on_spinbox1()));
    
    
    arduino_is_available = false;
        arduino_port_name = "";
        arduino = new QSerialPort;
    
        foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
                if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
                    if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                        if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
                            arduino_port_name = serialPortInfo.portName();
                            arduino_is_available = true;
                        }
                    }
                }
            }
    
         if(arduino_is_available){
               QMessageBox::information(this, "Serial Port connected", "Found the arduino port");
               arduino = new QSerialPort(this);
               arduino->setPortName(arduino_port_name);
               arduino->open(QSerialPort::ReadWrite);
               arduino->open(QIODevice::ReadWrite);
               arduino->setBaudRate(QSerialPort::Baud115200);
               arduino->setDataBits(QSerialPort::Data8);
               arduino->setFlowControl(QSerialPort::NoFlowControl);
               arduino->setParity(QSerialPort::NoParity);
               arduino->setStopBits(QSerialPort::OneStop);
    
           }else{
               qDebug() << "Couldn't find the correct port for the arduino.\n";
               QMessageBox::information(this, "Serial Port Error", "serial port not conected to arduino.");
           }
       }
    

    MainWindow::~MainWindow()
    {
    if(arduino->isOpen()){

              arduino->close();
      
          }      delete ui;
    

    }

    void MainWindow::on_pushbutton_clicked()
    {
    form = new Form(this);
    form->show();
    this->hide();
    }

    void MainWindow::on_pushbutton1_clicked()
    {
    member = new Member(this);
    member->show();
    this->hide();
    }

    void MainWindow::on_Start_clicked()
    {
    arduino->open(QIODevice::ReadWrite);
    arduino->write("456");
    qDebug() << "Couldn't write to serial!";
    }

    void MainWindow::on_Stop_clicked()
    {
    if(arduino->isWritable()){
    arduino->write("456");
    }else{
    qDebug() << "Couldn't write to serial!";
    }
    }

    void MainWindow::spinbox1()
    {
    spinbox-> value();
    }

    void MainWindow::readSerial()
    {

    }

    JonBJ 1 Reply Last reply
    0
    • R Ryuzo

      hello everyone
      I've been trying to write data sent by Arduino it says QIODevice::write (QSerialPort): device not open please help me.

      #include "mainwindow.h"
      #include <QPushButton>
      #include "ui_mainwindow.h"
      #include "form.h"
      #include <QtSerialPort/QSerialPort>
      #include <QDebug>
      #include <QList>
      #include <QSerialPortInfo>
      #include <QMessageBox>

      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
      {

      ui->setupUi(this);
      
      this->setStyleSheet("MainWindow {background-image:url(:/img/background.png)}");
      arduino = new QSerialPort(this);
      
      
      setWindowTitle("ชุดทดลองควบคุมมอเตอร์ด้วยภาษาC++");
      resize(500,400);
      
      QPushButton *btn1 = new QPushButton(this);
      btn1->setText("Start");
      btn1->setFont(QFont("Time", 14));
      btn1->setGeometry(90, 470, 111, 41);
      btn1->setStyleSheet("background-color: #a5a5a5");
      connect(btn1, SIGNAL(clicked()), this, SLOT(on_Start_clicked()));
      
      QPushButton *btn2 = new QPushButton(this);
      btn2->setText("Stop");
      btn2->setFont(QFont("Time", 14));
      btn2->setGeometry(240, 470, 111, 41);
      btn2->setStyleSheet("background-color: #a5a5a5");
      connect(btn2, SIGNAL(clicked()), this, SLOT(on_Stop_clicked()));
      
      QPushButton *btn3 = new QPushButton(this);
      btn3->setText("Properties");
      btn3->setFont(QFont("Time", 14));
      btn3->setGeometry(90, 540, 111, 41);
      btn3->setStyleSheet("background-color: #a5a5a5");
      btn3->show();
      connect(btn3, SIGNAL(clicked()), this, SLOT(on_pushbutton_clicked()));
      
      QPushButton *btn4 = new QPushButton(this);
      btn4->setText("Members");
      btn4->setFont(QFont("Time", 14));
      btn4->setGeometry(240, 540, 111, 41);
      btn4->setStyleSheet("background-color: #a5a5a5");
      btn4->show();
      connect(btn4, SIGNAL(clicked()), this, SLOT(on_pushbutton1_clicked()));
      
      spinbox = new QDoubleSpinBox(this);
      spinbox->setGeometry(170, 390, 111, 41);
      spinbox->setStyleSheet("background-color: #a5a5a5");
      spinbox->setMaximum(50);
      connect(spinbox, SIGNAL(valueChanged()), this, SLOT(on_spinbox1()));
      
      
      arduino_is_available = false;
          arduino_port_name = "";
          arduino = new QSerialPort;
      
          foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
                  if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
                      if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                          if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
                              arduino_port_name = serialPortInfo.portName();
                              arduino_is_available = true;
                          }
                      }
                  }
              }
      
           if(arduino_is_available){
                 QMessageBox::information(this, "Serial Port connected", "Found the arduino port");
                 arduino = new QSerialPort(this);
                 arduino->setPortName(arduino_port_name);
                 arduino->open(QSerialPort::ReadWrite);
                 arduino->open(QIODevice::ReadWrite);
                 arduino->setBaudRate(QSerialPort::Baud115200);
                 arduino->setDataBits(QSerialPort::Data8);
                 arduino->setFlowControl(QSerialPort::NoFlowControl);
                 arduino->setParity(QSerialPort::NoParity);
                 arduino->setStopBits(QSerialPort::OneStop);
      
             }else{
                 qDebug() << "Couldn't find the correct port for the arduino.\n";
                 QMessageBox::information(this, "Serial Port Error", "serial port not conected to arduino.");
             }
         }
      

      MainWindow::~MainWindow()
      {
      if(arduino->isOpen()){

                arduino->close();
        
            }      delete ui;
      

      }

      void MainWindow::on_pushbutton_clicked()
      {
      form = new Form(this);
      form->show();
      this->hide();
      }

      void MainWindow::on_pushbutton1_clicked()
      {
      member = new Member(this);
      member->show();
      this->hide();
      }

      void MainWindow::on_Start_clicked()
      {
      arduino->open(QIODevice::ReadWrite);
      arduino->write("456");
      qDebug() << "Couldn't write to serial!";
      }

      void MainWindow::on_Stop_clicked()
      {
      if(arduino->isWritable()){
      arduino->write("456");
      }else{
      qDebug() << "Couldn't write to serial!";
      }
      }

      void MainWindow::spinbox1()
      {
      spinbox-> value();
      }

      void MainWindow::readSerial()
      {

      }

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Ryuzo
      I see at least 3 occurrences of

      arduino = new QSerialPort(...)
      

      I also see at least one place where this arduino serial port is opened twice in a row one immediately after the other.

      So it is hardly surprising if at least one time it reports that the serial port is not open.

      JonBJ 1 Reply Last reply
      3
      • R Offline
        R Offline
        Ryuzo
        wrote on last edited by
        #3

        @JonB said in help solve QIODevice::write (QSerialPort): device not open:

        see at least 3 occurrences of

        I fixed it but it's still not good. this code
        #include "mainwindow.h"
        #include <QPushButton>
        #include "ui_mainwindow.h"
        #include "form.h"
        #include <QtSerialPort/QSerialPort>
        #include <QDebug>
        #include <QList>
        #include <QSerialPortInfo>
        #include <QMessageBox>

        MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        {

        ui->setupUi(this);
        this->setStyleSheet("MainWindow {background-image:url(:/img/background.png)}");
        
        
        
        setWindowTitle("ชุดทดลองควบคุมมอเตอร์ด้วยภาษาC++");
        resize(500,400);
        
        QPushButton *btn1 = new QPushButton(this);
        btn1->setText("Start");
        btn1->setFont(QFont("Time", 14));
        btn1->setGeometry(90, 470, 111, 41);
        btn1->setStyleSheet("background-color: #a5a5a5");
        connect(btn1, SIGNAL(clicked()), this, SLOT(on_Start_clicked()));
        
        QPushButton *btn2 = new QPushButton(this);
        btn2->setText("Stop");
        btn2->setFont(QFont("Time", 14));
        btn2->setGeometry(240, 470, 111, 41);
        btn2->setStyleSheet("background-color: #a5a5a5");
        connect(btn2, SIGNAL(clicked()), this, SLOT(on_Stop_clicked()));
        
        QPushButton *btn3 = new QPushButton(this);
        btn3->setText("Properties");
        btn3->setFont(QFont("Time", 14));
        btn3->setGeometry(90, 540, 111, 41);
        btn3->setStyleSheet("background-color: #a5a5a5");
        btn3->show();
        connect(btn3, SIGNAL(clicked()), this, SLOT(on_pushbutton_clicked()));
        
        QPushButton *btn4 = new QPushButton(this);
        btn4->setText("Members");
        btn4->setFont(QFont("Time", 14));
        btn4->setGeometry(240, 540, 111, 41);
        btn4->setStyleSheet("background-color: #a5a5a5");
        btn4->show();
        connect(btn4, SIGNAL(clicked()), this, SLOT(on_pushbutton1_clicked()));
        
        spinbox = new QDoubleSpinBox(this);
        spinbox->setGeometry(170, 390, 111, 41);
        spinbox->setStyleSheet("background-color: #a5a5a5");
        spinbox->setMaximum(50);
        connect(spinbox, SIGNAL(valueChanged()), this, SLOT(on_spinbox1()));
        
        
        arduino_is_available = false;
            arduino_port_name = "";
        
        
            foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
                    if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
                        if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                            if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
                                arduino_port_name = serialPortInfo.portName();
                                arduino_is_available = true;
                            }
                        }
                    }
                }
        
             if(arduino_is_available){
                   QMessageBox::information(this, "Serial Port connected", "Found the arduino port");
                   arduino = new QSerialPort(this);
                   arduino->setPortName(arduino_port_name);
                   arduino->open(QSerialPort::ReadWrite);
                   arduino->open(QIODevice::ReadWrite);
                   arduino->setBaudRate(QSerialPort::Baud9600);
                   arduino->setDataBits(QSerialPort::Data8);
                   arduino->setFlowControl(QSerialPort::NoFlowControl);
                   arduino->setParity(QSerialPort::NoParity);
                   arduino->setStopBits(QSerialPort::OneStop);
        
               }else{
                   qDebug() << "Couldn't find the correct port for the arduino.\n";
                   QMessageBox::information(this, "Serial Port Error", "serial port not conected to arduino.");
               }
           }
        

        MainWindow::~MainWindow()
        {
        if(arduino->isOpen()){
        arduino->close();
        }
        delete ui;

        }

        void MainWindow::on_pushbutton_clicked()
        {
        form = new Form(this);
        form->show();
        this->hide();
        }

        void MainWindow::on_pushbutton1_clicked()
        {
        member = new Member(this);
        member->show();
        this->hide();
        }

        void MainWindow::on_Start_clicked()
        {

               arduino->write("456");
               qDebug() << "Couldn't write to serial!";
        

        }

        void MainWindow::on_Stop_clicked()
        {
        if(arduino->isWritable()){
        arduino->write("456");
        }else{
        qDebug() << "Couldn't write to serial!";
        }
        }

        void MainWindow::spinbox1()
        {
        spinbox-> value();
        }

        void MainWindow::readSerial()
        {

        }

        213.png

        @JonB said in help solve QIODevice::write (QSerialPort): device not open:

        @Ryuzo
        I see at least 3 occurrences of

        arduino = new QSerialPort(...)
        

        I also see at least one place where this arduino serial port is opened twice in a row one immediately after the other.

        So it is hardly surprising if at least one time it reports that the serial port is not open.

        1 Reply Last reply
        0
        • JonBJ JonB

          @Ryuzo
          I see at least 3 occurrences of

          arduino = new QSerialPort(...)
          

          I also see at least one place where this arduino serial port is opened twice in a row one immediately after the other.

          So it is hardly surprising if at least one time it reports that the serial port is not open.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Ryuzo
          Please put blocks of code like this in the forum's Code tags to help those trying to help you.

          @JonB said in help solve QIODevice::write (QSerialPort): device not open:

          I also see at least one place where this arduino serial port is opened twice in a row one immediately after the other.

          Unaddressed.

          You do not check the return result from the open()s, so if they fail (as I am sure they do) you will ignore this and try to write to them anyway just as the error tells you, and you do not look at error(). Error checking is fundamental part of development.

          You also show a bunch of other error messages which you have not addressed.

          1 Reply Last reply
          1
          • E Offline
            E Offline
            Emre MUTLU
            wrote on last edited by Emre MUTLU
            #5

            you need to open that port

            arduino = new QSerialPort(this);
                      arduino->setPortName(arduino_port_name);
                      arduino->setBaudRate(QSerialPort::Baud115200);
                      arduino->setDataBits(QSerialPort::Data8);
                      arduino->setFlowControl(QSerialPort::NoFlowControl);
                      arduino->setParity(QSerialPort::NoParity);
                      arduino->setStopBits(QSerialPort::OneStop);
                      arduino->open(QIODevice::ReadWrite)
            
            SGaistS 1 Reply Last reply
            0
            • E Emre MUTLU

              you need to open that port

              arduino = new QSerialPort(this);
                        arduino->setPortName(arduino_port_name);
                        arduino->setBaudRate(QSerialPort::Baud115200);
                        arduino->setDataBits(QSerialPort::Data8);
                        arduino->setFlowControl(QSerialPort::NoFlowControl);
                        arduino->setParity(QSerialPort::NoParity);
                        arduino->setStopBits(QSerialPort::OneStop);
                        arduino->open(QIODevice::ReadWrite)
              
              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              @Emre-MUTLU said in help solve QIODevice::write (QSerialPort): device not open:

              you need to open that port

              arduino = new QSerialPort(this);
                        arduino->setPortName(arduino_port_name);
                        arduino->setBaudRate(QSerialPort::Baud115200);
                        arduino->setDataBits(QSerialPort::Data8);
                        arduino->setFlowControl(QSerialPort::NoFlowControl);
                        arduino->setParity(QSerialPort::NoParity);
                        arduino->setStopBits(QSerialPort::OneStop);
                        arduino->open(QIODevice::ReadWrite)
              

              You are missing error checking and reporting.

              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
              1

              • Login

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