【问题标题】:QT - Write and Read using QSerialPort (ASCII command)QT - 使用 QSerialPort 写入和读取(ASCII 命令)
【发布时间】:2024-01-19 03:52:01
【问题描述】:

我正在通过 QT 制作程序。需要PC和串口通信。

所以我连接了串口,但是我不知道怎么写和读。 该文档说“接口(串行端口)响应通过 USB 接收的 ASCII 命令,充当虚拟串行端口。”

我想我必须使用 QIODevice::Write 和 read 但我不知道如何使用它们并解释响应。能帮我一下吗? 我附上了我的代码和文件的某些部分。下面

motorport.h

#ifndef MOTORPORT_H
#define MOTORPORT_H

#include <QSerialPort>


class MotorPort : public QSerialPort
{
    Q_OBJECT
~~~~~

motorport.cpp

void MotorPort::openMotorPort(const QString &portName)
{
    setPortName(portName);
    setBaudRate(defaultBaudRate);
    setDataBits(defaultDataBits);
    setStopBits(defaultStopBits);
    setParity(defaultParity);
    open(QIODevice::ReadWrite);

}

ma​​inwindow.h

private:
    Ui::MainWindow *ui;
    MotorPort *rls;

//mainwindow.cpp
rls->openMotorPort("COM5");  //success to connect serialport
rls->write("?");  //try to send ASCII command '?' but I don't know....
QByteArray data = rls->readAll();  //

Reference Screenshot image

【问题讨论】:

  • 您的具体问题是什么?请把你的问题写清楚

标签: c++ qt serial-port ascii


【解决方案1】:
QObject::connect(serial,&QSerialPort::readyRead,this,&MainWindow::ReadData);

//read

void MainWindow::ReadData()
{
QByteArray buf;

buf=serial->readAll();
if(!buf.isEmpty())
{
    QString str = buf;
    ui->txtRead->appendPlainText(str);
}
buf.clear();
}


// write
{

serial->write(ui->txtWrite->toPlainText().toLatin1());

}

【讨论】:

    【解决方案2】:

    也许这个链接可以帮助你:https://forum.qt.io/topic/63856/read-and-write-in-serial-port

    #include "myserialport.h"
    #include <QtSerialPort/QSerialPort>
    #include <QMessageBox>
    #include <QObject>
    
    MySerialPort::MySerialPort()
    {
       serial = new QSerialPort(this);
       connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
       //openSerialPort();
    }
    
    
    void MySerialPort::openSerialPort()
    {
       serial->setPortName("COM3");
       serial->setBaudRate(QSerialPort::Baud9600);
       serial->setDataBits(QSerialPort::Data8);
       serial->setParity(QSerialPort::NoParity);
       serial->setStopBits(QSerialPort::OneStop);
       serial->setFlowControl(QSerialPort::NoFlowControl);
       if (serial->open(QIODevice::ReadWrite)) {
    
           showStatusMessage("Connectedd");
    
       } else {
    
           showStatusMessage(tr("Open error"));
       }
    }
    
    void MySerialPort::closeSerialPort()
    {
       if (serial->isOpen())
           serial->close();
    
       showStatusMessage(tr("Disconnected"));
    }
    
    void MySerialPort::writeData(const QByteArray &data)
    {
       serial->write(data);
    }
    
    void MySerialPort::readData()
    {
      QByteArray data = serial->readAll();
    
      qDebug() << data;
    
    }
    
    void MySerialPort::handleError(QSerialPort::SerialPortError error)
    {
       if (error == QSerialPort::ResourceError) {
           closeSerialPort();
       }
    }
    
    
    void MySerialPort::showStatusMessage(const QString &message)
    {
       qDebug() << message;
    }
    

    注意上面的代码是来自上述链接的逐字复制。

    【讨论】: