Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QtSerialPort - device not open error [solved]
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.
MainWindow::MainWindow(QWidget *parent) :
serial = new QSerialPort(this);
serial->setPortName("COM17"); //COM-port that arduino is connected to
serial->setBaudRate(QSerialPort::Baud9600); //this is the same
QString input = QString(bytearray);
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!
Hi and welcome to devnet,
Please add a check to serial->open and print the error you are getting if it fails.
Thanks for your immediate answer.
Ok, i added this check:
emit error(tr("error %1").arg(serial->error()));
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 :).
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...
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.
You have a lot of QtSerialPort examples "
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.
What do your sensors send ? Only value ? An identifier and the value ? A unit with it ?
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;
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.
Lets say that i have this data sending through serial:
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.
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.
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.
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 :)