Regarding Projector Programming using QT
-
@Prath said in Regarding Projector Programming using QT:
what is wrong?
Not using signal & slots, as @JonB suggested already.
Have you taken a look at Serial Terminal example? -
I have used the Qserial port program from Git. The program can connect to the serial port. However, I still could not switch off the projector using the command "WT+LEDE=0".
Any help in this regards? Our entire project is stuck because of this command being not sent to the projector./****************************************************************************
**
** Copyright (C) 2012 Denis Shienkov denis.shienkov@gmail.com
** Copyright (C) 2012 Laszlo Papp lpapp@kde.org
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "console.h"
#include "settingsdialog.h"#include <QMessageBox>
#include <QtSerialPort/QSerialPort>
#include <QDebug>
#include <QLabel>
#include <QPixmap>
#include <QFile>
#include <QTextStream>//! [0]
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//! [0]
ui->setupUi(this);
console = new Console;
console->setEnabled(false);
setCentralWidget(console);
//! [1]
serial = new QSerialPort(this);
//! [1]
settings = new SettingsDialog;ui->actionConnect->setEnabled(true); ui->actionDisconnect->setEnabled(false); ui->actionQuit->setEnabled(true); ui->actionConfigure->setEnabled(true); initActionsConnections(); connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleError(QSerialPort::SerialPortError)));
//! [2]
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
//! [2]
connect(console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray)));
//! [3]
}
//! [3]MainWindow::~MainWindow()
{
delete settings;
delete ui;
}//! [4]
void MainWindow::openSerialPort()
{
SettingsDialog::Settings p = settings->settings();
serial->setPortName(p.name);
if (serial->open(QIODevice::ReadWrite)) {
if (serial->setBaudRate(p.baudRate)
&& serial->setDataBits(p.dataBits)
&& serial->setParity(p.parity)
&& serial->setStopBits(p.stopBits)
&& serial->setFlowControl(p.flowControl)) {console->setEnabled(true); console->setLocalEchoEnabled(p.localEchoEnabled); ui->actionConnect->setEnabled(false); ui->actionDisconnect->setEnabled(true); ui->actionConfigure->setEnabled(false); ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits) .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl)); } else { serial->close(); QMessageBox::critical(this, tr("Error"), serial->errorString()); ui->statusBar->showMessage(tr("Open error")); } } else { QMessageBox::critical(this, tr("Error"), serial->errorString()); ui->statusBar->showMessage(tr("Configure error")); }
}
//! [4]//! [5]
void MainWindow::closeSerialPort()
{
serial->close();
console->setEnabled(false);
ui->actionConnect->setEnabled(true);
ui->actionDisconnect->setEnabled(false);
ui->actionConfigure->setEnabled(true);
ui->statusBar->showMessage(tr("Disconnected"));
}
//! [5]void MainWindow::about()
{
QMessageBox::about(this, tr("About Simple Terminal"),
tr("The <b>Simple Terminal</b> example demonstrates how to "
"use the Qt Serial Port module in modern GUI applications "
"using Qt, with a menu bar, toolbars, and a status bar."));
}//! [6]
void MainWindow::writeData(const QByteArray &data)
{
serial->write(data);
}
//! [6]//! [7]
void MainWindow::readData()
{
QByteArray data = serial->readAll();
console->putData(data);
}
//! [7]//! [8]
void MainWindow::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
closeSerialPort();
}
}
//! [8]void MainWindow::initActionsConnections()
{
connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(openSerialPort()));
connect(ui->actionDisconnect, SIGNAL(triggered()), this, SLOT(closeSerialPort()));
connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->actionConfigure, SIGNAL(triggered()), settings, SLOT(show()));
connect(ui->actionClear, SIGNAL(triggered()), console, SLOT(clear()));
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
connect(ui->actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
} -
@Prath said in Regarding Projector Programming using QT:
connect(console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray)));
I would at least verify that whatever console
getData()
is it does return the exact right string to send on to the port. E.g. I don't know if waits till you type a newline or sends characters immediately, and/or whether it sends that newline and that matters.At this point, how have you been able to verify your command works in principle outside of anything Qt?
-
Hi,
Write what ?
Write where ?
In what window ? -
@Prath said in Regarding Projector Programming using QT:
after running the above programme
which program?
The Serial Terminal example I suggested before?Have you tested managing the projector using any other terminal application? Just to validate it works fine...
-
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 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 know how to connect a slot to a signal?
https://doc.qt.io/qt-5/signalsandslots.html -
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.
-
@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.
-
Your MainWindow class does not have such a signal, it's one from the QSerialPort.