gpt4 book ai didi

c++ - QSerial错误与Arduino通信

转载 作者:行者123 更新时间:2023-11-30 05:29:23 25 4
gpt4 key购买 nike

我编写了一个简单的程序来使用 Qt(根据 this guide 之后的 Visual Studio 2013 源代码构建的 5.5 版)与 Arduino 进行通信。

这是一个简单的固件,它从串口读取并在 10 秒后打印“1”:

void setup() {
Serial.begin(9600); //Open Serial connection for debugging
}

void loop() {
if (Serial.available()) {
char user_input = Serial.read();
if (user_input == '1') {
delay(10000);
Serial.print('1');
}
}

这是 Qt 程序,当我按下开始按钮并继续监听要读取的消息时,它会在串行上发送“1”:

主要.cpp

#include "mainwindow.h"
#include <QApplication>

#pragma comment(lib, "advapi32")

int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

主窗口.h

#pragma once

#include <QMainWindow>
#include "SerialHandler.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT

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

private:
Ui::MainWindow* ui;
SerialHandler serialHandler;
public slots:
void onStart();
};

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::MainWindow), serialHandler(QString("COM6")) {
ui->setupUi(this);
QObject::connect(ui->startButton, SIGNAL(released()), this, SLOT(onStart()));
}

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

void MainWindow::onStart() {
serialHandler.moveForward();
}

SerialHandler.h

#pragma once

#include <QString>
#include <QtSerialPort>
#include <QtSerialPort/QSerialPort>


class SerialHandler {
public:
SerialHandler(QString& serialName);
~SerialHandler();
void moveForward();

private:
QSerialPort serial;
void writeOnSerial(QByteArray& msg);

private slots:
void handleReadyRead();
};

串口处理器.cpp

#include "SerialHandler.h"
#include <QDebug>
#include <QObject>


SerialHandler::SerialHandler(QString& serialName){
connect(&serial, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));

// Initialize Serial
serial.setPortName(serialName);

if (!serial.setBaudRate(QSerialPort::Baud9600)) {
throw std::runtime_error(QObject::tr("Can't set rate 9600 baud to port %1, error code %2")
.arg(serial.portName()).arg(serial.error()).toStdString());
}

if (!serial.open(QIODevice::ReadWrite)) {
throw std::runtime_error(QObject::tr("Can't open %1, error code %2")
.arg(serial.portName()).arg(serial.error()).toStdString());
}

if (!serial.setDataBits(QSerialPort::Data8)) {
throw std::runtime_error(QObject::tr("Can't set 8 data bits to port %1, error code %2")
.arg(serial.portName()).arg(serial.error()).toStdString());
}

if (!serial.setParity(QSerialPort::NoParity)) {
throw std::runtime_error(QObject::tr("Can't set no patity to port %1, error code %2")
.arg(serial.portName()).arg(serial.error()).toStdString());
}

if (!serial.setStopBits(QSerialPort::OneStop)) {
throw std::runtime_error(QObject::tr("Can't set 1 stop bit to port %1, error code %2")
.arg(serial.portName()).arg(serial.error()).toStdString());
}

if (!serial.setFlowControl(QSerialPort::NoFlowControl)) {
throw std::runtime_error(QObject::tr("Can't set no flow control to port %1, error code %2")
.arg(serial.portName()).arg(serial.error()).toStdString());
}
}

SerialHandler::~SerialHandler(){
if (serial.isOpen()) {
serial.close();
}
}

void SerialHandler::handleReadyRead() {
qDebug() << serial.readAll();
}

void SerialHandler::writeOnSerial(QByteArray& msg) {
if (serial.isOpen() && serial.isWritable()) {
serial.write(msg);
serial.flush();
}
}

void SerialHandler::moveForward(){
QByteArray msg("1");
writeOnSerial(msg);
}

问题出现在 writeOnSerial 方法 (SerialHandler.cpp) 中。调用 serial.write() 会引发此错误:

QObject::startTimer: Timers can only be used with threads started with QThread

“1”写入串行并由 arduino 读取,但计算机在 10 秒后再也没有收到任何回复,readyRead() 信号永远不会被触发!

我在 this post 中看到了同样的错误,但在我的例子中,exec 循环就在那里!

有什么建议吗?

最佳答案

这个最小的测试用例适用于我在 Windows 和 OS X 上的 Arduino,使用最新的 Qt 5.5。

这种最小化属于你的问题。多个文件(以及缺少的 .ui)并不是演示此类简单故障所必需的。

// https://github.com/KubaO/stackoverflown/tree/master/questions/miniserial-36431493
#include <QtWidgets>
#include <QtSerialPort>

int main(int argc, char ** argv) {
QApplication app(argc, argv);
QWidget w;
QFormLayout layout(&w);
QPushButton ping("Send");
QTextBrowser output;
layout.addRow(&ping);
layout.addRow(&output);
w.show();

QSerialPort port("COM6");
port.setBaudRate(QSerialPort::Baud9600); // these are guaranteed to return true
port.setDataBits(QSerialPort::Data8);
port.setParity(QSerialPort::NoParity);
port.setStopBits(QSerialPort::OneStop);
port.setFlowControl(QSerialPort::NoFlowControl);
if (!port.open(QIODevice::ReadWrite))
output.append("! Can't open the port :(<br/>");

QObject::connect(&ping, &QPushButton::clicked, &port, [&]{
if (port.isOpen()) {
port.write("1");
output.append("> 1<br/>");
} else
output.append("! Write failed :(<br/>");
});
QObject::connect(&port, &QIODevice::readyRead, &output, [&]{
auto data = port.readAll();
output.append(QStringLiteral("< %1<br/>")
.arg(QString::fromLatin1(data).toHtmlEscaped()));
});

return app.exec();
}

关于c++ - QSerial错误与Arduino通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com