Results 1 to 3 of 3

Thread: Help with Qprocess and sending commands to terminal

  1. #1
    Join Date
    Dec 2017
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Help with Qprocess and sending commands to terminal

    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
    Last edited by Oz; 22nd December 2017 at 20:19.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,543
    Thanks
    13
    Thanked 1,577 Times in 1,506 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help with Qprocess and sending commands to terminal

    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Dec 2017
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Help with Qprocess and sending commands to terminal

    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.

Similar Threads

  1. Sending command to terminal
    By dcasey627 in forum Newbie
    Replies: 3
    Last Post: 4th April 2016, 08:27
  2. Sending commands throught network
    By franco.amato in forum Newbie
    Replies: 1
    Last Post: 26th March 2012, 22:33
  3. Opening A Terminal using QProcess
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2011, 14:51
  4. Replies: 12
    Last Post: 11th September 2010, 01:39
  5. serial port printer sending commands, how?
    By triperzonak in forum Qt Programming
    Replies: 3
    Last Post: 6th April 2009, 14:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.