[英]Why I am reading garbage from the serial port?
I am trying to get the serial data and display it on the GUI, that i made in the QT for that when I try to get the serial data and display it in the output, it shows me this garbage type value.我试图获取串行数据并将其显示在 GUI 上,这是我在 QT 中制作的,当我尝试获取串行数据并将其显示在输出中时,它向我显示了这个垃圾类型值。 "?\?#\??\?#\??" “?\?#\??\?#\??” "?\?#\??\?#\???\?#\" "?\?#\??\?#\???\?#\"
this whole code is below, but the culprit lines are "readserial" function in the last.整个代码如下,但罪魁祸首是最后的“readserial”函数。
#include "dialog.h"
#include "ui_dialog.h"
#include <QSerialPort>
#include<string>
#include<QDebug>
#include<QMessageBox>
#include <QSerialPortInfo>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->label->setText("0");
arduino = new QSerialPort(this);
serialBuffer = "";
bool arduino_is_availabe = false;
QString arduino_uno_port_name;
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
if(serialPortInfo.hasProductIdentifier() && serialPortInfo.hasVendorIdentifier())
{
if((serialPortInfo.productIdentifier() == arduino_uno_product_id) && (serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id))
{
arduino_is_availabe = true;
arduino_uno_port_name = serialPortInfo.portName();
}
}
}
if(arduino_is_availabe)
{
qDebug() <<"Found the arduino port...\n";
arduino->setPortName(arduino_uno_port_name);
arduino->open(QSerialPort::ReadOnly);
arduino->setBaudRate(QSerialPort::Baud9600);
arduino->setBaudRate(QSerialPort::Data8);
arduino->setFlowControl(QSerialPort::NoFlowControl);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
}else
{
qDebug() <<"Couldn't find the correct port for the arduino.\n";
QMessageBox::information(this, "serial port error", "couldn't open the serial port to arduino ");
}
}
Dialog::~Dialog()
{
if(arduino->isOpen())
{
arduino->close(); // Close the serial port if it's open.
}
delete ui;
}
void Dialog::readSerial()
{
QByteArray serialData;
serialData=arduino->readAll();
serialBuffer+=QString::fromStdString(serialData.toStdString());
qDebug()<<serialBuffer;
}
void Dialog::on_label_linkActivated(const QString &link)
{
ui->label->setText(serialBuffer);
}
I think your problem is right here:我认为你的问题就在这里:
arduino->setBaudRate(QSerialPort::Baud9600);
arduino->setBaudRate(QSerialPort::Data8);
You are setting the baud rate twice in a row, which makes no sense.您连续两次设置波特率,这是没有意义的。 In the second line you are setting a value of QSerialPort::Data8 , so perhaps you are trying to define the setDataBits method.在第二行中,您正在设置QSerialPort::Data8的值,因此您可能正在尝试定义setDataBits方法。 It would look like this:它看起来像这样:
arduino->setBaudRate(QSerialPort::Baud9600);
arduino->setDataBits(QSerialPort::Data8);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.