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. QtSerialPort - device not open error [solved]
QtWS25 Last Chance

QtSerialPort - device not open error [solved]

Scheduled Pinned Locked Moved General and Desktop
14 Posts 2 Posters 17.9k 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.
  • U Offline
    U Offline
    uzz3
    wrote on last edited by
    #1

    Hello all,
    I've been trying for more then a week to read data sent by arduino through a COM port but i keep failing. I did some searching on google, read the blockingmaster example... and still i didn't succeeded. Ok so the basic thing is that i need to read continuously the COM port. I tried to keep the code as simple as possible.
    The functionality should be like when arduino will write something on serial, the label will display that data.

    @
    QSerialPort *serial;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    serial = new QSerialPort(this);
    serial->setPortName("COM17"); //COM-port that arduino is connected to
    serial->open(QIODevice::ReadOnly);
    serial->setBaudRate(QSerialPort::Baud9600); //this is the same
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }

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

    void MainWindow::serialReceived()
    {

    QByteArray bytearray;
    bytearray=serial->readAll();
    QString input = QString(bytearray);

    ui->label->setText(input);
    }
    @

    After running this, i receive:
    @
    bool QSerialPort::setBaudRate(qint32, QSerialPort::Directions): device not open
    bool QSerialPort::setDataBits(QSerialPort::DataBits): device not open
    bool QSerialPort::setParity(QSerialPort::Parity): device not open
    bool QSerialPort::setStopBits(QSerialPort::StopBits): device not open
    bool QSerialPort::setFlowControl(QSerialPort::FlowControl): device not open
    @

    And I use Qt 5.2.1/ Qt Creator 3.0;
    Any help will be appreciated. Thank you!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Please add a check to serial->open and print the error you are getting if it fails.

      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
      0
      • U Offline
        U Offline
        uzz3
        wrote on last edited by
        #3

        Thanks for your immediate answer.
        Ok, i added this check:
        @
        if(!serial->open(QIODevice::ReadOnly)){
        emit error(tr("error %1").arg(serial->error()));
        return;}
        @
        and it worked.. strange somehow, i mean i don't understand why it worked.

        Still data now is not consistent, but i expected that because the reading is asynchronously.
        Anyway for the beginnig i think i will implement the ReadLine() method to read untill a /n. It should work i hope.

        Thanks again.. and if you could explain me why it worked, it would be great :).

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can use a buffer and append the data using readAll(). Then parse the result, if it contains all that is needed, then update your label. That's the normal way of processing data from the serial port.

          As for why it suddenly work just adding the if… Good question...

          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
          0
          • U Offline
            U Offline
            uzz3
            wrote on last edited by
            #5

            Hello again, ok i tried your sugestion but i'm not able to implement it. If you wish, can you show me an example or just tell me where to look, because google is not helping me.
            Sorry for being a beginner, but i'm doing my best to learn as much as i can in my little free time, and asking for help instead of trying to discover everything by myself is more efficient.
            Thanks a lot for your help.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You have a lot of QtSerialPort examples "
              here":http://qt-project.org/doc/qt-5/examples.html

              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
              0
              • U Offline
                U Offline
                uzz3
                wrote on last edited by
                #7

                Hi, i managed to get correct readings from serial port. Now i'm not sure how to go further with my project. What i need to do is read data from different senzors and display the data that i receive from serial port in different labels.

                For example i have a temperature senzor which sends data through serial from 10 to 10 seconds. And a humidity senzor which sends data from 20 to 20 seconds.

                And the question is how can i display the data that i receive from temperature senzor in a label, and the data that i receive from humidity senzor in another label.

                Thanks for your help and support.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  What do your sensors send ? Only value ? An identifier and the value ? A unit with it ?

                  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
                  0
                  • U Offline
                    U Offline
                    uzz3
                    wrote on last edited by
                    #9

                    Well the sensors are sending only the value. For example the temperature sensor will send an int value like: 24;
                    And the humidity will send the same like: 60;

                    But if it helps i can insert a string among with that value like: "Temperature in Celsius: 24";"Humidity sensor: 60 %"; (right now this is how i'm sending data through serial)

                    Also if it helps i can assign an int id to a sensor so the output will be like:
                    1 24; //where 1 is id of temp sens;
                    2 60; // 2 - id of humidity sens;

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      So to my understanding, you control what the sensors send ? Then it's up to you to define the protocol. You should have something like a starting value that you know will be the beginning of a message then the content of it and an end value that tells you the message is over. This way you will be able to parse your communication protocol more easily even if your sensors send their value at different rate.

                      If you can have several sensors with similar outputs your should consider having an id, a type identifier and a unit.

                      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
                      0
                      • U Offline
                        U Offline
                        uzz3
                        wrote on last edited by
                        #11

                        Lets say that i have this data sending through serial:

                        "Temperature: 24"
                        "Humidity: 60"
                        So i can make something like... after doing serial-readAll(), if in the string that stores the serial data, i have "Temperature: " then ui->labelA-setText();
                        else if i have "Humidity:" then ui->labelB->setText();

                        Hope this makes sense;

                        Thanks a lot for your support. You are really helping me.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Beware that you might not get all the text at once when readyRead is called. So you should have a buffer where you put the result of readAll() then parse the buffer to see if you have a complete sensor data then remove it from the buffer and send it to your label.

                          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
                          0
                          • U Offline
                            U Offline
                            uzz3
                            wrote on last edited by
                            #13

                            Hello,
                            I want to thank you for helping me and for guiding me in my project. I managed to finish what i had to do, and it works great.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              You're welcome !

                              Good to know :)

                              Since all is working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                              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
                              0

                              • Login

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