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_Serial Port_Qt5 - Fatal编程技术网

C++ Qt两个串行端口发送和接收

C++ Qt两个串行端口发送和接收,c++,qt,serial-port,qt5,C++,Qt,Serial Port,Qt5,在我的主类main窗口中有两个类Foo的对象,它们使用两个FTDI转接板之间的串行连接来读取和写入串行数据。我已经通过终端检查了这个设置,它工作正常 在我的主类中,我试图将QByteArray从FooA发送到FooB,但在执行testConnection()时,received为空。我已经检查了与FooB端口并行连接的附加FTDI分支的串行连接,我在终端中看到数据仅由FooA在退出testConnection()时发送。如果我再次执行该函数,received列表中填充了字符串“Test”。单击按

在我的主类
main窗口中有两个类
Foo
的对象,它们使用两个FTDI转接板之间的串行连接来读取和写入串行数据。我已经通过终端检查了这个设置,它工作正常

在我的主类中,我试图将QByteArray从
FooA
发送到
FooB
,但在执行
testConnection()
时,
received
为空。我已经检查了与FooB端口并行连接的附加FTDI分支的串行连接,我在终端中看到数据仅由FooA在退出
testConnection()
时发送。如果我再次执行该函数,
received
列表中填充了字符串
“Test”
。单击按钮时将调用该插槽

我曾尝试在
m_serial->readyRead
ist启动时从
FooB
发出信号,并将其连接到
main窗口中的另一个插槽,但问题仍然存在

我是否在这里遗漏了什么,或者我如何解决这个问题而不必点击两次来接收消息

Foo类看起来像这样:

#include <QSerial>

class Foo : public QObject
{
    Q_OBJECT

public:
    Foo();
    ~Foo();

    void writeData(const QByteArray &data);
    QString readData();

private:
    QWidget *m_parent;
    QSerialPort *m_serial;
};    

void DataLink::writeData(const QByteArray &data)
{
    m_serial->clear();
    m_serial->write(data);
}

QString DataLink::readData()
{
    m_serial->waitForReadyRead(1500);
    return QString(m_serial->readAll()).simplified();
}
#包括
Foo类:公共QoObject
{
Q_对象
公众:
Foo();
~Foo();
无效写入数据(常量QByteArray和数据);
QString readData();
私人:
QWidget*m_家长;
QSerialPort*m_系列;
};    
void DataLink::writeData(常量QByteArray和数据)
{
m_串行->清除();
m_串行->写入(数据);
}
QString DataLink::readData()
{
m_serial->waitForReadyRead(1500);
返回QString(m_serial->readAll()).simplified();
}
MainWindow类的外观如下所示:

#include <QMainWindow>
#include <QDebug>
#include "foo.h"

namespace Ui
{
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void testConnection();

private:
    Ui::MainWindow *ui = nullptr;
    Foo *fooA = nullptr;
    Foo *fooB = nullptr;
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow), fooA(new Foo()), fooB(new Foo())
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::testConnection()
{
    fooA->writeData("Test");
    QString received = fooB->readData();
    QDebug() << received << endl;
}
#包括
#包括
#包括“foo.h”
名称空间用户界面
{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=nullptr);
~main窗口();
专用插槽:
void testConnection();
私人:
Ui::MainWindow*Ui=nullptr;
Foo*fooA=nullptr;
Foo*fooB=nullptr;
};
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)、ui(新ui::MainWindow)、fooA(新Foo())、fooB(新Foo())
{
用户界面->设置用户界面(此);
}
MainWindow::~MainWindow()
{
删除用户界面;
}
void MainWindow::testConnection()
{
fooA->writeData(“测试”);
QString received=fooB->readData();

QDebug()我查看了hyde的评论,并通过一个解决方法解决了这个问题:在退出
testConnection之前,我启动一个计时器,执行一个插槽来读取串行数据,这个解决方案会有一个小的延迟,并且不会太多地阻塞GUI。

如果不仔细查看代码,请尝试使用信号和插槽,不要使用waitFGUI应用程序中的orXxxxx方法(至少不是主线程)。更具体地说,因为您遇到了问题:将QSerialPort的每个信号连接到一个插槽,并将调试打印添加到所有插槽中。这将帮助您了解发生了什么,以便使其正常工作。