PDA

View Full Version : Help with Qprocess and sending commands to terminal



Oz
22nd December 2017, 21:07
Hello Everyone,

This is my first time posting, so bear with me as I get used to the format.

I am using Qt 5.4.2 and red hat Linux 7.2
Description:

1. I am creating a GUI that basically has some buttons to turn some relays on and off. (I'm done with the gui part)

2 . Now this device is on the network and is user/password protected which I know both.

3. I am creating a QProcess to communicate through Telnet and pass commands to turn on/off or enter credentials.

This is what I am doing (please assume I call this in the main):

void Example::process()
{
QString prog = "telnet";
QStringList arguments;
arguments << "123.456.78.90" << "23"; // ip and port
QProcess telnet;

telnet.start(program, arguments);
if(telnet.waitForStarted())
telnet.write("set 8 1");// which sets relay on

telnet.closeWriteChannel();

if(telnet.waitForFinished())
qDebug() << telnet.readAllStardardOutput();
}

//-------------------------Output--------------------------
Trying 123.456.78.90
Connected to 123.456.78.90
Escape Character is '^]'
"
//-----------------------------end of output ------------

Now if I put telnet.execute(...) instead of telnet.start(...)
//-------------------------Output--------------------------
Trying 123.456.78.90
Connected to 123.456.78.90
Escape Character is '^]'

Synacces Inc. Telnet Session V6.1
//-----------------------------end of output ------------
which is how it looks when I do that in the terminal, but program hangs waiting.

Now my question is:

How can I make it that it can receive more commands without closing that process? I don't want to close telnet because I have to tell it the ip and port again.
Mainly notice that when I say telnet.write(command) it doesn't do anything. and I have put that line outside of the if and nothing.

I would be running the terminal in the background while GUI is displaying buttons, but like I said GUI part is done.

Thanks for any help and I hope it makes sense what I'm trying to accomplish

ChrisW67
23rd December 2017, 07:15
The subprocess communication for QProcess is typically handled asynchronously by connecting to the readyRead() signal etc.

If you want a top-to-bottom synchronous telnet session then you should look at the waitForBytesWritten() and waitForReadyRead() function on the QProcess object.

Oz
23rd December 2017, 17:39
Thanks Chris,

I have read the documentation on QProcess. I've also used waitForBytesWritten() and saw it returns the numbers of bytes the device wants to write or 0 in some cases.

Now I will experiment more with readyRead () and see what I can come up with.

If any one else has extra suggestions they are welcomed too.