I am new in c++ programming , so i need help with the logic (code would be awesome). I want to make QTextEdit to work as a terminal like widget.
I am trying to achieve that with QProcess like:
void QPConsole::command(QString cmd){
QProcess* process = new QProcess();
connect(process, &QProcess::readyReadStandardOutput, [=](){ print(process->readAllStandardOutput()); });
connect(process, &QProcess::readyReadStandardError, [=](){ print(process->readAllStandardError()); });
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),[=](int exitCode, QProcess::ExitStatus exitStatus){ prepareCommandLine(); return; });
process->setProgram("/bin/bash");
process->start();
process->write(cmd.toStdString().c_str());
// process->write("\n\r");
process->closeWriteChannel();
}
The problem is next:
If i don't close write channel (process->closeWriteChannel) than i am stuked in infinite loop. And if i do close it, than i cannot remember state (kinda makes new session) for example if i do "pwd" i get result, but if i do next "cd .." and then "pwd" the result is same as the first output of "pwd"
So, my question is is it possible to achieve some kind of real time output + having previous state remembered (like in real terminal) with QProcess or i have to do in some other way. In python i did it with subprocess calls with pipes, but i don't know what is the equilavent for c++.
An code example would be great. I have QTextEdit part (which is sending QString param to function), i need part with interaction with console.
The idea is simple: Keep an instance of QProcess and write commands to it followed by \n!
Here is a full example, with a QMainWindow and a Bash class, put it in main.cpp:
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
class Bash : public QObject
{
Q_OBJECT
public:
explicit Bash(QObject *parent = nullptr) : QObject(parent)
{
}
~Bash()
{
closePrivate();
}
signals:
void readyRead(QString);
public slots:
bool open(int timeout = -1)
{
if (m_bash_process)
return false;
m_timeout = timeout;
return openPrivate();
}
void close()
{
closePrivate();
}
bool write(const QString &cmd)
{
return writePrivate(cmd);
}
void SIGINT()
{
SIGINTPrivate();
}
private:
//Functions
bool openPrivate()
{
if (m_bash_process)
return false;
m_bash_process = new QProcess();
m_bash_process->setProgram("/bin/bash");
//Merge stdout and stderr in stdout channel
m_bash_process->setProcessChannelMode(QProcess::MergedChannels);
connect(m_bash_process, &QProcess::readyRead, this, &Bash::readyReadPrivate);
connect(m_bash_process, QOverload<int>::of(&QProcess::finished), this, &Bash::closePrivate);
m_bash_process->start();
bool started = m_bash_process->waitForStarted(m_timeout);
if (!started)
m_bash_process->deleteLater();
return started;
}
void closePrivate()
{
if (!m_bash_process)
return;
m_bash_process->closeWriteChannel();
m_bash_process->waitForFinished(m_timeout);
m_bash_process->terminate();
m_bash_process->deleteLater();
}
void readyReadPrivate()
{
if (!m_bash_process)
return;
while (m_bash_process->bytesAvailable() > 0)
{
QString str = QLatin1String(m_bash_process->readAll());
emit readyRead(str);
}
}
bool writePrivate(const QString &cmd)
{
if (!m_bash_process)
return false;
if (runningPrivate())
return false;
m_bash_process->write(cmd.toLatin1());
m_bash_process->write("\n");
return m_bash_process->waitForBytesWritten(m_timeout);
}
void SIGINTPrivate()
{
if (!m_bash_process)
return;
QProcess::startDetached("pkill", QStringList() << "-SIGINT" << "-P" << QString::number(m_bash_process->processId()));
}
bool runningPrivate()
{
if (!m_bash_process)
return false;
QProcess process;
process.setProgram("pgrep");
process.setArguments(QStringList() << "-P" << QString::number(m_bash_process->processId()));
process.setProcessChannelMode(QProcess::MergedChannels);
process.start();
process.waitForFinished(m_timeout);
QString pids = QLatin1String(process.readAll());
QStringList pid_list = pids.split(QRegularExpression("\n"), QString::SkipEmptyParts);
return (pid_list.size() > 0);
}
//Variables
QPointer<QProcess> m_bash_process;
int m_timeout;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
{
connect(&m_bash, &Bash::readyRead, this, &MainWindow::readyRead);
QWidget *central_widget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(central_widget);
layout->addWidget(&m_message_board);
layout->addWidget(&m_cmd_edit);
layout->addWidget(&m_sigint_btn);
m_message_board.setReadOnly(true);
m_cmd_edit.setEnabled(false);
m_sigint_btn.setText("Send SIGINT(CTRL+C)");
connect(&m_cmd_edit, &QLineEdit::returnPressed, this, &MainWindow::writeToBash);
connect(&m_sigint_btn, &QPushButton::clicked, this, &MainWindow::SIGINT);
setCentralWidget(central_widget);
resize(640, 480);
QTimer::singleShot(0, this, &MainWindow::startBash);
}
~MainWindow()
{
m_bash.close(); //Not mandatory, called by destructor
}
private slots:
void startBash()
{
//Open and give to each operation a maximum of 1 second to complete, use -1 to unlimited
if (m_bash.open(1000))
{
m_cmd_edit.setEnabled(true);
m_cmd_edit.setFocus();
}
else
{
QMessageBox::critical(this, "Error", "Failed to open bash");
}
}
void writeToBash()
{
QString cmd = m_cmd_edit.text();
m_cmd_edit.clear();
if (!m_bash.write(cmd))
{
QMessageBox::critical(this, "Error", "Failed to write to bash");
}
}
void readyRead(const QString &str)
{
m_message_board.appendPlainText(str);
}
void SIGINT()
{
m_bash.SIGINT();
}
private:
Bash m_bash;
QPlainTextEdit m_message_board;
QLineEdit m_cmd_edit;
QPushButton m_sigint_btn;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "main.moc"
Related
First things first, I'm a newbie in QT so don't blame me. I know that many similar questions have been in the forum, but I couldn't solve my problem.
Problem description. I want to have a GUI application that receives and parses data and update some qt widget. Formerly I did them all in the Mainwindow thread, but since it hangs, I tried to make it multi-threaded. But it still hangs when I try to update GUI data as fast as 10 ms.
Now, this is what I have tried.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
class Parser : public QThread , public QRunnable
{
Q_OBJECT
public:
explicit Parser(QThread *parent = nullptr);
~Parser();
signals:
void data1Available(unsigned char*);
void data2Available(unsigned char*);
void finished();
// QRunnable interface
public:
void run();
public slots:
void parse();
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect( &SerialPort, SIGNAL(readyRead()), this, SLOT(ReadData()) );
QThread* thread = new QThread;
thread->setObjectName("Parser Thread");
qInfo()<<"Parser Thread";
Parser* parser= new Parser();
parser->moveToThread(thread);
QObject::connect(thread,&QThread::started,parser,&Parser::run);
QObject::connect(parser,&Parser::finished,parser,&Parser::deleteLater);
QObject::connect(parser,&Parser::finished,thread,&QThread::quit);
QObject::connect(thread,&QThread::finished,thread,&QThread::deleteLater);
QObject::connect(parser,SIGNAL(data1Available(unsigned char *)),this,SLOT(on_data1Available(unsigned char *)));
QObject::connect(parser,SIGNAL(data2Available(unsigned char *)),this,SLOT(on_data2Available(unsigned char *)));
thread->start();
}
void MainWindow::ReadData()
{
QByteArray Data = SerialPort.readAll();
for (unsigned char i=0;i<Data.length();i++)
circBuff.append(Data[i]);
}
void MainWindow::on_data1Available(unsigned char* tempData)
{
ui->label1->setNum(tempData[5]);
}
void MainWindow::on_data2Available(unsigned char* tempData)
{
ui->label2->setNum(tempData[7]);
}
void Parser::run()
{
qInfo()<<this<<Q_FUNC_INFO<<QThread::currentThread();
QScopedPointer<QEventLoop> loop (new QEventLoop);
QScopedPointer<QTimer> timer (new QTimer);
timer->setInterval(5);
connect(timer.data(),&QTimer::timeout,this,&Parser::parse);
connect(this,&Parser::finished,loop.data(),&QEventLoop::quit);
timer->start();
loop->exec();
qInfo()<<this<<"Finished... "<<QThread::currentThread();
}
void Parser::parse()
{
unsigned char tempData[16];
while (1)
{
while (circBuff.size()>=16)
{
if ( )
{
if ()
emit data1Available(tempData);
else
emit data2Available(tempData);
}
}
}
emit finished();
}
I apologize in advance am very new to QT (and fairly new to C++)
I am trying setup a program that will execute a function anytime a specific file is changed. Despite hours on google and reading the docs provided on QT I cant find a way to get myfunction() to execute when SSOpen_Log is edited
currently my code looks something like this (SOpen_Log is a QString declared in .h):
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFileSystemWatcher watcher;
watcher.addPath(SOpen_Log);
MainWindow::connect(&watcher, SIGNAL(fileChanged(QString)), this, SLOT(myfunction()));
}
MainWindow::myfunction()
{
//mycode executes here
}
You allocated your instance on the stack and it thus get destructed when the constructor ends. I would suggest that you make a class member for the instance so that it remains valid throughout the class.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_watcher.addPath(SOpen_Log);
MainWindow::connect(&m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(myfunction()));
}
There is no need to complicate this by putting it on the heap, effectively using a pointer, although that is also an option to consider.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
watcher->addPath(SOpen_Log);
MainWindow::connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(myfunction()));
}
While we are at it, I would also like to mention to use the new signal-slot syntax style rather than the old. The old one is checked at runtime and the new one is checked at compilation time. So, you catch any issues with it during compilation rather than at runtime.
A complete and simple example:
Dirwatcher.h
class DirWatcher : public QObject
{
Q_OBJECT
public:
static const int sMaxCount = 5;
explicit DirWatcher(const QString& root, QObject* parent = nullptr);
virtual ~DirWatcher();
private slots:
void hDirChanged(const QString&);
void hFileChanged(const QString&);
private:
quint64 m_rbSize;
QByteArray m_ringBuffer[10];
QFileSystemWatcher m_watcher;
QDir m_root;
QSet<QString> m_directories;
QMap<QString, QSet<QString>> m_abspaths;
QString m_lastKey;
};
and Dirwatcher.cpp
namespace
{
void split(const char* str, const char* delim, std::vector<std::string>& out)
{
const char* begin = str;
const char* it = strstr(str, delim);
if (it != NULL)
{
std::string data{begin, it};
out.push_back(data);
it++;
split(it, delim, out);
} else {
std::string data{str};
out.push_back(data);
}
}
}
DirWatcher::DirWatcher(const QString &root, QObject* parent):
QObject(parent), m_root(root), m_rbSize{10}
{
m_watcher.addPath(root);
QObject::connect(&m_watcher, SIGNAL(directoryChanged(const QString&)),
this, SLOT(hDirChanged(const QString&)));
QObject::connect(&m_watcher, SIGNAL(fileChanged(const QString&)),
this, SLOT(hFileChanged(const QString&)));
}
DirWatcher::~DirWatcher()
{
}
void DirWatcher::hDirChanged(const QString &path)
{
QDirIterator it(m_root.path());
while (it.hasNext()) {
QString d = it.next();
if (d.at(0) == '.' || d.at(d.size()-1) == '.') {
} else {
if (!m_directories.contains(d)){
m_directories << d;
m_lastKey = d;
}
}
}
#if 0 //delete directories when count reaches...
if (m_directories.count() > DirWatcher::sMaxCount) {
QSetIterator<QString> setit(m_directories);
while (setit.hasNext()) {
QString toDel = setit.next();
if (toDel != m_lastKey) {
QDir rem (toDel);
rem.removeRecursively();
}
}
m_directories.clear();
m_directories << m_lastKey;
}
#endif
std::cout << "##############################################\r\n";
QSetIterator<QString> setit(m_directories);
while (setit.hasNext()) {
QString d = setit.next();
if (d.contains(".tlf")) {
m_watcher.addPath(d);
}
std::cout << d.toStdString() << std::endl;
}
}
void DirWatcher::hFileChanged(const QString& file)
{
static size_t cnt = 0;
QFile f{file};
if (f.open(QFile::ReadOnly)) {
quint64 s = f.size();
f.seek(f.size()-f.size()/2);
while (!f.atEnd()) {
QByteArray data = f.readLine();
m_ringBuffer[cnt++ % m_rbSize]=data;
}
}
std::cout << "----------------- B E G I N ----------------------\r\n";
for(auto i : m_ringBuffer) {
std::cout << "~~~~~~\r\n";
std::cout << i.toStdString().c_str() << "\r\n";
}
}
It's a real background monitoring job process for deleting files on a given time or count, but you will understand what is missing in your code if you debug it.
I'm trying to create my own worker thread for I/O operations. As far as I know the low-level management of the serial port is already done in a separate thread, but I want to put my code in another one to handle timeouts and other middleware stuff.
Here the complete code of my class. Please note it's my first attempt with Qt's multi-threading.
EDIT
The code is updated without sub-classing QThread
#ifndef WORKERSERIAL_H
#define WORKERSERIAL_H
#include <QObject>
#include <QThread>
#include <QQueue>
#include <QMutex>
#include <QTimer>
#include <QDebug>
#include "myserial.h"
class WorkerSerial : public QObject
{
Q_OBJECT
public:
explicit WorkerSerial(QObject *parent = 0) : QObject(parent)
{
m_serial = new MySerial(this);
connect(m_serial, &MySerial::lineReceived, this, &WorkerSerial::serial_LineReceived);
m_stop = false;
}
bool open(QString port, quint32 baudrate) { return m_serial->open(port, baudrate); }
bool isOpen() { return m_serial->serial()->isOpen(); }
QStringList ports() { return m_serial->ports(); }
private:
MySerial *m_serial;
QQueue<QString> m_queue;
QMutex m_mutex;
bool m_stop;
private slots:
void serial_LineReceived(QByteArray line)
{
emit lineReceived(line);
}
signals:
void lineReceived(QByteArray line);
public slots:
void close() { m_serial->close(); }
void send(QString data) { m_mutex.lock(); m_queue.enqueue(data); m_mutex.unlock(); }
void stop() { m_mutex.lock(); m_stop = true; m_mutex.unlock(); }
void run() {
forever {
m_mutex.lock();
if (m_stop)
{
qDebug() << "Closing...";
QTimer::singleShot(0, this, SLOT(close()));
while(m_serial->serial()->isOpen());
m_mutex.unlock();
return;
}
if (!m_queue.isEmpty())
{
QString data = m_queue.dequeue();
qDebug() << m_serial->sendMessage(data.toLocal8Bit(), true);
}
m_mutex.unlock();
}
}
};
#endif // WORKERSERIAL_H
The MySerial class it's a convenient wrapper for QSerialPort. Here the relevant functions:
bool MySerial::open(QString port, quint32 baudrate) {
m_serial->setPortName(port);
if (!m_serial->open(QIODevice::ReadWrite)) return false;
m_serial->setDataBits(QSerialPort::Data7);
m_serial->setParity(QSerialPort::EvenParity);
m_serial->setStopBits(QSerialPort::TwoStop);
m_serial->setBaudRate(baudrate);
m_serial->setFlowControl(QSerialPort::NoFlowControl);
return true;
}
QByteArray MySerial::sendMessage(QByteArray data) {
m_serial->write(data);
return data;
}
Finally, here how I start, use and close the worker:
QThread *workerThread = new QThread;
m_worker = new WorkerSerial;
m_worker->moveToThread(workerThread);
workerThread->start();
QTimer::singleShot(0, m_workerDMX, SLOT(run()));
// ...
m_worker->open("COM1", 250000));
// ...
m_worker->send("Hello World!");
// ...
m_worker->stop();
workerThread->quit();
workerThread->wait(1000);
// Here the application ends
This is the output:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QSerialPort(0x19882160), parent's thread is QThread(0x187fe598), current thread is WorkerSerial(0x19d4c9b8)
[this error appears when it calls the SendMessage() function]
"Hello World!"
Closing...
QMutex: destroying locked mutex
QThread: Destroyed while thread is still running
[the two errors above are due the singleSlot() isn't executed and thus the serial won't close]
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
It seems clear I've messed up something!
Would you help me to understand my mistakes?
Here is (an incomplete) implementation, which should give you an idea of how to simplify your code. Some additional notes are below:
class WorkerSerial : public QObject
{
Q_OBJECT
public:
explicit WorkerSerial(QObject *parent = 0) : QObject(parent)
{
// no need for slot -- connect directly to our signal
connect(&m_serial, &MySerial::lineReceived, this, &WorkerSerial::lineReceived);
}
bool open(QString port, quint32 baudrate) { return m_serial.open(port, baudrate); }
bool isOpen() { return m_serial.serial().isOpen(); }
QStringList ports() { return m_serial.ports(); }
private:
// doesn't need to be a pointer
MySerial m_serial;
signals:
void lineReceived(const QByteArray& line);
public slots:
void close() { m_serial.close(); }
void send(const QByteArray& data)
{
// "lengthy" function call
qDebug() << m_serial.sendMessage(data, true);
}
};
class Sender : public QObject
{
Q_OBJECT
public:
void send(const QByteArray& data) { emit sig_send(data); }
signals:
void sig_send(const QByteArray& data);
}
...
WorkerSerial m_worker;
m_worker.open("COM1", 250000));
QThread m_thread;
m_worker.moveToThread(&m_thread);
m_thread.start();
Sender m_sender;
QObject::connect(&m_sender, &Sender::sig_send, &m_worker, &WorkerSerial::send, Qt::QueuedConnection);
m_sender.send("Hello World!");
m_thread.quit();
m_thread.wait();
m_worker.close();
Additional notes:
I've added a Sender class that queues calls to WorkerSerial::send() on a different thread. If you have a signal in your code, which triggers calls to WorkerSerial::send() you don't need this class.
Once you move m_worker to another thread, it is unsafe to access any of its methods, unless (a) they are guaranteed to be thread-safe; or (b) you serialize (protect) access to m_serial with a mutex.
NOTE: this is not a fully working solution (yet). But I think it's better to post it as an answer than add another piece of code in the original question.
I managed to a different approach. Perhaps it looks weird... but it should have some advantages:
it guarantees the order of the messages to be transmitted
using a QQueue I can know the queue size or other information about
the "variable shadowing" mechanism should allow to safely communicate with the thread
Here the code, below the two last issues:
#ifndef WORKERSERIAL_H
#define WORKERSERIAL_H
#include <QObject>
#include <QThread>
#include <QQueue>
#include <QMutex>
#include <QDebug>
#include "myserial.h"
class WorkerSerial : public QThread
{
Q_OBJECT
public:
explicit WorkerSerial(QObject *parent = 0, int timeout = 0) : QThread(parent), timeout(timeout)
{
quit = false;
command = None;
}
~WorkerSerial()
{
mutex.lock();
quit = true;
mutex.unlock();
wait();
}
void open(QString port, quint32 baudrate)
{
mutex.lock();
this->port = port;
this->baudrate = baudrate;
command = Open;
mutex.unlock();
}
void close()
{
mutex.lock();
command = Close;
mutex.unlock();
}
void stop()
{
mutex.lock();
command = Quit;
mutex.unlock();
}
private:
enum Commands
{
None,
Open,
Close,
Quit
};
QQueue<QString> m_queue;
QMutex mutex;
int timeout;
int command;
QString port;
int baudrate;
QString request;
bool quit;
signals:
void portStateChanged(bool isOpen);
void lineReceived(QByteArray line);
public slots:
void send(QString data)
{
mutex.lock();
m_queue.enqueue(data);
mutex.unlock();
}
void run()
{
MySerial serial;
QString _port;
int _baudrate;
int _timeout;
QString _request = "";
int _command = None;
connect(&serial, &MySerial::lineReceived, this, &WorkerSerial::lineReceived);
mutex.lock();
_timeout = timeout;
mutex.unlock();
forever {
mutex.lock();
_command = command;
command = None;
if (!m_queue.isEmpty()) _request = m_queue.dequeue();
mutex.unlock();
switch (_command) {
case Open:
mutex.lock();
_port = port;
_baudrate = baudrate;
mutex.unlock();
if (serial.open(_port, _baudrate)) emit portStateChanged(true);
else portStateChanged(false);
break;
case Close:
serial.close();
emit portStateChanged(serial.serial()->isOpen());
break;
case Quit:
serial.close();
return;
break;
default:
break;
}
if (!_request.isEmpty())
{
qDebug() << serial.sendMessage(_request.toLocal8Bit(), true);
_request = "";
}
msleep(1);
}
}
};
#endif // WORKERSERIAL_H
Here how to use it:
WorkerSerial *worker = new WorkerSerial(this);
connect(worker , &WorkerSerial::lineReceived, this, &MainWindow::lineReceived);
connect(worker , &WorkerSerial::portStateChanged, this, &MainWindow::portStateChanged);
worker->start();
// ..
worker->open("COM2", 250000);
worker->send("Hello World");
// ..
worker->stop();
worker->wait(1000);
It "works" but there are two main issues:
the serialport is opened but the data are not actually transmitted. The MySerial class is working because if I do the following, bypassing the WorkerSerial:
MySerial serial;
serial.open("COM2", 250000);
serial.sendMessage("Hello World!", true);
the data is sent! Hence there is still something wrong in my WorkerSerial.
due to the forever loop, I need to insert a small delay, otherwise the CPU will run up to 100% doing nothing.
In my program I have a child process that interacts with a serial port specified to it when the parent process demands it. For example the parent process commands the child process to read a number of bytes with a certain time out from the opened port by sending this command: read 100 1000. The child process launches and opens the port successfully and I can see the message port openned successfully! but from there onwards it won't read the parent commands.
Here's the child source code:
SerialPortHandler.h
#ifndef SERIALPORTHANDLER_H
#define SERIALPORTHANDLER_H
#include <QObject>
#include <QSocketNotifier>
#include <QTextStream>
#include <QSerialPort>
#include <QFile>
#include <QTimer>
#include <QDebug>
#include <QtCore>
enum CommandType { READ, WRITE, BAD, UNKNOWN };
class SerialPortHandler : public QObject
{
Q_OBJECT
public:
explicit SerialPortHandler(QString portname, QString baudrate, QObject *parent = 0);
signals:
public slots:
void execmd();
bool open(const QString& portname, const QString& baudrate);
qint64 read(char * buff, const qint64 size, const qint32 timeout);
QString convertToCaseInsensitiveRegExp(QString str);
CommandType analyze(const QString& line);
qint64 getNum(const QString &line, int index);
void reply(char *buff);
private:
QSocketNotifier *innotif;
QSerialPort *sp;
QTimer *timer;
};
#endif // SERIALPORTHANDLER_H
SerialPortHandler.cpp
#include "SerialPortHandler.h"
#include <unistd.h>
#include <limits>
SerialPortHandler::SerialPortHandler(QString portname, QString baudrate, QObject *parent) :
QObject(parent)
{
timer = new QTimer(this);
sp = new QSerialPort(this);
if(!open(portname, baudrate)) {
qDebug() << sp->error() << sp->errorString();
exit(sp->error());
}
innotif = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read, this);
connect(innotif, SIGNAL(activated(int)), this, SLOT(execmd()));
}
void SerialPortHandler::execmd()
{
qDebug() << "command received. analyzing...";
// qint64 nbr = -1, size = -1;
// qint32 timeout = -1;
// char * buff = 0;
// QTextStream in(stdin);
// QString ln = in.readAll();
// switch (analyze(ln)) {
// case READ:
// size = getNum(ln, 1);
// timeout = getNum(ln, 2);
// if(size > -1 && timeout > -1)
// nbr = read(buff, size, timeout);
// if(nbr > -1)
// reply(buff);
// break;
// default:
// break;
// }
}
bool SerialPortHandler::open(const QString &portname, const QString &baudrate)
{
sp->setPortName(portname);
if (!sp->open(QIODevice::ReadWrite) ||
!sp->setBaudRate(baudrate.toInt()) ||
!sp->setDataBits(QSerialPort::Data8) ||
!sp->setParity(QSerialPort::NoParity) ||
!sp->setStopBits(QSerialPort::OneStop) ||
!sp->setFlowControl(QSerialPort::NoFlowControl)) {
return false;
}
sp->clear();
qDebug() << "port openned successfully!";
return true;
}
//day light wont affect this timer so the system wont freeze
qint64 SerialPortHandler::read(char *buff, const qint64 size, const qint32 timeout)
{
qint64 numbytesread = -1;
timer->start(timeout);
while (true) {
if(timer->remainingTime() > 0) {
return -1;
}
if((sp->isReadable() && sp->bytesAvailable() > 0) ||
(sp->isReadable() && sp->waitForReadyRead(10))) {
numbytesread += sp->read(buff, size);
}
if(numbytesread < 0) {
return -1;
}
if(numbytesread == size) {
break;
}
}
return numbytesread;
}
void SerialPortHandler::notify()
{
}
QString SerialPortHandler::convertToCaseInsensitiveRegExp(QString str)
{
QString result;
for(int i = 0 ; i < str.size() ; ++i) {
result.append("[");
result.append(str.at(i).toLower());
result.append(str.at(i).toUpper());
result.append("]");
}
return result;
}
CommandType SerialPortHandler::analyze(const QString &line)
{
QString read, write;
read = convertToCaseInsensitiveRegExp("read");
write = convertToCaseInsensitiveRegExp("write");
if(line.contains(QRegExp(QString("^.*%1\\s+[1-9]\\d*\\s+[1-9]\\d*.*").arg(read)))) {
return READ;
}
return UNKNOWN;
}
qint64 SerialPortHandler::getNum(const QString& line, int index) {
QStringList args(line.split(QRegExp("\\s+")));
bool done;
qint64 size = args.at(index).toInt(&done, 10);
if(done) {
return size;
}
return -1;
}
void SerialPortHandler::reply(char * buff) {
QDataStream out(stdout);
out << buff;
}
main.cpp
#include <QCoreApplication>
#include <QDebug>
#include "SerialPortHandler.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if(argc != 3) {
qDebug() << "usage:" << argv[0] << "port" << "baudrate";
} else {
SerialPortHandler *sph = new SerialPortHandler(argv[1], argv[2]);
}
return a.exec();
}
My parent process consists of the following:
ParentProcess.h
#ifndef PARENTPROCESS_H
#define PARENTPROCESS_H
#include <QObject>
#include <QtCore>
class ParentProcess : public QObject
{
Q_OBJECT
public:
explicit ParentProcess(QObject *parent = 0);
signals:
public slots:
private slots:
void sendRead();
void writeSomething();
void handleError(QProcess::ProcessError error);
private:
QProcess *p;
};
#endif // PARENTPROCESS_H
ParentProcess.cpp
#include "ParentProcess.h"
#include <QDebug>
ParentProcess::ParentProcess(QObject *parent) :
QObject(parent)
{
p = new QProcess(this);
connect(p, SIGNAL(readyReadStandardOutput()), this, SLOT(sendRead()));
connect(p, SIGNAL(readyReadStandardError()), this, SLOT(sendRead()));
connect(p, SIGNAL(started()), this, SLOT(writeSomething()));
connect(p, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleError(QProcess::ProcessError)));
QStringList args;
args << "/dev/ttyUSB0" << "115200";
p->start("/home/moki/Work/Programs/build-serialio-Desktop_Qt_5_3_0_GCC_64bit-Debug/serialio", args, QProcess::ReadWrite);
}
void ParentProcess::sendRead() {
qDebug() << "data:" << p->readAllStandardError() << p->readAllStandardOutput();
}
void ParentProcess::writeSomething() {
qDebug() << "writing";
QString cmd = "read 10 10000\n";
qint64 a = p->write(cmd.toStdString().c_str());
qDebug() << "wrote:" << a;
}
void ParentProcess::handleError(QProcess::ProcessError error)
{
switch (error) {
case QProcess::FailedToStart:
qDebug() << "failed to start";
break;
case QProcess::Crashed:
qDebug() << "crashed.";
break;
default:
break;
}
}
main.cpp
#include <QCoreApplication>
#include "ParentProcess.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ParentProcess p;
return a.exec();
}
I have seen a couple of other answers in SO but none of them address my issue. As you can see my child process is not supposed to complete and exit. It will remain launched as long as the parent process wishes. Is it correct to use QProcess-launched processes this way?
I changed my code to the following and now it's working. But I haven't understood why this code works and my original one doesn't.
in the parent process source i changed the constructor as follows:
ParentProcess::ParentProcess(QObject *parent) :
QObject(parent)
{
p = new QProcess(this);
connect(p, SIGNAL(error(QProcess::ProcessError)), this, SLOT(handleError(QProcess::ProcessError)));
connect(p, SIGNAL(readyRead()), this, SLOT(sendRead()));
connect(p, SIGNAL(started()), this, SLOT(writeSomething()));
QStringList args;
args << "/dev/ttyUSB0" << "115200";
p->setProcessChannelMode(QProcess::MergedChannels);
p->start("/home/moki/Work/Programs/build-serialio-Desktop_Qt_5_3_0_GCC_64bit-Debug/serialio", args, QProcess::ReadWrite);
}
and the sendRead() function to this:
void ParentProcess::sendRead() {
int bytes = p->bytesAvailable();
qDebug() << "data:" << p->read(bytes);
}
finally, the writeSomething() to:
void ParentProcess::writeSomething() {
qDebug() << "gonna write.";
if(p->state() == QProcess::Running) {
qDebug() << "writing";
QString cmd = "read 10 10000\n";
qint64 a = p->write(cmd.toStdString().c_str());
qDebug() << "wrote:" << a << "bytes.";
}
}
and now it works. If anyone could please explain this to me, I would be really grateful.
I'm trying to code a wrapper class using QProcess to drive the CLI applications (e.g. telnet.exe, ftp.exe) on Windows but so far with no luck. Do you know if this is even possible?
Below is the code I used to try with telnet.exe on Windows 7. I was expecting this code will print out the "welcome message" after telnet connected to the server but there is nothing print out (from standard output or error output).
#include <QCoreApplication>
#include <QProcess>
#include <iostream>
class ProcessWrapper :public QObject
{
Q_OBJECT
public:
ProcessWrapper();
~ProcessWrapper();
void start();
public slots:
void readStandardError();
void readStandardOutput();
private:
QProcess *process;
};
ProcessWrapper::ProcessWrapper()
{
process = new QProcess(this);
connect(process, SIGNAL(readyReadStandardError()), this, SLOT(readStandardError()));
connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(readStandardOutput()));
}
void ProcessWrapper::start()
{
if(process) {
process->start("telnet.exe",QStringList() << "135.251.142.36");
process->waitForStarted();
}
}
ProcessWrapper::~ProcessWrapper()
{
if(process) delete process;
}
void ProcessWrapper::readStandardOutput()
{
if(process) {
QByteArray s = process->readAllStandardOutput();
QString str(s);
std::cout << str.toStdString();
}
}
void ProcessWrapper::readStandardError()
{
if(process) {
QByteArray s = process->readAllStandardError();
QString str(s);
std::cout << str.toStdString();
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ProcessWrapper p;
p.start();
return a.exec();
}
#include "main.moc"
The function main() exit immediately after you called ProcessWrapper::start().