Unable to find the slot of arduino though Qt...
-
#include "dialog.h" #include "ui_dialog.h" #include <QSerialPort> #include <QSerialPortInfo> #include <QDebug> #include <QtWidgets> #include <QString> #include <string> #include <QObject> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); //ui->degree_lcdNumber->display("---"); // ui->distance_lcdNumber->display("---"); arduino_is_available = false; port_name = ""; arduino =new QSerialPort; serialBuffer = ""; foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) { if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendorid){ if(serialPortInfo.productIdentifier()== arduino_uno_productid){ port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } if(arduino_is_available){ //open and configure the port arduino->setPortName(port_name); arduino->open(QSerialPort::ReadOnly); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->setFlowControl(QSerialPort::NoFlowControl); QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(&serialReceived())); }else{ //give error message QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!"); } } Dialog::~Dialog() { if(arduino->isOpen()){ arduino->close(); } delete ui; } void Dialog::serialReceived(){ qDebug()<<"works" ; QStringList bufferSplit = serialBuffer.split("."); serialData = arduino->readAll(); serialBuffer += QString::fromStdString(serialData.toStdString()); serialBuffer = ","; qDebug()<<bufferSplit; } void Dialog::updateLCD(const QString sensor_reading){ // ui->degree_lcdNumber->display(sensor_reading); }
//your code here(.h)
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QSerialPort> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private: Ui::Dialog *ui; QSerialPort *arduino; static const quint16 arduino_uno_vendorid =9025; static const quint16 arduino_uno_productid =67; void updateLCD(const QString); void serialReceived(); QString port_name; //void readSerial(); QByteArray serialData; QString serialBuffer; bool arduino_is_available; }; #endif // DIALOG_H
I just started the Qt. I want to connect Qt with arduino serially. I am reading the data but I am not able to connect with arduino slot.
I am getting a message after compilation. The message is QObject::connect: No such slot Dialog::&serialReceived() in ..\serial_sensor\dialog.cpp:45
QObject::connect: (receiver name: 'Dialog').Can I know why?
-
@Mohit-Tripathi said in Unable to find the slot of arduino though Qt...:
private:
Ui::Dialog *ui;
QSerialPort *arduino;
static const quint16 arduino_uno_vendorid =9025;
static const quint16 arduino_uno_productid =67;
void updateLCD(const QString);
void serialReceived();void serialReceived();
is clearly private and has not theslots
magro needed for the Qt4-Style syntax you're using for your connect statement, that won't work;use
private slots:
if you want to access it from within the class via Signal/slot
usepublic slots:
if you want to access it from outside. -
It is still not working.
-
@Mohit-Tripathi
QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived())); -
@J.Hilk I have done same thing in my main code as you can check in my code above QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));. I have changed the private slots to public slots of void serialReceived();.
Please check it.
Can you tell me what i am doing wrong here?I have corrected it to QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived()));.
-
@Mohit-Tripathi
please post your updated code and the error message you get. -
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QSerialPort> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); void serialReceived(); private: Ui::Dialog *ui; QSerialPort *arduino; static const quint16 arduino_uno_vendorid =9025; static const quint16 arduino_uno_productid =67; void updateLCD(const QString); QString port_name; QByteArray serialData; QString serialBuffer; bool arduino_is_available; public: }; #endif // DIALOG_H
#include "dialog.h" #include "ui_dialog.h" #include <QSerialPort> #include <QSerialPortInfo> #include <QDebug> #include <QtWidgets> #include <QString> #include <string> #include <QObject> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); //ui->degree_lcdNumber->display("---"); // ui->distance_lcdNumber->display("---"); arduino_is_available = false; port_name = ""; arduino =new QSerialPort; serialBuffer = ""; foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) { if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendorid){ if(serialPortInfo.productIdentifier()== arduino_uno_productid){ port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } if(arduino_is_available){ //open and configure the port arduino->setPortName(port_name); arduino->open(QSerialPort::ReadOnly); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->setFlowControl(QSerialPort::NoFlowControl); QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived())); }else{ //give error message QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!"); } } Dialog::~Dialog() { if(arduino->isOpen()){ arduino->close(); } delete ui; } void Dialog::serialReceived(){ qDebug()<<"works" ; QStringList bufferSplit = serialBuffer.split("."); serialData = arduino->readAll(); serialBuffer += QString::fromStdString(serialData.toStdString()); serialBuffer = ","; qDebug()<<bufferSplit; } void Dialog::updateLCD(const QString sensor_reading){ // ui->degree_lcdNumber->display(sensor_reading); }
I think, i have corrected it as per your instructions.
Please let me know what is wrong now. -
again, you need to SLOT-
magromacro for it to work with Qt4-Syntax//Change it from this public: explicit Dialog(QWidget *parent = 0); ~Dialog(); void serialReceived(); //to public: explicit Dialog(QWidget *parent = 0); ~Dialog(); public slots: void serialReceived();
-
@Mohit-Tripathi said in Unable to find the slot of arduino though Qt...:
i have corrected it as per your instructions.
Nope, you forgot the slot part:
Q_SLOT void serialReceived();
Or, if you are using Qt5, even better:
connect(arduino,&QSerialPort::readyRead,this,&Dialog::serialReceived);
-
void Dialog::serialReceived() { QStringList bufferSplit = serialBuffer.split(","); if(bufferSplit.length()<3){ serialData = arduino->readAll(); serialBuffer +=QString::fromStdString(serialData.toStdString()); }else { qDebug()<< bufferSplit; serialBuffer = ""; } }
Please let me know what is wrong in this code. It is unable to fetch the data from sensor and print after compiling.
-
@Mohit-Tripathi said in Unable to find the slot of arduino though Qt...:
It is unable to fetch the data from sensor
Can you describe your problem better than just "it's not working"?
You have 2 major bugs in your code both due to the fact that
serialReceived
might be called even with just a partial chunk of data:- Multi-byte chars split across 2 readyread calls will just be invalid
- The last part after the second
.
might be trucated
-
additionally to what @VRonin said, I'm pretty sure, according to your previous post, thatserialBuffer
is initialized empty and therefore the size ofbufferSplit
will be 0 andif(bufferSplit.length()<3)
will therefore never be true.
serious read error on my side. -
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.setFixedSize(540,240); w.setWindowTitle("Temperature Sensor"); w.show(); return a.exec(); }
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QSerialPort> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); void serialReceived(); private: Ui::Dialog *ui; QSerialPort *arduino; static const quint16 arduino_uno_vendorid =9025; static const quint16 arduino_uno_productid =67; void updateLCD(const QString); QString port_name; QByteArray serialData; QString serialBuffer; bool arduino_is_available; public: }; #endif // DIALOG_H
#include "dialog.h" #include "ui_dialog.h" #include <QSerialPort> #include <QSerialPortInfo> #include <QDebug> #include <QtWidgets> #include <QString> #include <string> #include <QObject> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); ui->temp_lcdNumber->display("-------"); arduino_is_available = false; port_name = ""; arduino =new QSerialPort; serialBuffer = ""; foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) { if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendorid){ if(serialPortInfo.productIdentifier()== arduino_uno_productid){ port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } if(arduino_is_available){ //open and configure the port arduino->setPortName(port_name); arduino->open(QSerialPort::ReadOnly); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); arduino->setFlowControl(QSerialPort::NoFlowControl); connect(arduino,&QSerialPort::readyRead,this,&Dialog::serialReceived); }else{ //give error message QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!"); } } Dialog::~Dialog() { if(arduino->isOpen()){ arduino->close(); } delete ui; } void Dialog::readSerial() { qDebug()<< "works"; QStringList bufferSplit = serialBuffer.split(","); if(bufferSplit.length()<3){ serialData = arduino->readAll(); serialBuffer +=QString::fromStdString(serialData.toStdString()); }else { qDebug()<< bufferSplit; Dialog::updateLCD(bufferSplit[1]); serialBuffer = ""; } } void Dialog::updateLCD(const QString sensor_reading) { ui->temp_lcdNumber->display(sensor_reading); }
This is my whole code. I have defined the LCD as temp_lcdNumber in application. I am not able to see any change in value in application or QT. The sensor is temperature sensor(LM35).
Please let me know what is wrong in this code. Why I am not able to see any change in value? -
@Mohit-Tripathi putting aside that your "whole code" (main.cpp, dialog.h and dialog.cpp) as is is not compiling because of
no ‘void Dialog::readSerial()’ member function declared in class ‘Dialog’ void Dialog::readSerial() ^
which should be serialReceived() instead, what is the output of both qDebug statements in serialReceived()? Are you seeing data actually received at that point?
I'd add another qDebug statement in updateLCD() just before ui->temp_lcdNumber->display(sensor_reading); just to be sure what is the value that the LCD should be displaying
In addition, are you sure the Arduino board is actually sending any data? Have you tried running another (non Qt) app to check what the device is really sending? For instance in Linux you can try (use the device Arduino is connected to):tail -f /dev/ttyUSB0