一,头文件
#include <QSerialPort> //提供访问串口的功能
#include <QSerialPortInfo> //提供系统中存在的串口的信息
二,搜索串口
//搜索串口,并添加到选项上供使用者选择 foreach(const QSerialPortInfo &Info,QSerialPortInfo ::availablePorts()) { QSerialPort CurrentPort; CurrentPort.setPort(Info); if(CurrentPort.open(QIODevice::ReadWrite)) { ui->comboBox_port->addItem(CurrentPort.portName());//插入串口的名字 CurrentPort.close(); //先开再关,把串口名称先导入 } }三,连接串口
CurrentUart->open(QIODevice::ReadWrite);四,读串口数据
QByteArry ReadBuf = CurrentUart->readAll(); if(!ReadBuf.isEmpty()) { str += tr(ReadBuf);//??? ui->plainTextEdit->appendPlainText(str); } ReadBuf.clear();
五&#x
