无法在QProcess中执行echo命令

时间:2013-10-29 07:45:24

标签: qt echo qprocess

我想在Ubuntu 10.04 LTS和Qt下使用netcat实用程序在我的设备中启动SCPI命令。我的代码如下:

env = "echo TRIG | nc 192.168.1.100 23 -q1";
process1.execute(env);
process1.waitForFinished(1000);

此命令不会返回任何数据,只会触发数据采集。 如果使用具有相同“echo TRIG | nc 192.168.1.100 23 -q1”命令的终端,一切正常。 从Qt,它不起作用。调试输出为“TRIG | nc 10.0.3.250 23 -q1”...所以没有“echo”。我的设备没有收到TRIG命令。

你可以告诉我我做错了吗? 非常感谢。

2 个答案:

答案 0 :(得分:2)

下面的代码显示了此功能的相当完整的异步实现。它演示了如何在不启动外部进程的情况下完成它,以及如何在Qt 5中利用C ++ 11 lambdas。对于Qt 4,来自main()的插槽需要存在于他们自己的QObject中 - 衍生类。

screenshot

#include <QtWidgets>
#include <QtNetwork>

class SocketSignaler : public QObject
{
   Q_OBJECT
   Q_SLOT void stateChanged(QAbstractSocket::SocketState state) {
      if (state == QAbstractSocket::UnconnectedState) emit unconnected();
      else emit busy();
      emit hasState(this->state());
   }
public:
   explicit SocketSignaler(QAbstractSocket * socket) : QObject(socket) {
      connect(socket, &QAbstractSocket::stateChanged, this, &SocketSignaler::stateChanged);
      connect(&(const QObject&)QObject(), &QObject::destroyed, this, // defer signal emission
              [=]{ emit stateChanged(socket->state()); }, Qt::QueuedConnection);
   }
   Q_SIGNAL void busy();
   Q_SIGNAL void unconnected();
   Q_SIGNAL void hasState(const QString &);
   QString state() const {
      switch (static_cast<QAbstractSocket*>(parent())->state()) {
      case QAbstractSocket::UnconnectedState: return "Disconnected";
      case QAbstractSocket::HostLookupState: return "Looking up host";
      case QAbstractSocket::ConnectingState: return "Connecting";
      case QAbstractSocket::ConnectedState: return "Connected";
      case QAbstractSocket::ClosingState: return "Closing";
      default: return {};
      }
   }
};

class Ui : public QWidget {
   Q_OBJECT
   Q_PROPERTY(bool busy WRITE setBusy)
   QVBoxLayout m_layout{this};
   QFormLayout m_form;
   QLineEdit m_target{"192.168.1.100"};
   QLineEdit m_message{"TRIG"};
   QLabel m_state;
   QDialogButtonBox m_box;
   QPushButton * const m_send = m_box.addButton("Send", QDialogButtonBox::AcceptRole);
   QPushButton * const m_cancel = m_box.addButton(QDialogButtonBox::Cancel);
   QMessageBox m_msgBox{this};
public:
   Ui() {
      m_form.addRow("Target Host", &m_target);
      m_form.addRow("Command", &m_message);
      m_layout.addLayout(&m_form);
      m_layout.addWidget(&m_state);
      m_layout.addWidget(&m_box);
      m_msgBox.setIcon(QMessageBox::Critical);
      connect(m_send, &QPushButton::clicked, this, &Ui::send);
      connect(m_cancel, &QPushButton::clicked, this, &Ui::cancel);
   }
   void setState(const QString & text) { m_state.setText(text); }
   QString target() const { return m_target.text(); }
   QString message() const { return m_message.text(); }
   void showError(const QString & text) {
      m_msgBox.setText(text);
      m_msgBox.show();
   }
   void setBusy(bool busy) {
      m_send->setEnabled(!busy);
      m_cancel->setEnabled(busy);
   }
   Q_SIGNAL void send();
   Q_SIGNAL void cancel();
};

int main(int argc, char *argv[])
{
   const int targetPort = 23;
   QApplication app{argc, argv};
   Ui ui;
   ui.show();

   QTcpSocket socket;
   SocketSignaler socketSig{&socket};
   QObject::connect(&socketSig, &SocketSignaler::hasState, &ui, &Ui::setState);

   QStateMachine machine;
   QState sReady{&machine};
   QState sBusy{&machine};
   sReady.assignProperty(&ui, "busy", false);
   sBusy.assignProperty(&ui, "busy", true);
   sReady.addTransition(&socketSig, &SocketSignaler::busy, &sBusy);
   sBusy.addTransition(&socketSig, &SocketSignaler::unconnected, &sReady);

   QObject::connect(&ui, &Ui::send, [&](){
      socket.connectToHost(ui.target(), targetPort);
   });
   QObject::connect(&ui, &Ui::cancel, [&](){ socket.abort(); });
   QObject::connect(&socket,
                    static_cast<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>
                    (&QAbstractSocket::error), [&]()
   {
      ui.showError(socket.errorString());
   });
   QObject::connect(&socket, &QAbstractSocket::connected, [&](){
      auto msg = ui.message().toLatin1();
      msg.append('\n');
      if (socket.write(msg) >= msg.size()) socket.close();
   });
   QObject::connect(&socket, &QAbstractSocket::bytesWritten, [&](){
      if (!socket.bytesToWrite()) socket.close();
   });

   machine.setInitialState(&sReady);
   machine.start();
   return app.exec();
}
#include "main.moc"

答案 1 :(得分:1)

您不能使用QProcess管道命令(|)。

有几种方法可以解决这个问题: -

您可以在Qt中或在另一次调用QProcess之前调用第一个命令并检索其输出。

或者,创建一个从QProcess调用并检索输出的脚本。

最后,假设您正在使用linux / OSX,您可以使用/ bin / bash调用QProcess并将命令传递给它。例如: -

env = "/bin/bash \"echo TRIG | nc 192.168.1.100 23 -q1\"";
process1.execute(env);

你可以找到一个等同于/ bin / bash for windows,也许是cmd.exe