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. Trouble with QtSerialPort
Forum Update on Monday, May 27th 2025

Trouble with QtSerialPort

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

    Hey guys, I'm trying to send either an 'H' or 'L', depending on which button I press, through a com port, to my arduino. I'm running the physical pixel sketch on the arduino(it basically turns on an led if you send an 'H' through the serial port and turns it off when you send an 'L'). The problem is, it always says, 'QIODevice::write: device not open'. I've reason to believe that it is a problem with my code, since I tried using the terminal example program to send the 'H' and 'L', and it worked. Here's my mainwindow.cpp file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QDebug>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        SetupPort();
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::SetupPort()
    {
        serial.setPortName("COM4");// I have serial declared in the mainwindow.h file like so: QSerialPort serial;
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setFlowControl(QSerialPort::NoFlowControl);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setParity(QSerialPort::NoParity);
    
        if(!serial.open(QIODevice::ReadWrite| QIODevice::Unbuffered))
        {
            qDebug()<<"error: can't open com port";
        }
    
    
    }
    
    void MainWindow::on_pushButton_released()
    {
        char on = 'H';
        serial.write(&on);
    }
    
    void MainWindow::on_pushButton_2_released()
    {
        char off = 'L';
        serial.write(&off);
    }
    
    

    anybody know what's wrong? Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      michelson
      wrote on last edited by michelson
      #2

      HA! Dont know is this a case here but i encountered similar behaviour. Simple serial.write(&on, 1) can make a difference. In my case it seemd that qserialport was sending some trash as well, if you specify nymber of bytes to send it can fix the problem - it worked for me.

      Yup, i used something like serial->write(&bytes_to_send, number_of_bytes_to_send); serial->waitForBytesWritten(timeout) combination-style;

      1 Reply Last reply
      0
      • quentinthorntonQ Offline
        quentinthorntonQ Offline
        quentinthornton
        wrote on last edited by
        #3

        Thanks for replying. I change my two functions to look like this:

        void MainWindow::on_pushButton_released()
        {
            char on = 'H';
            serial.write(&on, 1);
            serial.waitForBytesWritten(10);
        }
        
        void MainWindow::on_pushButton_2_released()
        {
            char off = 'L';
            serial.write(&off, 1);
            serial.waitForBytesWritten(10);
        }
        

        but that didn't work either. Same error. I also messed around with the timeout in serial.waitForBytesWritten() and with the number of bytes in serial.write(&on, //righthere) but changing them didn't do anything. Any idea what I did wrong? Thanks again.

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

          Any idea what I did wrong?
          anybody know what's wrong?

          I know, but I will not say. Please, read about QSerialPort::open() method, again and again.. And then see on what do you do in your code! (Attentiveness test)

          1 Reply Last reply
          0
          • quentinthorntonQ quentinthornton

            Thanks for replying. I change my two functions to look like this:

            void MainWindow::on_pushButton_released()
            {
                char on = 'H';
                serial.write(&on, 1);
                serial.waitForBytesWritten(10);
            }
            
            void MainWindow::on_pushButton_2_released()
            {
                char off = 'L';
                serial.write(&off, 1);
                serial.waitForBytesWritten(10);
            }
            

            but that didn't work either. Same error. I also messed around with the timeout in serial.waitForBytesWritten() and with the number of bytes in serial.write(&on, //righthere) but changing them didn't do anything. Any idea what I did wrong? Thanks again.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @kuzulis
            :D

            @quentinthornton
            I can assure you, the serial port module works perfectly. I have recently deployed a project with it and had no problems whatsoever. You have a problem with open, actually 2 problems, as @kuzulis pointed out, and as he said, you should read the docs carefully. He's referring to this warning:

            Warning: The mode has to be QIODevice::ReadOnly, QIODevice::WriteOnly, or QIODevice::ReadWrite. Other modes are unsupported.

            Which is checked explicitly here:
            http://code.qt.io/cgit/qt/qtserialport.git/tree/src/serialport/qserialport.cpp?h=5.6.1#n557

            So your unbuffered mode is causing your port to not open. One more thing, when you try opening the port you don't do anything about it, so then you write directly without checking if the open operation succeeded. This is not a good thing to do, you could for example add something like this to your slot(s):

            void MainWindow::on_pushButton_released()
            {
                if (!serial.isOpen())
                    return;   //< Can't write to a port that's not open, and we didn't care to save the result of QSerialPort::open in the constructor
            
                char on = 'H';
                serial.write(&on, 1);
                serial.waitForBytesWritten(10);
            }
            

            Kind regards.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • quentinthorntonQ Offline
              quentinthorntonQ Offline
              quentinthornton
              wrote on last edited by
              #6

              Thanks for your help and time guys! It works! This is my final code:

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              #include <QDebug>
              
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  SetupPort();
              
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              void MainWindow::SetupPort()
              {
                  serial.setPortName("COM4");
                  serial.setBaudRate(QSerialPort::Baud9600);
                  serial.setDataBits(QSerialPort::Data8);
                  serial.setFlowControl(QSerialPort::NoFlowControl);
                  serial.setStopBits(QSerialPort::OneStop);
                  serial.setParity(QSerialPort::NoParity);
              
                  if(!serial.open(QIODevice::ReadWrite))
                  {
                      qDebug()<<"error: can't open com port";
                  }
              
              
              }
              
              void MainWindow::on_pushButton_released()
              {
                  if(!serial.isOpen())
                      return;
              
                  char on = 'H';
                  serial.write(&on);
              }
              
              void MainWindow::on_pushButton_2_released()
              {
                  if(!serial.isOpen())
                      return;
              
                  char off = 'L';
                  serial.write(&off);
              }
              
              
              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