Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt-获取数据并通过串行连接转发_C++_Qt_Qtconcurrent - Fatal编程技术网

C++ Qt-获取数据并通过串行连接转发

C++ Qt-获取数据并通过串行连接转发,c++,qt,qtconcurrent,C++,Qt,Qtconcurrent,我正在尝试开发一个简单的Qt应用程序。 按下“开始”按钮后,应用程序应继续从设备检索数据(使用第三方库),并尽快通过串行连接转发数据 到目前为止,我使用的(丑陋的)软件是一个控制台应用程序,它以顺序方式运行,并在主机提供数据帧后立即获得数据帧,使用以下周期: while(1) { [...] while( MyClient.GetFrame().Result != Result::Success ) {

我正在尝试开发一个简单的Qt应用程序。 按下“开始”按钮后,应用程序应继续从设备检索数据(使用第三方库),并尽快通过串行连接转发数据

到目前为止,我使用的(丑陋的)软件是一个控制台应用程序,它以顺序方式运行,并在主机提供数据帧后立即获得数据帧,使用以下周期:

    while(1)
    {
          [...]
        while( MyClient.GetFrame().Result != Result::Success )
        {
            Sleep( 200 );
            std::cout << ".";
        }

          [... pack and send on serial]
    }
while(1)
{
[...]
while(MyClient.GetFrame().Result!=Result::Success)
{
睡眠(200);

std::cout由于现有库强制您轮询数据,唯一要做的就是在计时器上运行它。执行此任务的对象是在主线程中运行,还是在工作线程中运行,这是您的选择。不需要使用Qt Concurrent或QRunnable-使用QObject使生活变得更简单,因为您可以轻松地提供对GUI的反馈

例如,对客户的API做出一些假设:

class Worker : public QObject {
  Client m_client;
  QSerialPort m_port;
  QBasicTimer m_timer;

  void processFrame() {
    if (m_client.GetFrame().Result != Result::Success) return;
    QByteArray frame = QByteArray::fromRawData(
      m_client.GetFrame().Data, m_client.GetFrame().Size);
    ... process the frame
    if (m_port.write(frame) != frame.size()) {
      ... process the error
    }
  }
  void timerEvent(QTimerEvent * ev) {
    if (ev->timerId() == m_timer.timerId()) processFrame();
  }
public:
  Worker(QObject * parent = 0) : QObject(parent) {}
  Q_SLOT bool open(const QString & name) {
    m_port.close();
    m_port.setPortName(name);
    if (!m_port.open(name)) return false;
    if (!m_port.setBaudRate(9600)) return false;
    if (!m_port.setDataBits(QSerialPort::Data8)) return false;
    ... other settings go here
    return true;
  }
  Q_SLOT void start() { m_timer.start(200, this); }
  Q_SLOT void stop() { m_timer.stop(); }
  ...
}

/// A thread that's always safe to destruct
class Thread : public QThread {
  using QThread::run; // lock the default implementation
public:
  Thread(QObject * parent = 0) : QThread(parent) {}
  ~Thread() { quit(); wait(); }
};

int main(int argc, char ** argv) {
  QApplication app(argc, argv);
  Worker worker;
  Thread thread; // worker must be declared before thread!
  if (true) {
    // this code is optional, disabling it should not change things
    // too much unless the client implementation blocks too much
    thread.start();
    worker.moveToThread(&thread);
  }

  QPushButton button("Start");
  QObject::connect(&button, &QPushButton::clicked, [&worker]{
    // Those are cross-thread calls, they can't be done directly
    QMetaObject::invoke(&worker, "open", Q_ARG(QString, "COM1");
    QMetaObject::invoke(&worker, "start");
  });
  button.show();

  return app.exec(argc, argv);
}