Regarding Projector Programming using QT
-
wrote on 12 Nov 2019, 23:52 last edited by
Yes. @Pablo-J-Rogina . I am connecting the projector to Quectel QCOM application. Now I want to skip that application and create my customised application to control the projector.
I wrote a programme based on the guidelines of Quectel to switch off the projector. Following is the code. Even I used the QSerialPort code. But the projector couldn't switch off. However using the command prompt of the Quectel QCOM application, I could control the projector.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include<QSerialPort>
#include<string>
#include<QtGui>
#include <QMessageBox>using namespace std;
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
QMessageBox::information(this,"Title here","Hello World");
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);if(!serial->open(QIODevice::ReadWrite)) { qDebug()<<"error: can't open com port"; } QString command = "WT+LEDE=0\n"; QByteArray x = command.toLocal8Bit(); serial->write(x);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = nullptr);
~MainWindow();private slots:
void on_pushButton_clicked();private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H#-------------------------------------------------
Project created by QtCreator 2019-11-02T10:55:21
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GUISerialPort
TEMPLATE = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.h
FORMS += mainwindow.ui
-
Yes. @Pablo-J-Rogina . I am connecting the projector to Quectel QCOM application. Now I want to skip that application and create my customised application to control the projector.
I wrote a programme based on the guidelines of Quectel to switch off the projector. Following is the code. Even I used the QSerialPort code. But the projector couldn't switch off. However using the command prompt of the Quectel QCOM application, I could control the projector.
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include<QSerialPort>
#include<string>
#include<QtGui>
#include <QMessageBox>using namespace std;
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
QMessageBox::information(this,"Title here","Hello World");
serial = new QSerialPort(this);
serial->setPortName("COM3");
serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);if(!serial->open(QIODevice::ReadWrite)) { qDebug()<<"error: can't open com port"; } QString command = "WT+LEDE=0\n"; QByteArray x = command.toLocal8Bit(); serial->write(x);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget *parent = nullptr);
~MainWindow();private slots:
void on_pushButton_clicked();private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H#-------------------------------------------------
Project created by QtCreator 2019-11-02T10:55:21
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GUISerialPort
TEMPLATE = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.h
FORMS += mainwindow.ui
@Prath said in Regarding Projector Programming using QT:
if(!serial->open(QIODevice::ReadWrite))
Does open() succeed?
-
@Prath said in Regarding Projector Programming using QT:
if(!serial->open(QIODevice::ReadWrite))
Does open() succeed?
-
@jsulm Yes. Open succeeded. But still, the projector could not read the commands which I am sending through serial port.
@Prath You should check what write() returns and connect a slot to https://doc.qt.io/qt-5/qserialport.html#errorOccurred to see whether you get any errors.
-
@Prath You should check what write() returns and connect a slot to https://doc.qt.io/qt-5/qserialport.html#errorOccurred to see whether you get any errors.
-
@jsulm Could you please help me with the syntax for checking the error. I am completely new in using this.
@Prath You know how to connect a slot to a signal?
https://doc.qt.io/qt-5/signalsandslots.html -
wrote on 14 Nov 2019, 08:38 last edited by Prath
Yeah @jsulm . We have to connect this signal [signal]void QSerialPort::errorOccurred(QSerialPort::SerialPortError error) to the slot right?
Is this correct?
void MainWindow::errorReport(QSerialPort::SerialPortError error)
{
if(error!=0)
qDebug()<<"ERROR:"<<endl<<error; // nothing printed
}connect(this->Serial,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(errorReport(QSerialPort::SerialPortError)));
-
Yeah @jsulm . We have to connect this signal [signal]void QSerialPort::errorOccurred(QSerialPort::SerialPortError error) to the slot right?
Is this correct?
void MainWindow::errorReport(QSerialPort::SerialPortError error)
{
if(error!=0)
qDebug()<<"ERROR:"<<endl<<error; // nothing printed
}connect(this->Serial,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(errorReport(QSerialPort::SerialPortError)));
@Prath said in Regarding Projector Programming using QT:
Is this correct?
Signal is https://doc.qt.io/qt-5/qserialport.html#errorOccurred not error. Just check the documentation.
-
wrote on 14 Nov 2019, 18:02 last edited by
I think it is correct @jsulm. We are catching the signal using the slot and then displaying the signal.
-
I think it is correct @jsulm. We are catching the signal using the slot and then displaying the signal.
wrote on 14 Nov 2019, 18:58 last edited by@Prath
You wroteconnect(this->Serial,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(errorReport(QSerialPort::SerialPortError)));
Where you have
SIGNAL(error(QSerialPort::SerialPortError))
@jsulm is expectingSIGNAL(errorOccurred(QSerialPort::SerialPortError))
. If what you have works, we do not understand how.If you are writing new code and would change over to the new signal/slot syntax you would presumably receive a compile-time error.
-
wrote on 24 Nov 2019, 22:25 last edited by
-
Your MainWindow class does not have such a signal, it's one from the QSerialPort.
-
wrote on 24 Nov 2019, 23:28 last edited by
When I click on the push button the function on_pushButton_clicked is called and then I call the Qserialport to check the error. I think the code is written correctly.
What's your take?
-
When I click on the push button the function on_pushButton_clicked is called and then I call the Qserialport to check the error. I think the code is written correctly.
What's your take?
@Prath said in Regarding Projector Programming using QT:
What's your take?
Your connect is wrong: MainWindow does NOT have signal error(QSerialPort::QSerialPort), this signal is in QSerialPort, so not in "this" but in "serial".
And please do not post screen-shots, copy paste your code...
-
@Prath said in Regarding Projector Programming using QT:
What's your take?
Your connect is wrong: MainWindow does NOT have signal error(QSerialPort::QSerialPort), this signal is in QSerialPort, so not in "this" but in "serial".
And please do not post screen-shots, copy paste your code...
wrote on 25 Nov 2019, 08:48 last edited bythis signal is in QSerialPort, so not in "this" but in "serial".
Is this a case where changing to new-style signal/slot syntax would generate an error for the wrong "context" variable? Or, would testing the return result from the
connect()
or from https://doc.qt.io/qt-5/qmetaobject-connection.html#operator-bool in code tell the user if the connection has failed because of the wrong context? -
this signal is in QSerialPort, so not in "this" but in "serial".
Is this a case where changing to new-style signal/slot syntax would generate an error for the wrong "context" variable? Or, would testing the return result from the
connect()
or from https://doc.qt.io/qt-5/qmetaobject-connection.html#operator-bool in code tell the user if the connection has failed because of the wrong context?@JonB Yes, new connect syntax would help here, unless MainWindow has a signal with exact same signature :-)
-
wrote on 10 Jan 2020, 06:17 last edited by
Guys can we send two commands to two serial ports concurrently using QT?
-
If you mean use several QSerialPorts at the same time in one application then yes, no problem with that. If you mean something else, please give more details.
-
If you mean use several QSerialPorts at the same time in one application then yes, no problem with that. If you mean something else, please give more details.
wrote on 11 Jan 2020, 08:29 last edited byThis post is deleted!