1.获取有效的串口、打开和关闭串口
01.在工程管理文件(.pro)内添加 "QT += serialport"
QT += serialport
02.在mainwindow.h文件中包含QSerialPort 、QSerialPortInfo头文件
#include <QSerialPort>
#include <QSerialPortInfo>
03.串口初始化程序,使用foreach获取有效的串口信息
//串口初始化
void MainWindow::SerialInit()
{
//使用foreach获取有效的串口信息
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
//这里相当于自动识别串口号之后添加到了cmb,如果要手动选择可以用下面列表的方式添加进去
MySerial.setPort(info);
if(MySerial.open(QIODevice::ReadWrite))
{
//将串口号添加到cmb
ui->comboBox_Port->addItem(info.portName());
//关闭串口等待人为(打开串口按钮)打开
MySerial.close();
}
}
}
04. 打开串口按钮操作函数
//打开设备按钮
void MainWindow::on_pushButton_Open_clicked()
{
//设置串口号;也就是说打开的是当前显示的串口
MySerial.setPortName(ui->comboBox_Port->currentText());
if(ui->pushButton_Open->text()=="打开设备")//打开串口
{
if(MySerial.open(QIODevice::ReadWrite))//读写方式打开,成功后设置串口
{
MySerial.setBaudRate(115200);//设置波特率
MySerial.setDataBits(QSerialPort::Data8);//设置数据位
MySerial.setParity(QSerialPort::NoParity);//设置校验位
MySerial.setFlowControl(QSerialPort::NoFlowControl);//设置流控制
MySerial.setStopBits(QSerialPort::OneStop);//设置停止位
ui->pushButton_Open->setText("关闭设备");
ui->comboBox_Port->setEnabled(false);//端口下拉框不可用
}
else//串口打开失败
{
QMessageBox::about(NULL, "提示", "打开出错,串口被占用!");
return ;
}
}
else if(ui->pushButton_Open->text()=="关闭设备")//关闭串口
{
ui->pushButton_Open->setText("打开设备");
MySerial.close();//关串口
ui->comboBox_Port->setEnabled(true);//端口下拉框可用
}
}
05.实现的效果,可以自动搜索到电脑的串口默认添加到下拉框中,点击pushbutton打开。
2.添加发送和接收部分功能
01.串口部分的接收(定时器接收方式)
串口的数据接收需要开启定时器,添加QTimer头文件,使用QTimer添加一个指针变量,声明一个定时器溢出触发的串口接收函数。
#include <QTimer>
QTimer *timer0;
void readSerialDataSlot();
打开串口需要开启定时器,以及串口接收时需要定时器对串口接收函数的槽绑定
//使用定时器循环读取,接收的数据
timer0 = new QTimer(this);
connect(timer0, SIGNAL(timeout()), this, SLOT(readSerialDataSlot()));//连接定时器时间到的槽函数
timer0->start(100);//100ms
关闭串口时需要关闭定时器
else if(ui->pushButton_Open->text()=="关闭设备")//关闭串口
{
ui->pushButton_Open->setText("打开设备");
timer0->stop();//关定时器
MySerial.close();//关串口
ui->comboBox_Port->setEnabled(true);//端口下拉框可用
}
定时器串口接收函数如下:
//定时器时间到后执行的槽函数-读取串口数据槽函数 并显示在接收区
void MainWindow::readSerialDataSlot()
{
QByteArray readData = MySerial.readAll();//读取串口数据
if(readData != NULL)//将读到的数据显示到数据接收区
{
ui->textEdit_Recv->append(readData);
readData.clear();//清除接收缓存
}
}
成功接收的效果:
02.串口数据的发送
按键串口发送数据函数如下:
//点击发送按钮槽函数-数据发送
void MainWindow::on_pushButton_Send_clicked()
{
QString SendData;
QByteArray SendHex;//HEX发送
if(ui->pushButton_Open->text()=="关闭设备")//打开串口才可以发送数据
{
if(!ui->textEdit_2->toPlainText().isEmpty())//发送区不为空
{
SendData = ui->textEdit_2->toPlainText();//获取发送区的数据
SendHex.append(SendData);
MySerial.write(SendHex,SendHex.length());//写入缓冲区
}
else//发送区为空
{
QMessageBox::about(NULL, "提示", "没有数据哦");
}
}
else//串口未打开
{
QMessageBox::about(NULL, "提示", "要打开串口哦!");
}
}
成功发送数据的效果:
3.添加十六进制发送和接收功能
接收部分函数:
void MainWindow::readSerialDataSlot()
{
QByteArray readData = MySerial.readAll();//读取串口数据
if(readData != NULL)//将读到的数据显示到数据接收区
{
if(ui->DisplayCheckBox->isChecked()==true)//选中HEX显示
{
readData=readData.toHex();//转为HEX
ui->textEdit_Recv->append(readData);
}
else//未选中HEX显示
{
ui->textEdit_Recv->append(readData);
}
readData.clear();//清除接收缓存
}
}
发送部分函数:
void MainWindow::on_pushButton_Send_clicked()
{
QString SendData;
QByteArray SendHex;//HEX发送
if(ui->pushButton_Open->text()=="关闭设备")//打开串口才可以发送数据
{
if(!ui->textEdit_2->toPlainText().isEmpty())//发送区不为空
{
SendData = ui->textEdit_2->toPlainText();//获取发送区的数据
if(ui->SendCheckBox->isChecked()==true)//SendCheckBox被选中HEX发送
{
SendHex.append(SendData).toHex();//转HEX存储
}
else//没有选中HEX发送
{
SendHex.append(SendData);
//qDebug()<<SendHex<<endl;
}
MySerial.write(SendHex,SendHex.length());//写入缓冲区
}
else//发送区为空
{
QMessageBox::about(NULL, "提示", "没有数据哦");
}
}
else//串口未打开
{
QMessageBox::about(NULL, "提示", "要打开串口哦!");
}
}