Unsolved Cannot output a value using QSerialPort
-
@jude-bato said in Cannot output a value using QSerialPort:
This is the code line
If you're looking for some help in the forum please make others' life easier.
Why is so difficult to show some code snippet of yours? not just ONE line.
Come on, some context helps understand what might be going on with your issue. -
This is the code snippet, its in my dialog.cpp file
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); connect(*arduino, Dialog::readSerial(updateVoltage()),this,readSerial()); //too few arguments to function call, expected 1, have 0 } else { qDebug()<<"Could not find the correct port \n"; QMessageBox::information(this,"Serial Port Error", "Could not open the serial port"); } }
and the read serial code I have:
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); } }
-
Hi,
One thing that is missing: you don't check that the open call is successful. You should add that.
-
@jude-bato said in Cannot output a value using QSerialPort:
connect(*arduino, Dialog::readSerial(updateVoltage()),this,readSerial());
Again, please read the documentation regarding signal and slots. You may want to try this article as well.
Tip: a typical connect sentence has 4 elements, try to identify such elements within your code...
Dialog::updateVoltage(parsed_data);
Do you know why you're calling an static method here?
-
@SGaist I believe I do
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); QObject::connect(arduino, SIGNAL(readyRead), this, SLOT(readSerial())); //I know this is wrong, this is what I'm trying to fix } else { qDebug()<<"Could not find the correct port \n"; QMessageBox::information(this,"Serial Port Error", "Could not open the serial port"); } }
-
@jude-bato said in Cannot output a value using QSerialPort:
I believe I do
No, you don't check the value of QSerialPoort::open(). And you should set the parameter before opening the device.
-
@Pablo-J-Rogina I've already tried with the new syntax and I've tried with different elements and I keep getting the
expected 1, have 0
output. As for theparsed_data
I personally have never used it but I used Vannevar Morgan code as reference and it worked (https://github.com/vannevar-morgan/Qt-Temperature-Sensor/blob/master/DS18B20_Qt/dialog.cpp)@Christian-Ehrlicher Then I'm not sure how to go about doing so, I thought that mean to check if the port on the Arduino is open. When debugging I tested throughout my code and was open the Serial Port and read an open port on the Arduino, the only issue is I can't read anything from it
-
How did you test that ?
As for the call to open: again, always check that it succeeded. You might be trying to use an already opened device or you don't have the rights to open said device. Therefore, add that check before going further.
-
@SGaist I have
Dialog::~Dialog() { if(arduino->isOpen()) { arduino->close(); } delete ui; }
Would this satisfy the QSerialPort::open()?
Also would I need to use
connect
? I change that line to justreadSerial();
but I'm not able to implementvoid Dialog::updateVoltage(QString sensor_reading)
intoif(arduino_is_available)
-
@jude-bato said in Cannot output a value using QSerialPort:
Would this satisfy the QSerialPort::open()?
Of course not, it does not have anything to do with open().
if(!arduino->open(QSerialPort::ReadOnly)) { qDebug() << "Opening serial port failed"; qDebug() << arduino->error() << arduino->errorString(); }
-
@jsulm Thank you for your suggestion, I put in your code and I ran it again. This is what I got: I'm so confused why my program isn't working correctly. I have a feeling that my problem has to do with reading from or something with making a connection with Arduino rather than the Serial Port itself.
-
First: please don't post screenshots, paste the text itself. It's easier for others to copy/paste/reply to that!
Device is already open
Have you seen this message?
What do you think about that? My bet is that you also have other program (i.e. Arduino IDE) connected to your device... -
@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."