c++ - QSerialPort readyRead() 信号不能正常工作

标签 c++ qt arduino

因此,我试图让我的 Arduino 与我的 Qt 项目进行通信,但信号 readyRead() 从未由 QSerialPort 发出。我试错了许多方法和命令来实现它,尝试了以下视频中的代码,但它也不起作用,尝试了另一个微 Controller 和另一个端口。

https://www.youtube.com/watch?v=AX-HhBXBzGg

我用这段代码建立了串行连接,所以我的 arduino 的写 LED 灯闪烁,它的端口很忙。当我定期使用 waitForReadyRead() 函数时,我只能执行 readSerial() ,这是愚蠢的。没有该函数 readRead() 不会被发射。

另一个观察结果:当我尝试向我的 arduino 写入内容时,它的读取 LED 正在闪烁,但它没有正确读取信号。使用 Arduino IDE 串行监视器,它工作正常。

这是我的Qt代码和arduino代码:

序列号.h:

#ifndef SERIAL_H
#define SERIAL_H
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>

class Serial : public QSerialPort
{
    Q_OBJECT

public:
    Serial();
    ~Serial();

public slots:
    void readSerial();
};

#endif // SERIAL_H

序列号.cpp:

#include "serial.h"

Serial::Serial(){
    QObject::connect(this, SIGNAL(readyRead()), this, SLOT(readSerial()));
    setBaudRate(QSerialPort::Baud9600);
    setDataBits(QSerialPort::Data8);
    setParity(QSerialPort::NoParity);
    setStopBits(QSerialPort::OneStop);
    setFlowControl(QSerialPort::NoFlowControl);
    setReadBufferSize(10);

    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        if (info.description() == "Arduino Leonardo"){
            setPort(info);
            open(QSerialPort::ReadWrite);
            if (isOpen()) qDebug() << portName();
        }
    }
}

Serial::~Serial(){
    if (isOpen()){
        close();
    }
}

void Serial::readSerial(){
    qDebug() << "Never triggered!";
    while (!atEnd()){
            QByteArray data = readAll();
            clear();
            qDebug() << data;
    }
}

arduino(有点琐碎):


void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.print(0);
  Serial.flush();
}

非常感谢!

最佳答案

我在使用 5.13.1 和 mingw 时遇到完全相同的问题(readyRead() 仅在调用 waitForReadyRead() 时触发)。与 5.13.0 相同的代码有效。我的解决方案是周期性计时器事件(~50 毫秒),它依次执行 waitForReadyRead(1)

这是一个已知问题,已记录 here .

关于c++ - QSerialPort readyRead() 信号不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080271/

相关文章:

c++ - OpenCV 中的鸟瞰投影误差

c++ - 有什么方法可以使内部在宏中通用?

python - 通过 python 和 sqlite3 访问替代方案

arduino - 如何知道按下了哪个按钮?

javascript - Ajax 将按钮单击提交到 php,然后提交到串行设备

c# - C# 中的 BesselK 函数

c++ - 我可以使用带有 OpenGL glTexImage2D 函数的灰度图像吗?

python - 使用 eventFilters 时,QTextEdit 光标出现问题

XCode 8 错误 "Project ERROR: Xcode not set up properly",即使许可证已被接受

arduino - 获取AT命令响应