Unsolved Cannot output a value using QSerialPort
-
@jude-bato Exactly what @SGaist was suggesting
-
@Pablo-J-Rogina I have an Arduino Uno connected via USB, that's my original point in making the forum post is because I am unable to capture a voltage in Qt from it. I have it stated in my original post.
-
@jude-bato said in Cannot output a value using QSerialPort:
I have an Arduino Uno connected via USB,
Yes, I assumed that. What you should know, if not aware yet, is that you cannot have two applications (i.e. your Qt app and the Arduino IDE) using the same port simultaneously
-
@Pablo-J-Rogina So how can I just read voltage from my Arduino Uno in Qt
-
@jude-bato said in Cannot output a value using QSerialPort:
So how can I just read voltage from my Arduino Uno in Qt
Since you haven't describe your environment/use case so far, I'll assume that you have a program running on the Arduino board, and that program reads some sensor and it outputs such readings via serial.
So with that assumption, I expect you to go through something like this:
- Write the Arduino code, and via Arduino IDE deploy it on your Arduino device
- Close the Arduino IDE
- Start Qt Creator and write a Qt application that will read the serial port and display the readings on screen
- Run your Qt application
- Success!
-
@Pablo-J-Rogina Thats exactly what I'm trying to do. I read upon this article: https://forum.qt.io/topic/64696/sending-a-data-to-arduino-through-serial-port-using-qt and it is a similar problem to the one I'm having, for some reason Qt is not reading from the Arduino at all. It recognizes it but it's not reading from it. Here is my Arduino IDE code:
int offset = 20; void setup() { Serial.begin(9600); } void loop() { int volt = analogRead(A0); double voltage = map(volt, 0, 1023, 0, 2500) + offset; voltage /= 100; Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V"); delay(500); }
I've tried doing you recommendation but I'm having difficulties with Qt and grabbing information off Arduino.
-
I am testing the hardware and I see that when I use the Arduino IDE or the Qt code they both work the same way and allow for a reading to happen I believe it is something to do with the GUI that I have designed that would allow for the values to be shown I will try and change it up and see if anything happens. I've been reading many many forum posts and it seems that my code is fine because it compiles and it works the same way when running the board through the IDE.
-
@jude-bato I don't know why I didn't yet suggested to check with some Qt examples, anyway. What if you try building and running:
Command Line Reader Async Example (no GUI, just to test you are able to receive data from Arduino...)
Terminal "Terminal shows how to create a terminal for a simple serial interface by using Qt Serial Port." -
@Pablo-J-Rogina I ran the example and the Application Output I got was:
16:00:11: Running steps for project creaderasync... 16:00:11: Configuration unchanged, skipping qmake step. 16:00:11: Starting: "C:\Qt\Tools\mingw730_32\bin\mingw32-make.exe" -j8 C:/Qt/Tools/mingw730_32/bin/mingw32-make -f Makefile.Debug mingw32-make[1]: Entering directory 'C:/Qt/Examples/Qt-5.12.6/serialport/build-creaderasync-Desktop_Qt_5_12_6_MinGW_32_bit-Debug' mingw32-make[1]: Nothing to be done for 'first'. mingw32-make[1]: Leaving directory 'C:/Qt/Examples/Qt-5.12.6/serialport/build-creaderasync-Desktop_Qt_5_12_6_MinGW_32_bit-Debug' 16:00:12: The process "C:\Qt\Tools\mingw730_32\bin\mingw32-make.exe" exited normally. 16:00:12: Elapsed time: 00:01.
I'm not sure of the result this is a little foreign to me.Looking back on my code I figured something out. When I have
if(arduino_is_available) { qDebug()<<"Found the port \n"; arduino->setPortName(arduino_uno_port_name); arduino->open(QSerialPort::ReadOnly); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial); } else { qDebug()<<"Could not find the correct port \n"; QMessageBox::information(this,"Serial Port Error", "Could not open the serial port"); }
I also have:
void Dialog::updateVoltage(QString sensor_reading) { ui->voltagelcdNumber->display(sensor_reading); }
which is never called. And when I tried to call it in the previous code I get the error:
too few arguments to function call, expected 1, have 0
If I am able to implement the update voltage into my previous code I believe it will work. My issue now is how am I supposed to get around this error. When I hover over it, it says it requires a QString, I have tried everything to try and get around this error.
-
@jude-bato said in Cannot output a value using QSerialPort:
I'm not sure of the result this is a little foreign to me.
Sorry but it's evident that you don't read the documentation...
When running the command line example, please check the arguments it needs... -
Hi
Why not try the same
https://doc.qt.io/qt-5/qtserialport-terminal-example.html
as @Pablo-J-Rogina linked to ?
Its a littel serial comm GUI that can easy open a serial port and
read and write to it.
Very good for testing out if the Arduino responds as expected. -
@mrjj @Pablo-J-Rogina I ran both examples and since this is my first Qt project I'm not exactly sure what I'm looking for. I have attached my results.
I know that my Arduino works already because I have tested it in the Arduino IDE as well as when I am debugging my code. I already know that everything works, my issue is just being able to display it. That second example is similar to what I want to do expect having my own GUI. Many thanks for all the responses thus far! -
Hi
Ok super. so it works.
You are not showing complete code so hard to guess.
QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial);
This looks fineSo in
Dialog::readSerialyou call updateVoltage( yourbuffer )
Or when is that called ?
Im not sure where "too few arguments to function call, expected 1, have 0" comes
but it sounds like you called updateVoltage without its qstring. -
@jude-bato said in Cannot output a value using QSerialPort:
I'm not exactly sure what I'm looking for
If guessing, to write a Qt app that can connect to your Arduino device and display information sent from it via the serial port.
So your next steps would be to use the Simple Terminal example as a reference to check what you need to do in order to open the port, check for errors, how to connect signal and slots, etc.
-
@mrjj This is the full code:
#include "dialog.h" #include "ui_dialog.h" #include <iostream> #include <QSerialPort> #include <QSerialPortInfo> #include <string> #include <QDebug> #include <QMessageBox> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); ui->voltagelcdNumber->display("0.00"); arduino = new QSerialPort(this); serialBuffer = ""; parsed_data = ""; voltage_value = 0.0; bool arduino_is_available = false; QString arduino_uno_port_name; foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){ if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier()){ if((serialPortInfo.productIdentifier() == arduino_uno_product_id) && (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id)){ arduino_is_available = true; arduino_uno_port_name = serialPortInfo.portName(); } } } if(arduino_is_available) { qDebug()<<"Found the port \n"; arduino->setPortName(arduino_uno_port_name); arduino->open(QSerialPort::ReadOnly); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); QSerialPort::connect(arduino, &QSerialPort::readyRead, this, &Dialog::readSerial); //ui->voltagelcdNumber->display(sensor_reading); Here is where I'm haveing trouble } else { qDebug()<<"Could not find the correct port \n"; QMessageBox::information(this,"Serial Port Error", "Could not open the serial port"); } } Dialog::~Dialog() { if(arduino->isOpen()) { arduino->close(); } delete ui; } void Dialog::readSerial() { QStringList buffer_split = serialBuffer.split(","); if(buffer_split.length() < 3) { serialData = arduino->readAll(); serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString()); serialData.clear(); } else { serialBuffer = ""; qDebug() << buffer_split << "\n"; parsed_data = buffer_split[1]; voltage_value = (parsed_data.toDouble()) - 0.1; qDebug() << "Voltage: " << voltage_value << "\n"; parsed_data = QString::number(voltage_value,'g',4); Dialog::updateVoltage(parsed_data); } } void Dialog::updateVoltage(QString sensor_reading) { ui->voltagelcdNumber->display(sensor_reading); }
sensor_reading is private and initialized in QString
@Pablo-J-Rogina I have tested it in the simple terminal and it works, as well as working in the Arduino IDE
-
Hi
Some notes
You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.Actually you can :)
must be before open!
All of them. Open goes last.You do not check if
arduino->open(QSerialPort::ReadOnly);
fails.
like
if ( ! arduino->open(QSerialPort::ReadOnly) ) {
QMessageBox::information(this,...report error);
return;
}
see @jsulm post !So does it ever enter into void Dialog::readSerial() ?
-
@mrjj said in Cannot output a value using QSerialPort:
You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.
I already said this some days ago: https://forum.qt.io/topic/112346/cannot-output-a-value-using-qserialport/23 but it is simply ignored...
-
@Christian-Ehrlicher
Indeed and also @jsulm also noted to check open which seems to be forgotten also. -
@mrjj said in Cannot output a value using QSerialPort:
You call setBaudRate, setDataBits etc AFTER you open port. that is wrong.
must be before open!This is not true. You can configure QSP in any time.
-
@kuzulis
Ok
thank you for clarifying that.
Over the years I have not had huge success with that without close and reopen the port but its
good to know QSerialPort does support it :)