Arduino与Qt 5.7之间的双向串行通信 我试图把数据从ARDUINO转移到C++ QT5.7,从ARDUNO到C++ QT5.7(MIWW)程序。
我能够毫无问题地将数据从QT传输到ARDUINO。 Arduino完美地闪烁着 另一方面,从ARDUINO传输到QT的数据并不总是预期的(在应该“LED关闭”时发送“LED打开”),有时它根本不通信 QT代码:Arduino与Qt 5.7之间的双向串行通信 我试图把数据从ARDUINO转移到C++ QT5.7,从ARDUNO到C++ QT5.7(MIWW)程序。,c++,qt,arduino,serial-port,C++,Qt,Arduino,Serial Port,我能够毫无问题地将数据从QT传输到ARDUINO。 Arduino完美地闪烁着 另一方面,从ARDUINO传输到QT的数据并不总是预期的(在应该“LED关闭”时发送“LED打开”),有时它根本不通信 QT代码: #include <QCoreApplication> #include <QDebug> #include <QSerialPort> #include <QSerialPortInfo> #include <QThread>
#include <QCoreApplication>
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QThread>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSerialPort serial;
serial.setPortName("COM6");
serial.setBaudRate(9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if(serial.open(QSerialPort::ReadWrite))
{
string c;
QByteArray s;
QByteArray received;
while(true)
{
qDebug("TRUE");
//WRITE
cin >> c;
cout << endl;
s = QByteArray::fromStdString(c);
serial.write(s);
serial.waitForBytesWritten(-1);
//serial.flush();
s = serial.readAll();
serial.waitForReadyRead(-1);
cout << s.toStdString() << endl;
//serial.flush();
}
}
else
{
QString error = serial.errorString();
cout << error.toStdString() << endl;
qDebug("FALSE") ;
}
serial.close();
return a.exec();
}
错误的终端映像:
请帮忙!
谢谢,我认为您必须在QT上使用EOL和Carrige返回字符。尝试替换Arduino代码中的
Serial.write
到Serial.println
。您没有任何打包功能:在单个数据块之间没有分隔符-除了时间传递
write
,而应使用println
,以便每条消息都是完整的一行waitForReadyRead
之后从串行端口获得完整响应。您可以保证至少有1个字节可供读取。这就是你问题的根源。请注意,您是如何获得LE
,然后不久您将D关闭
,紧接着LED打开
。您必须等待数据,直到完整的行可用QTextStream
而不是iostream来减少使用的API数量。最后,您不需要app.exec
,因为您编写了阻塞代码
// https://github.com/KubaO/stackoverflown/tree/master/questions/arduino-read-40246601
#include <QtSerialPort>
#include <cstdio>
int main(int argc, char *argv[])
{
QCoreApplication a{argc, argv};
QTextStream in{stdin};
QTextStream out{stdout};
QSerialPort port;
port.setPortName("COM6");
port.setBaudRate(9600);
port.setDataBits(QSerialPort::Data8);
port.setParity(QSerialPort::NoParity);
port.setStopBits(QSerialPort::OneStop);
port.setFlowControl(QSerialPort::NoFlowControl);
if (!port.open(QSerialPort::ReadWrite)) {
out << "Error opening serial port: " << port.errorString() << endl;
return 1;
}
while(true)
{
out << "> ";
auto cmd = in.readLine().toLatin1();
if (cmd.length() < 1)
continue;
port.write(cmd);
while (!port.canReadLine())
port.waitForReadyRead(-1);
while (port.canReadLine())
out << "< " << port.readLine(); // lines are already terminated
}
}
//https://github.com/KubaO/stackoverflown/tree/master/questions/arduino-read-40246601
#包括
#包括
int main(int argc,char*argv[])
{
qcorea{argc,argv};
{stdin}中的QTextStream;
QTextStream out{stdout};
QSerialPort端口;
port.setPortName(“COM6”);
波特率(9600);
setDataBits(QSerialPort::Data8);
setParity(QSerialPort::NoParity);
端口.setStopBits(QSerialPort::OneStop);
port.setFlowControl(QSerialPort::NoFlowControl);
如果(!port.open(QSerialPort::ReadWrite)){
非常感谢您提供完整且最少的代码来重现您的问题。欢迎使用堆栈溢出!
// https://github.com/KubaO/stackoverflown/tree/master/questions/arduino-read-40246601
#include <QtSerialPort>
#include <cstdio>
int main(int argc, char *argv[])
{
QCoreApplication a{argc, argv};
QTextStream in{stdin};
QTextStream out{stdout};
QSerialPort port;
port.setPortName("COM6");
port.setBaudRate(9600);
port.setDataBits(QSerialPort::Data8);
port.setParity(QSerialPort::NoParity);
port.setStopBits(QSerialPort::OneStop);
port.setFlowControl(QSerialPort::NoFlowControl);
if (!port.open(QSerialPort::ReadWrite)) {
out << "Error opening serial port: " << port.errorString() << endl;
return 1;
}
while(true)
{
out << "> ";
auto cmd = in.readLine().toLatin1();
if (cmd.length() < 1)
continue;
port.write(cmd);
while (!port.canReadLine())
port.waitForReadyRead(-1);
while (port.canReadLine())
out << "< " << port.readLine(); // lines are already terminated
}
}
#include <QtSerialPort>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app{argc, argv};
QWidget ui;
QFormLayout layout{&ui};
QLineEdit portName{"COM6"};
QTextBrowser term;
QLineEdit command;
QPushButton open{"Open"};
layout.addRow("Port", &portName);
layout.addRow(&term);
layout.addRow("Command:", &command);
layout.addRow(&open);
ui.show();
QSerialPort port;
port.setBaudRate(9600);
port.setDataBits(QSerialPort::Data8);
port.setParity(QSerialPort::NoParity);
port.setStopBits(QSerialPort::OneStop);
port.setFlowControl(QSerialPort::NoFlowControl);
QObject::connect(&open, &QPushButton::clicked, &port, [&]{
port.setPortName(portName.text());
if (port.open(QSerialPort::ReadWrite)) return;
term.append(QStringLiteral("* Error opening serial port: %1").arg(port.errorString()));
});
QObject::connect(&command, &QLineEdit::returnPressed, &port, [&]{
term.append(QStringLiteral("> %1").arg(command.text()));
port.write(command.text().toLatin1());
});
QObject::connect(&port, &QIODevice::readyRead, &term, [&]{
if (!port.canReadLine()) return;
while (port.canReadLine())
term.append(QStringLiteral("< %1").arg(QString::fromLatin1(port.readLine())));
});
return app.exec();
}