Im trying to use QTserialport to connect to a port via uart
I get QIODevice::read (QSerialPort): device not open this but device is connect as i tested with other applications and with Qserialportinfo I was able to detect the serial port but not connect to it and read from it which part am i doing wrong ??
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m.setBaudRate(QSerialPort::Baud115200);
m.setDataBits(QSerialPort::Data8);
m.setPortName("COM2");
m.setFlowControl(QSerialPort::NoFlowControl);
m.setParity(QSerialPort::NoParity);
m.setStopBits(QSerialPort::OneStop);
m.setReadBufferSize(1000);
qDebug()<<m.portName();
while(1){
qDebug()<<m.readAll();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QSerialPort>
#include <QMainWindow>
#include <QSerialPortInfo>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QSerialPort m;
QSerialPortInfo info;
};
#endif // MAINWINDOW_H
ps. I tried to use //./ & \.\ & ////.// & \\.\ before the port but it didnt worked
I believe you have to open the port before reading from it. Try something like this:
if (m.open(QIODevice::ReadOnly)){
while (1){
if (m.waitForReadyRead(1000)){
qDebug() << m.readAll();
}
}
}
after adding m.open() worked perfectly
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m.setBaudRate(QSerialPort::Baud115200);
m.setDataBits(QSerialPort::Data8);
m.setPortName("COM2");
m.setFlowControl(QSerialPort::NoFlowControl);
m.setParity(QSerialPort::NoParity);
m.setStopBits(QSerialPort::OneStop);
m.setReadBufferSize(1000);
m.open(QIODevice::ReadOnly); //after adding this worked perfectly
qDebug()<<m.portName();
while(1){
qDebug()<<m.readAll();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Related
I have been login for a way to save some text to a file when I press a QPushbutton and I was wondering if anyone could lead me to somewhere to find out?
I am programing in qt 5.12 with C++.
You can try the following code:
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *button = new QPushButton(this);
button->setText("Push Here");
button->setGeometry(0, 0, 100, 30);
button->show();
//signal and slot connection
connect(button, &QPushButton::clicked, this, &MainWindow::writeSomeText);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::writeSomeText()
{
QFile n_file("file.txt");
if(n_file.open(QIODevice::WriteOnly)) //open a file
{
n_file.write("Some Text"); // write on a file
n_file.close(); //close the file
}
}
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void writeSomeText();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Please be sure to check Signal And Slot doc and QFile Class to better understanding of the code.
use a QFile and a QTextStream, open the file with readWrite flags stream the text whenever you need
void MainWindow::on_Button_clicked()
{
QString filename{"MyFile.txt"};
QFile file{filename};
if(file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
{
QTextStream stream(&file);
stream << "Hello World" << endl;
file.close();
}
}
I am connecting the projector to Quectel QCOM application. Now I want to skip that application and create my customized application to control the projector.
I wrote a program 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.
Please help me in this guys! I am stuck in this since a month.
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_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
QMake project file:
#-------------------------------------------------
Project created by QtCreator 2019-11-02T10:55:21
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = GUISerialPort
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I've got a strange problem with filtering QFileSystemModel
In "dialog.ui" there is only a QListView.
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QFileSystemModel>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QFileSystemModel* fileModel;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
fileModel = new QFileSystemModel(this);
fileModel->setFilter(QDir::Files);
ui->listView->setModel(fileModel);
ui->listView->setRootIndex(fileModel->setRootPath("/"));
////////////////////////////////// - Problem!
fileModel->setRootPath("/home/");
fileModel->setRootPath("/");
//////////////////////////////////
}
Dialog::~Dialog()
{
delete ui;
}
The problem is I see "home" folder (only this one) on the list, although filtering is set to QDir::Files. How to delete this entry?
If you want a file explorer with tree view showing folders and list view showing files, change QListView to QTreeView, Use the following constructor code, which is portable:
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
fileModel = new QFileSystemModel(this);
fileModel->setFilter(QDir::Files);
ui->treeView->setModel(fileModel);
auto myhome = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
////////////////////////////////// - Problem!
ui->treeView->setRootIndex(fileModel->setRootPath(myhome));
ui->treeView->setRootIndex(fileModel->setRootPath("/"));
//////////////////////////////////
}
hello everyone I use python send a string to qt but i do not know how show the string on a label can anyone help me ???
my mainwindow.cpp is
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
timer->start();
tcpServer.listen(QHostAddress::Any,42207);
//QByteArray Msg= tcpSocket->readAll();
readMessage();
}
void MainWindow::showTime()
{
QTime time=QTime::currentTime();
QString time_text=time.toString("hh:mm:ss");
ui->Digital_clock->setText(time_text);
QDateTime dateTime = QDateTime::currentDateTime();
QString datetimetext=dateTime.toString();
ui->date->setText(datetimetext);
}
void MainWindow::readMessage()
{
ui->receivedata_2->setText("no connection yet");
if(!tcpServer.listen(QHostAddress::Any,42207))
ui->receivedata_2->setText("waitting!");
//QByteArray Msg= tcpSocket->readAll();
}
every time i try to put socket->readall() it will get crashed when i debug
The connection between sockets is not necessarily sequential, can occur at any time so that QtcpServer handles appropriate signals, when creating a new connection we must use the signal newConnection.
In the slot that connects to the previous signal we must use nextPendingConnection that returns a pending connection through a socket.
This socket issues the readyRead signal when there is pending information, and in that task you get the data you want.
Also when disconnected this emits the signal disconnected, in that slot we must eliminate the socket.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpServer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void newConnection();
void readyRead();
void disconnected();
private:
Ui::MainWindow *ui;
QTcpServer* tcpServer;
};
#endif // MAINWINDOW_H
mainwindow.h
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTcpSocket>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tcpServer = new QTcpServer(this);
connect(tcpServer, &QTcpServer::newConnection, this, &MainWindow::newConnection);
if(!tcpServer->listen(QHostAddress::Any,42207)){
qDebug() << "Server could not start";
}
else{
qDebug() << "Server started!";
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newConnection()
{
QTcpSocket *socket = tcpServer->nextPendingConnection();
connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readyRead);
connect(socket, &QTcpSocket::disconnected, this, &MainWindow::disconnected);
}
void MainWindow::readyRead()
{
QTcpSocket* socket = qobject_cast<QTcpSocket *>(sender());
ui->receivedata_2->setText(socket->readAll());
}
void MainWindow::disconnected()
{
sender()->deleteLater();
}
I'm trying to connect a push button to a lineEdit.when click on push button,set text of lineEdit to "Hello".
but i have a problem with signal and slot!
This is my form
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),ui->lineEdit,SLOT(setText("Hello")));
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QPushButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Application output
Starting C:\Qt\Qt5.3.0\Tools\QtCreator\bin\build-E01S01-Desktop_Qt_5_3_0_MSVC2013_OpenGL_64bit-Debug\debug\E01S01.exe...
QObject::connect: No such slot QLineEdit::setText("Hello") in ..\E01S01\mainwindow.cpp:9
QObject::connect: (sender name: 'pushButton')
QObject::connect: (receiver name: 'lineEdit')
You can only connect a SIGNAL to a SLOT if they has the same signature.
You can use QSignalMapper to accomplish what you want:
QSignalMapper * mapper = new QSignalMapper(this);
QObject::connect(mapper, SIGNAL(mapped(const QString&)), ui->lineEdit, SLOT(setText(const QString&)));
QObject::connect(ui->pushButton, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(ui->pushButton, tr("Hello"));
Since you are using Qt5, you can use a lambda expression.
First make sure c++11 is enabled by adding CONFIG += c++11 in your .pro file.
#include <QApplication>
#include <QWidget>
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0) : QWidget(parent)
{
setLayout(new QVBoxLayout);
QLineEdit *lineEdit = new QLineEdit("TEXT");
QPushButton *button = new QPushButton("BUTTON");
connect(button, &QPushButton::clicked, this, [=]{lineEdit->setText("PRESSED");});
layout()->addWidget(lineEdit);
layout()->addWidget(button);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"