我写了一个简单的程序来使用Qt(5.5从源代码构建为Visual Studio 2013以下的版本本指南 )与Arduino的到comunicate。
这里是从串行和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');
}
}
这里是一个发送“1”系列时,我按下启动按钮并保持监听消息读了Qt程序:
main.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();
}
mainwindow.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();
};
mainwindow.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();
};
SerialHandler.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()信号也决不解雇了!
我看到了同样的错误,在这个岗位 ,但在我的情况下,高管循环是有!
有什么建议么?