Qt5之串口编程入门篇之写数据

Qt5.0以上版本集成了两个串口库,QSerialPort.h以及QSerialPortInfo.h 

QSerialPort类提供了操作串口的函数。QSerialPortInfo提供本机串口信息。

编程软件QtCreate:

新建工程一路确认,finish。


编程开始:

首先在pro文件中加入一句:QT += serialport

点击构建->Qmake;

开始编程:


#include "mainwindow.h"
#include 
#include
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
    QSerialPort myPort;
    myPort.setPortName("COM3");//设置串口3
    myPort.open( QIODevice::WriteOnly);//打开串口三并设置为只写模式
    myPort.write("hello");//写数据

    return a.exec();
}


 以上程序就完成了通过串口“COM3”发送hello数据的目的,这是最最简单的串口写数据程序。

电脑安装虚拟串口软件之后,可以通过串口助手接收到hello信息。当然,停止位、校验位、波特率、硬件输出流控制都默认设置。

万事开头难,在此基础上可以开始开发复杂的串口程序了。



 

你可能感兴趣的:(个人开发)