I successfully read from a serial port with QSerialPort when I setup the instance's parameters in my main window constructor. I want my GUI to enable changing port however, but I can't seem to start reading when changing my com port after starting my application (I do so with a spinBox that sets up the COM port name of my QSerialPort instance). Here is my code (mainwindow.cpp), my problem is that if I do not directly use serial->setPortName(); in my constructor, and I put it in my slot linked to my spinBox signal, I can't read data received from my com port with qDebug anymore.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this); // Here by default. Takes a pointer to mainwindow as argument
serial = new QSerialPort(this);
qDebug() << "nb ports: " << QSerialPortInfo::availablePorts().length();
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
qDebug() << "name" << serialPortInfo.portName();
}
qDebug() << "is " << serial->open(QSerialPort::ReadOnly);
qDebug() << "err " << serial->error();
// Create the signal and slot for
connect(ui->com_spinBox, SIGNAL(valueChanged(const QString&)),
this, SLOT(setComPort(const QString&)));
// Create the signal and slot for receiving data from device
connect(serial, SIGNAL(readyRead()), this, SLOT(serialReceived()));
}
MainWindow::~MainWindow()
{
delete ui;
serial->close(); // instance is closed when mainwindow destroyed
}
// My 2 custom slots below!!!
void MainWindow::serialReceived()
{
QByteArray ba;
ba = serial->readAll();
ui->label->setText(serial->readAll());
qDebug()<<ba;
}
void MainWindow::setComPort(const QString& com)
{
serial->close();
serial = new QSerialPort(this); // this (mainwindow) is parent
qDebug() << serial->portName();
QString comPort = "COM" + com;
qDebug() << comPort;
serial->setPortName(comPort);
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QSerialPort::ReadOnly);
}
Here I tried moving all my serial instance setup from constructor to my slot, and close and re-open the instance to see if it helps, but I still cannot read anything with serialReceived() slot when tuning to the right COM port.. It does work if I put everything in the constructor and setPortName() with the right port number at the start of the program.
Thanks!
Try to delete and create again your QSerialPort before setPortName()
serial->deletLater();
serial = new QSerialPort(this);
serial->setPortName("COM1");
Related
I am writing a gui in QT for controlling usb relays. I wrote a relay class which has turn on/off member functions. also wrote a custom widget which has radio buttons for controlling, a relay pointer, as well as a switchStatus() slot in which the relay turn on/off functions were called. The mainwindow has relay members. When I call the the ui->widget->switchStatus() from the mainwindow constructor, everything works fine, the relay can be turn on and off well. However, if I connect the radio button signal to switchStatus(), the program crashes whenever I click the radio button. It crashes at the line serialPort->write. But it's not about write, whatever code related to serialPort pointer it first come to will cause crash. even I want to get the port name or port address.
myWidget::myWidget(QWidget *parent) : QWidget(parent), m_ui(new Ui::Form)
{ status = 0;
m_ui->setupUi(this);
m_ui->statusIndicator->status = &status; // status in ui pointing to null before this
m_ui->turnOffButton->setChecked(true);
connect(m_ui->turnOnButton, SIGNAL(clicked(bool)), this, SLOT(switchStatus()));
//crashes when click ratioButton,
//compare to last line in the mainwindow construtor
connect(m_ui->turnOffButton, SIGNAL(clicked(bool)), this, SLOT(switchStatus()));
}
void myWidget::switchStatus()
{
qDebug() << "swithcing";
if(status)
{
setStatus(false);
}
else
{
setStatus(true);
}
m_relay->switchStatus();
}
void relay::switchStatus()
{ if(status) turnOff();
else turnOn();
}
bool relay::turnOn(){
qDebug() << writeDataOn; // test if string is correct
qDebug() << serialPort; // crashes whenever serialPort address is called
const qint64 bytesWritten = serialPort->write(writeDataOn);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
qDebug() << infos[0].description();
QSerialPort serialPort;
serialPort.setPortName(infos[0].portName());
serialPort.setBaudRate(QSerialPort::Baud9600);
if (!serialPort.open(QIODevice::ReadWrite)) {
qDebug() << QObject::tr("Failed to open port %1, error: %2").arg(serialPort.portName()).arg(serialPort.error()) ;
}
ui->setupUi(this);
for(int i=0;i<4;i++)
{
relays[i].setRelayNumber(i);
relays[i].setPort(&serialPort);
relays[i].setStatus(relays[i].getRealStatus());
}
ui->widget->m_relay = relays;
qDebug() << ui->widget->m_relay;
qDebug() << ui->widget->m_relay->getPort();
ui->widget->switchStatus(); // this line runs well
}
Make your serial port a member of the MainWindow class. At the moment it is local to your constructor, so it is deallocated when the constructor finishes, hence the crashing. If you have declared a 'serialPort' member, you are hiding it by redeclaring it as a local scope variable inside the constructor.
I am writing a client server program that server is multi thread , The code compile without any error, but it doesn't show any message from client.
just it run up to the "qDebug() << " Client connected";"
Here is my code .I would be grateful if you can say where is the problem.
myclient.cpp
#include "myclient.h"
#include "QTcpsocket"
#include "QTcpServer"
#include "mainwindow.h"
#include "QHostAddress"
myclient::myclient(QObject* parent): QObject(parent)
{
}
void myclient::start(QString address, quint16 port)
{
QHostAddress LocalHost;
LocalHost.setAddress(address);
m_client.connectToHost(LocalHost, 6666);
QObject::connect(&m_client, SIGNAL(connected()),this, SLOT(startTransfer()));
}
void myclient::startTransfer()
{
m_client.write("Hello", 5);
}
mythread.cpp
#include "mythread.h"
#include "myserver.h"
mythread::mythread(QTcpSocket*, QObject *parent) :
QThread(parent)
{
}
void mythread::run()
{
qDebug() << " Thread started";
if (m_client)
{
connect(m_client, SIGNAL(connected()), this, SLOT(readyRead()), Qt::DirectConnection);
}
qDebug() << " Client connected";
exec();
}
void mythread::readyRead()
{
QByteArray Data = m_client->readAll();
qDebug()<< " Data in: " << Data;
m_client->write(Data);
}
void mythread::disconnected()
{
qDebug() << " Disconnected";
m_client->deleteLater();
exit(0);
}
myserver.cpp
#include "myserver.h"
#include "mythread.h"
myserver::myserver(QObject *parent) :
QObject(parent)
{
}
void myserver::startserver()
{
connect(&m_server,SIGNAL(newConnection()), this ,SLOT(newConnection()));
int port = 6666;
if(m_server.listen(QHostAddress::Any, port))
{
qDebug() << "Listening to port " ;
}
else
{
qDebug() << "Could not start server "<<m_server.errorString();
}
}
void myserver::newConnection()
{
m_client = m_server.nextPendingConnection();
qDebug() << " Connecting...";
mythread *thread = new mythread(m_client,this);
thread->start();
}
Documentation about nextPendingConnection() says:
Note: The returned QTcpSocket object cannot be used from another
thread. If you want to use an incoming connection from another thread,
you need to override incomingConnection().
So, you can't use that socket in another thread. As the docs say, you can subclass QTcpServer and override incomingConnection(), this method is invoked whenever a client tries to connect to your server.
incomingConnection() method provides a socket descriptor (just like regular file descriptors). And then you can pass that socket descriptor to another thread and create the QTcpSocket completely over there.
Inside that thread, you would need something like this:
QTcpSocket client = new QTcpSocket();
client.setSocketDescriptor(sockId);
// Now, you can use this socket as a connected socket.
// Make sure to connect its ready read signal to your local slot.
Once you get in newConnection the client is already connected, and you just need to start a thread that calls readAll and then replies.
There's no need to wait for a connected() signal again.
EDIT
The QSocket class is designed to work asynchronously on the main thread, based on events. From the documentation:
Warning: QSocket is not suitable for use in threads. If you need to uses sockets in threads use the lower-level QSocketDevice class.
I want to create a application, by this i can download the file from my ftp server
and show the progress in progressbar. Ive wrote some code, but if im clicking on the button to download the file from ftp server, my application is crashing. Ive become some qDebug answers like:
"no errors request",
"updateDataTransferProgress started"
the file which must be downloaded was created in a folder, but the file is empty.:( What can u do to fix my problem?
Many thanks!
#include "f1.h"
#include "ui_f1.h"
#include "ui_form2.h"
#include "form2.h"
#include <QNetworkAccessManager>
#include <QFile>
#include <QFtp>
#include <QtNetwork>
#include <QMessageBox>
f1::f1(QWidget *parent) :
QFrame(parent),
ui(new Ui::f1)
{
ui->setupUi(this);
// ui->progressBar->setValue(0);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(onDownServ()));
}
void f1::closeEvent(QCloseEvent *event)
{
F2->helloWorld();
}
f1::~f1()
{
delete ui;
}
void f1::onDownServ()
{
QNetworkAccessManager *nam = new QNetworkAccessManager();
QUrl url2("ftp://test.cz/plugins.txt");
url2.setPassword("test");
url2.setUserName("test");
reply = nam->get(QNetworkRequest(url2));
connect(reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),this, SLOT(updateDataTransferProgress(qint64,qint64)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(requestError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));
}
void f1::requestFinished()
{
qDebug() << "finished !";
save->flush();
save->close();
}
void f1::requestError(QNetworkReply::NetworkError)
{
qDebug() << "no errors, request";
}
void f1::readyRead()
{
qDebug() << "ready read!";
save=new QFile("plugins.txt");
if (!save->open(QIODevice::WriteOnly))
return;
save->write(reply->readAll());
}
void f1::updateDataTransferProgress(qint64 done, qint64 total)
{
qDebug() << "updateDataTransferProgress started";
ui->progressBar->setMaximum(100);
ui->progressBar->setValue(done*100/total);
}
QNetworkReply is a sequential-access QIODevice in which whenever more data is received from the network, the readyRead() signal is emitted. So your readyRead() slot will probably get called multiple times as new data comes gradually. So you should not initialize your file in that slot. The file initialization should be done in onDownServ() slot once:
QNetworkAccessManager *nam = new QNetworkAccessManager();
QUrl url2("ftp://test.cz/plugins.txt");
url2.setPassword("test");
url2.setUserName("test");
save=new QFile("plugins.txt");
if (!save->open(QIODevice::WriteOnly))
return;
reply = nam->get(QNetworkRequest(url2));
When you do file initialization in readyRead() slot, it opens file in the first call and subsequent calls are returned as it can not open the new file for write operation. So the readyRead() slot gets called repeatedly and the application crashes.
I have a multiserverapp that works fine so far. I got 4 cpp files.
Main.cpp constructs the program. MainWindow.cpp constructs the ui and starts (via buttonclick) MyServer.cpp. MyServer.cpp creates a thread and starts MyThread.cpp.
My aim is to show several major steps (like the "server started", "new connection", etc..) on a textBrowser.
I pass the outputs from MyServer.cpp via emit updateUI("server started"); to mainwindow.cpp where the output gets catched by:
//Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myserver.h"
#include "mythread.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::AppendToBrowser(const QString text)
{
ui->textBrowser->append(text);
}
void MainWindow::on_startButton_clicked()
{
MyServer* mServer = new MyServer;
connect(mServer, SIGNAL(updateUI(const QString)), this, SLOT(AppendToBrowser(const QString)));
mServer->StartServer();
ui->textBrowser->setPlainText("Server Started");
}
That works just right because the connect command is just in the mainwindow.cpp itself.
The problem starts one step "deeper" in the mythread.cpp.
I created another signal in the
//MyThread.h
signals:
void updateUI_serv(const QString text);
and connected it in the MyServer.cpp with the MainWindow.cpp.
//MyServer.cpp
#include "myserver.h"
#include "mainwindow.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
}
void MyServer::StartServer()
{
if(!this->listen(QHostAddress::Any,1234))
{
qDebug("Server Error");
}
else
{
qDebug("Server started");
}
}
void MyServer::incomingConnection(int socketDescriptor)
{
qDebug("new connection");
MyThread *thread = new MyThread(socketDescriptor,this);
MainWindow *mMain = new MainWindow;
connect(thread, SIGNAL(updateUI_serv(const QString)),mMain ,SLOT(AppendToBrowser(const QString)));
//flags thread for selfdeletion
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
//calls run
thread->start();
emit updateUI("thread started!");
}
// MyThread.cpp
#include "mythread.h"
#include "mainwindow.h"
#include "myserver.h"
MyThread::MyThread(int ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
emit updateUI_serv("start");
}
void MyThread::run()
{
//thread stars here
qDebug("Starting thread");
socket = new QTcpSocket();
emit updateUI_serv("hallo");
//set socketdescriptor number
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
qDebug("client connected");
exec();
}
void MyThread::readyRead()
{
QByteArray Data = socket->readAll();
QString Datain = QString::fromLatin1(Data);
qDebug("Date in:");
emit updateUI_serv("data in:");
socket->write(Data);
}
void MyThread::disconnected()
{
qDebug("Disconnected");
socket->deleteLater();
exit(0);
}
The connect command lays here "in between" the signal (updateUI_serv from mythread.cpp) and the slot (AppendToBrowser from mainwindow.cpp) file.
At this point the program crashes as soon as I try to write data (as a client via telnet) to the serverapp.
I tried to set the connect command into the mainwindow and the mythread as well, but both times I get different problems (like debugging problems, or the text does just not show up in the textBrowser).
Thanks so far.
Eventually the myServer-Object is not running in the MainThread, therefor accessing an ui element from another thread crashes your app.
You can make sure only Messages from mainThread will get displayed by adding the following code to your AppendToBrowser Slot:
if( QApplication::instance()->thread() == QThread::currentThread() )
ui->textBrowser->setPlainText("whateverTextThereShallBe");
else
return;
//You should not run into this else...
This if section checks if the current object calling the update is the mainThread. The else-section checks for erros. If you are running in the else-section you are trying to change ui-elements form a thread which is not the mainThread (UI-Thread). Connect your SIGNAL in server to another SIGNAL (SIGNAL->SIGNAL connection) and add a connect to SIGNAL(SERVER) -> SLOT(MainWindow) in your MainWindow.cpp. Eventually try your connect-call with the 5th. parameter for Queued Connection (Qt::QueuedConnection IIRC).
Ahh i got it on my own.
I solved the problem by creating a NEW function ( void forwarding(const Qstring); ) and in that function i emitted it with the ordinary emit updateUI(text); .. stuff works finaly!
I created a connection between server and client, the connection works fine in console, but i coudn't connect my QTcpServer class to GUI with signals and slots. Here is my code :
ServerTCP.cpp
ServerTcp :: ServerTcp (QWidget *parent)
{
listen(QHostAddress::Any,4000);
QObject:: connect(this, SIGNAL(newConnection()),
this, SLOT(connectionRequest()));
}
void ServerTcp :: connectionRequest()
{
emit IHM_connection();
clientConnection = nextPendingConnection();
QObject:: connect(clientConnection, SIGNAL(readyRead()),
this, SLOT(read()));
}
void ServerTcp::read()
{
QString ligne;
while(clientConnection->canReadLine())
{
ligne = clientConnection->readLine();
emit IHM_text(ligne);
}
QTextStream text(clientConnection);
}
ManinWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(&server,SIGNAL(IHM_connetion()),this,SLOT(connectionRequested()));
QObject::connect(&server,SIGNAL(read(QString)),this,SLOT(print_text(QString)));
}
void MainWindow::on_quitButton_clicked()
{
qApp->quit();
}
void MainWindow::print_text(QString text){
ui->log->append("message : " + text);
}
void MainWindow::connectionRequested(){
ui->log->append("connection OK!");
}
MainWindow::~MainWindow()
{
delete ui;
}
You have a typo in connect method: IHM_connetion
QObject::connect(&server,SIGNAL(**IHM_connetion**())
while the emitted signal is:
emit IHM_connection()
QObject:connect returns a bool value which indicates if signal-slot connection was successful. Checking this value (for example, with Q_ASSERT) is a good practice and can save you a lot of time in case of typos like this. .