使用串口步骤:
1. 首先需要QT支持串口模块,需要在pro文件中添加
QT += serialport #添加串口模块
2. 在实现头文件中添加串口的头文件
#include <QSerialPort> //串口
#include <QSerialPortInfo> //串口的信息
3. 在开发中实例化串口对象即可。
需要注意的是,串口发送数据前需要配置一些属性,打开后才能发送与接收数据。
基于QT的串口调试助手
可以实时检测串口是否在线,并可以发送与接收数据,通过ascii或hex来显示。
项目实现代码:
//dialog.hpp#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
//添加串口头文件
#include <QSerialPort>
#include <QSerialPortInfo>#include <QTimer>
#include <QDateTime>
#include <QMessageBox>
#include <QString>
#include <QTextCodec>namespace Ui {
class Dialog;
}class Dialog : public QDialog
{Q_OBJECT //宏替换,把一些声明加入到类中。public:explicit Dialog(QWidget *parent = 0);~Dialog();private:Ui::Dialog *ui;QStringList portStringList; //定义一个容器用来验证是否有串口需要添加QSerialPort *serial; //定义一个串口QTimer *timer1; //实现热插拔定时器
private slots:void timerouttofindport(); //超时函数void datarecv(); //数据接收void datasend(); //数据发送void openSerial(); //打开串口void closeSerial(); //关闭串口void findSerial(); //热插拔函数
};#endif // DIALOG_H//dialog.cpp#include "dialog.h"
#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog)
{ui->setupUi(this);ui->pushButton_close->setEnabled(false);ui->pushButton_send->setEnabled(false);findSerial();//检测电脑的串口timer1=new QTimer(this);QObject::connect(timer1,SIGNAL(timeout()),this,SLOT(timerouttofindport()));//定时器timer1->start(500);serial=new QSerialPort(this);//实例化串口QObject::connect(serial,SIGNAL(readyRead()),this,SLOT(datarecv()));//串口数据接收QObject::connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(datasend()));//发送数据QObject::connect(ui->pushButton_open,SIGNAL(clicked()),this,SLOT(openSerial()));//打开串口QObject::connect(ui->pushButton_close,SIGNAL(clicked()),this,SLOT(closeSerial()));//关闭串口QObject::connect(ui->pushButton_clear,SIGNAL(clicked()),ui->textBrowser_recv,SLOT(clear()));//清除接收数据
}Dialog::~Dialog()
{delete ui;
}void Dialog::findSerial()
{QStringList newPortStringList; //定义一个string容器newPortStringList.clear();foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())//foreach是qt定义的一个按照顺序遍历容器的循环{newPortStringList +=info.portName();//info就是遍历的成员,它的portName()方法是转化为QString类型}if(newPortStringList.size()!=portStringList.size())//判断是否有新的串口或者取出串口{closeSerial();portStringList=newPortStringList;ui->comboBox->clear();ui->comboBox->addItems(portStringList);}
}void Dialog::timerouttofindport()
{findSerial();timer1->start(500);
}void Dialog::datarecv()
{QByteArray temp=serial->readAll();int len=temp.length();QString sss;sss.setNum(len,10);if(ui->radioButton_ascii->isChecked()==true)//ascii{ui->textBrowser_recv->append(QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss zzz]")+" Recv ASCII 个数:"+sss);ui->textBrowser_recv->append(temp);}else//hex{ui->textBrowser_recv->append(QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss zzz]")+" Recv HEX 个数:"+sss);QString data1=temp.toHex();int length=data1.length(),i;for(i=0;i<=length/2;i++){data1.insert(2+3*i,' ');}ui->textBrowser_recv->append(data1);}
}void Dialog::datasend()
{QByteArray bytearray;bytearray=ui->textBrowser_send->toPlainText().toLatin1();serial->write(bytearray);int len=bytearray.length();QString sss;sss.setNum(len,10);if(ui->radioButton_ascii->isChecked()==true)//ascii{ui->textBrowser_recv->append(QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss zzz]")+" Send ASCII 个数:"+sss);QString temp=bytearray;ui->textBrowser_recv->append(temp.toLatin1());}else{ui->textBrowser_recv->append(QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss zzz]")+" Send HEX 个数:"+sss);QString data1=bytearray.toHex();int length=data1.length(),i;for(i=0;i<=length/2;i++){data1.insert(2+3*i,' ');}ui->textBrowser_recv->append(data1);}
}void Dialog::openSerial()
{serial->setPortName(ui->comboBox->currentText());//设置串口名字serial->setBaudRate(ui->comboBox_btl->currentText().toInt());//设置波特率switch (ui->comboBox_data->currentText().toInt())//设置数据位{case 5:serial->setDataBits(QSerialPort::Data5);break;case 6:serial->setDataBits(QSerialPort::Data6);break;case 7:serial->setDataBits(QSerialPort::Data7);break;case 8:serial->setDataBits(QSerialPort::Data8);break;default:serial->setDataBits(QSerialPort::UnknownDataBits);}switch (ui->comboBox_jiaoyan->currentIndex())//设置校验位{case 0:break;//偶校验case 1:serial->setParity(QSerialPort::OddParity);break;//奇校验case 2:serial->setParity(QSerialPort::NoParity);break;//无校验default:serial->setParity (QSerialPort::UnknownParity);break;}switch (ui->comboBox_stop->currentIndex())//停止位{case 0:serial->setStopBits(QSerialPort::OneStop);break;//1case 1:serial->setStopBits(QSerialPort::OneAndHalfStop);break;//1.5case 2:serial->setStopBits(QSerialPort::TwoStop);break;//2default:serial->setStopBits(QSerialPort::UnknownStopBits);}serial->setFlowControl(QSerialPort::NoFlowControl);//设置流控if(!serial->open(QIODevice::ReadWrite)){QMessageBox::warning(this,"error",serial->errorString(),QMessageBox::Ok);return;}ui->pushButton_open->setEnabled(false);ui->pushButton_close->setEnabled(true);ui->pushButton_send->setEnabled(true);ui->comboBox->setEnabled(false);ui->comboBox_btl->setEnabled(false);ui->comboBox_data->setEnabled(false);ui->comboBox_jiaoyan->setEnabled(false);ui->comboBox_stop->setEnabled(false);
}void Dialog::closeSerial()
{if(serial->isOpen())serial->close();ui->pushButton_open->setEnabled(true);ui->pushButton_close->setEnabled(false);ui->pushButton_send->setEnabled(false);ui->comboBox->setEnabled(true);ui->comboBox_btl->setEnabled(true);ui->comboBox_data->setEnabled(true);ui->comboBox_jiaoyan->setEnabled(true);ui->comboBox_stop->setEnabled(true);
}
ui:
本项目实现了基本的数据发送与接收,但中文乱码问题并没有解决。
本人才疏学浅,该项目到此为止。