QT5串口编写

来源:互联网 发布:金蝶java面试题 编辑:程序博客网 时间:2024/05/22 06:37

首先,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

原创粉丝点击