首先,QT5是自带QSerialPort这个类的,使用时需要在pro文件里面添加一行:QTserialport 然后直接引用头文件就可以使用了。includeQtSerialPortQSerialPortincludeQtSerialPortQSerialPortInfo QSerialPort:提供访问串口的功能 QSerialPortInfo:提供系统中存在的串口的信息 接下来需要创建一个QSerialPort的对象,对串口的名称、波特率、数据位、校验位、停止位等参数进行设置,然后才进行串口读写操作。 大概总结了一下,设置、读、写的过程。一、设置(举例)QSerialPortserialnewQSerialPort;设置串口名serialsetPortName(name);打开串口serialopen(QIODevice::ReadWrite);设置波特率serialsetBaudRate(BaudRate);设置数据位数serialsetDataBits(QSerialPort::Data8);设置奇偶校验serialsetParity(QSerialPort::NoParity);设置停止位serialsetStopBits(QSerialPort::OneStop);设置流控制serialsetFlowControl(QSerialPort::NoFlowControl); 这里设置了串口名为name(通常为COMXX),打开串口并设置为可读可写,波特率为BaudRate,数据位为8位,没有奇偶校验位,停止位为1位,没有流控制。设置完这些就能进行读写操作了。作为一名新手,发现遇到不懂得可以在QtCreator里面可以选择关键字,按F1打开文档看类、函数等数据的手册。二、读取数据voidMainWindow::ReadData(){QByteArraybuf;bufserialreadAll();} 当串口收到数据并且接收完毕后,会发出一个readyRead()的信号,因此只需要编写一个槽函数ReadData(),设置信号槽,并在槽函数中使用readAll()把收到的数据读到buf中。三、发送数据serialwrite(data); 使用write函数便可以把字符串data一个个字节发送出去。 使用串口就只需以上步骤,使用完后只需要执行serialclose(); 就可以关闭串口了。我使用了ui界面设计来编写上位机的,界面如下: 代码如下:mianwindow。hifndefMAINWINDOWHdefineMAINWINDOWHincludeQMainWindowincludeQDebugincludeQtSerialPortQSerialPortincludeQtSerialPortQSerialPortInfonamespaceUi{classMainWindow;}classMainWindow:publicQMainWindow{QOBJECTpublic:explicitMainWindow(QWidgetparent0);MainWindow();privateslots:voidonclearButtonclicked();voidonsendButtonclicked();voidonopenButtonclicked();voidReadData();private:Ui::MainWindowui;QSerialPortserial;};endifMAINWINDOWHmainwindow。cincludemainwindow。hincludeuimainwindow。hMainWindow::MainWindow(QWidgetparent):QMainWindow(parent),ui(newUi::MainWindow){uisetupUi(this);查找可用的串口foreach(constQSerialPortInfoinfo,QSerialPortInfo::availablePorts()){QSerialPortserial;serial。setPort(info);if(serial。open(QIODevice::ReadWrite)){uiPortBoxaddItem(serial。portName());serial。close();}}设置波特率下拉菜单默认显示第三项uiBaudBoxsetCurrentIndex(3);关闭发送按钮的使能uisendButtonsetEnabled(false);qDebug()tr(界面设定成功!);}MainWindow::MainWindow(){deleteui;}清空接受窗口voidMainWindow::onclearButtonclicked(){uitextEditclear();}发送数据voidMainWindow::onsendButtonclicked(){serialwrite(uitextEdit2toPlainText()。toLatin1());}读取接收到的数据voidMainWindow::ReadData(){QByteArraybuf;bufserialreadAll();if(!buf。isEmpty()){QStringstruitextEdittoPlainText();strtr(buf);uitextEditclear();uitextEditappend(str);}buf。clear();}voidMainWindow::onopenButtonclicked(){if(uiopenButtontext()tr(打开串口)){serialnewQSerialPort;设置串口名serialsetPortName(uiPortBoxcurrentText());打开串口serialopen(QIODevice::ReadWrite);设置波特率serialsetBaudRate(uiBaudBoxcurrentText()。toInt());设置数据位数switch(uiBitNumBoxcurrentIndex()){case8:serialsetDataBits(QSerialPort::Data8);break;default:break;}设置奇偶校验switch(uiParityBoxcurrentIndex()){case0:serialsetParity(QSerialPort::NoParity);break;default:break;}设置停止位switch(uiStopBoxcurrentIndex()){case1:serialsetStopBits(QSerialPort::OneStop);break;case2:serialsetStopBits(QSerialPort::TwoStop);break;default:break;}设置流控制serialsetFlowControl(QSerialPort::NoFlowControl);关闭设置菜单使能uiPortBoxsetEnabled(false);uiBaudBoxsetEnabled(false);uiBitNumBoxsetEnabled(false);uiParityBoxsetEnabled(false);uiStopBoxsetEnabled(false);uiopenButtonsetText(tr(关闭串口));uisendButtonsetEnabled(true);连接信号槽QObject::connect(serial,QSerialPort::readyRead,this,MainWindow::ReadData);}else{关闭串口serialclear();serialclose();serialdeleteLater();恢复设置使能uiPortBoxsetEnabled(true);uiBaudBoxsetEnabled(true);uiBitNumBoxsetEnabled(true);uiParityBoxsetEnabled(true);uiStopBoxsetEnabled(true);uiopenButtonsetText(tr(打开串口));uisendButtonsetEnabled(false);}}main。cincludemainwindow。hincludeQApplicationintmain(intargc,charargv〔〕){QApplicationa(argc,argv);MainWindoww;w。show();returna。exec();}