Trouble with QtSerialPort



  • 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



  • 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;



  • 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.



  • 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)


  • Qt Champions 2016

    @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.



  • 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);
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.