QT5串口编写

首先,QT5是自带QSerialPort这个类的,使用时需要在pro文件里面添加一行:

QT += serialport

然后直接引用头文件就可以使用了。

#include <QtSerialPort/QSerialPort>  
#include <QtSerialPort/QSerialPortInfo> 

QSerialPort:提供访问串口的功能

QSerialPortInfo:提供系统中存在的串口的信息

接下来需要创建一个QSerialPort的对象,对串口的名称、波特率、数据位、校验位、停止位等参数进行设置,然后才进行串口读写操作。
大概总结了一下,设置、读、写的过程。

一、设置(举例):

QSerialPort *serial = new QSerialPort;  
//设置串口名  
serial->setPortName(name);  
//打开串口  
serial->open(QIODevice::ReadWrite);  
//设置波特率  
serial->setBaudRate(BaudRate);  
//设置数据位数  
serial->setDataBits(QSerialPort::Data8);  
 //设置奇偶校验  
 serial->setParity(QSerialPort::NoParity);   
//设置停止位  
serial->setStopBits(QSerialPort::OneStop);  
//设置流控制  
serial->setFlowControl(QSerialPort::NoFlowControl); 

这里设置了串口名为name,打开串口并设置为可读可写,波特率为BaudRate,数据位为8位,没有奇偶校验位,停止位为1位,没有流控制。设置完这些就能进行读写操作了。作为一名新手,发现遇到不懂得可以在QtCreator里面可以选择关键字,按F1打开文档看类、函数等数据的手册。

二、读取数据

void MainWindow::Read_Data()  
{  
    QByteArray buf;  
    buf = serial->readAll();  
} 

三、发送数据

serial->write(data); 

使用write函数便可以把字符串data一个个字节发送出去。

使用串口就只需以上步骤,使用完后只需要执行

serial->close(); 

就可以关闭串口了。

完整代码:

//mianwindow.h  
#ifndef MAINWINDOW_H  
#define MAINWINDOW_H  
#include <QMainWindow>  
#include <QDebug>  
#include <QtSerialPort/QSerialPort>  
#include <QtSerialPort/QSerialPortInfo>  
namespace Ui {  
class MainWindow;  
}  
class MainWindow : public QMainWindow  
{  
    Q_OBJECT  
public:  
    explicit MainWindow(QWidget *parent = 0);  
    ~MainWindow();  
private slots:  
    void on_clearButton_clicked();  
    void on_sendButton_clicked();  
    void on_openButton_clicked();  
    void Read_Data();  
private:  
    Ui::MainWindow *ui;  
    QSerialPort *serial;  
};  
#endif // MAINWINDOW_H  
//mainwindow.c  
#include "mainwindow.h"  
#include "ui_mainwindow.h"  

MainWindow::MainWindow(QWidget *parent) :  
    QMainWindow(parent),  
    ui(new Ui::MainWindow)  
{  
    ui->setupUi(this);  
    //查找可用的串口  
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())  
    {  
        QSerialPort serial;  
        serial.setPort(info);  
        if(serial.open(QIODevice::ReadWrite))  
        {  
            ui->PortBox->addItem(serial.portName());  
            serial.close();  
        }  
    }  
    //设置波特率下拉菜单默认显示第三项  
    ui->BaudBox->setCurrentIndex(3);  
    //关闭发送按钮的使能  
    ui->sendButton->setEnabled(false);  
    qDebug() << tr("界面设定成功!");  
}  
MainWindow::~MainWindow()  
{  
    delete ui;  
}  
//清空接受窗口  
void MainWindow::on_clearButton_clicked()  
{  
    ui->textEdit->clear();  
}  
//发送数据  
void MainWindow::on_sendButton_clicked()  
{  
    serial->write(ui->textEdit_2->toPlainText().toLatin1());  
}  
//读取接收到的数据  
void MainWindow::Read_Data()  
{  
    QByteArray buf;  
    buf = serial->readAll();  
    if(!buf.isEmpty())  
    {  
        QString str = ui->textEdit->toPlainText();  
        str+=tr(buf);  
        ui->textEdit->clear();  
        ui->textEdit->append(str);  
    }  
    buf.clear();  
}  
void MainWindow::on_openButton_clicked()  
{  
    if(ui->openButton->text()==tr("打开串口"))  
    {  
        serial = new QSerialPort;  
        //设置串口名  
        serial->setPortName(ui->PortBox->currentText());  
        //打开串口  
        serial->open(QIODevice::ReadWrite);  
        //设置波特率  
        serial->setBaudRate(ui->BaudBox->currentText().toInt());  
        //设置数据位数  
        switch(ui->BitNumBox->currentIndex())  
        {  
        case 8: serial->setDataBits(QSerialPort::Data8); break;  
        default: break;  
        }  
        //设置奇偶校验  
        switch(ui->ParityBox->currentIndex())  
        {  
        case 0: serial->setParity(QSerialPort::NoParity); break;  
        default: break;  
        }  
        //设置停止位  
        switch(ui->StopBox->currentIndex())  
        {  
        case 1: serial->setStopBits(QSerialPort::OneStop); break;  
        case 2: serial->setStopBits(QSerialPort::TwoStop); break;  
        default: break;  
        }  
        //设置流控制  
        serial->setFlowControl(QSerialPort::NoFlowControl);  
        //关闭设置菜单使能  
        ui->PortBox->setEnabled(false);  
        ui->BaudBox->setEnabled(false);  
        ui->BitNumBox->setEnabled(false);  
        ui->ParityBox->setEnabled(false);  
        ui->StopBox->setEnabled(false);  
        ui->openButton->setText(tr("关闭串口"));  
        ui->sendButton->setEnabled(true);  
        //连接信号槽  
        QObject::connect(serial, &QSerialPort::readyRead, this, &MainWindow::Read_Data);  
    }  
    else  
    {  
        //关闭串口  
        serial->clear();  
        serial->close();  
        serial->deleteLater();  
        //恢复设置使能  
        ui->PortBox->setEnabled(true);  
        ui->BaudBox->setEnabled(true);  
        ui->BitNumBox->setEnabled(true);  
        ui->ParityBox->setEnabled(true);  
        ui->StopBox->setEnabled(true);  
        ui->openButton->setText(tr("打开串口"));  
        ui->sendButton->setEnabled(false);  
    }  
}  

转自:http://blog.csdn.net/u014695839/article/details/50611549

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt是一款跨平台的C++应用程序开发框架,它提供了丰富的类库和工具,可以用来开发各种类型的应用程序,包括串口通信程序。 Qt提供了QSerialPort类,可以方便地进行串口通信的编程。下面是一个简单的例子,演示了如何利用Qt编写串口通信程序: ```cpp #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> #include <QtCore/QString> #include <QtCore/QDebug> int main() { // 扫描可用的串口 QList<QSerialPortInfo> serialPortInfos = QSerialPortInfo::availablePorts(); foreach (const QSerialPortInfo &serialPortInfo, serialPortInfos) { qDebug() << "Port: " << serialPortInfo.portName() << "Description: " << serialPortInfo.description(); } // 创建串口对象 QSerialPort serial; // 设置串口参数 serial.setPortName("COM1"); // 设置串口名字 serial.setBaudRate(QSerialPort::Baud115200); // 设置波特率 serial.setDataBits(QSerialPort::Data8); // 设置数据位 serial.setParity(QSerialPort::NoParity); // 设置校验位 serial.setStopBits(QSerialPort::OneStop); // 设置停止位 serial.setFlowControl(QSerialPort::NoFlowControl); // 设置流控制 // 打开串口 if (serial.open(QIODevice::ReadWrite)) { qDebug() << "Serial port opened!"; // 读取数据 QByteArray data = serial.readAll(); qDebug() << "Read data: " << data; // 写入数据 serial.write("Hello, Serial!"); // 关闭串口 serial.close(); } else { qDebug() << "Failed to open serial port!"; } return 0; } ``` 以上代码演示了如何使用Qt进行串口通信。首先需要扫描可用的串口,然后创建一个QSerialPort对象,并设置串口参数。接下来,打开串口,并可以通过read()函数读取数据,通过write()函数写入数据。最后,关闭串口。 使用Qt编写串口通信程序,可以方便地实现串口相关的功能,并且跨平台性强,可以在不同的操作系统上运行。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值