由于工作原因,需要设计一款串口测试软件,方便以后工作中各种仪表及设备的通讯测试,现在完成了初版软件。
1、首先需要安装虚拟串口软件,安装完毕后,配置虚拟串口3,串口4,相互对应。虚拟串口软件很好找,百度一下下载使用便可。
2、下面测试Qt串口软件与普通串口软件,之间发送数据进行通讯测试,左边Qt串口程序,右边普通串口程序
2.1 下面为qt发送字符串,普通串口接收。
2.2 下面为qt发送16进制,普通串口接收16进制。
2.3 下面为普通串口发送字符串,Qt串口接收字符串
2.4 下面为普通串口发送16进制,Qt串口接收16进制
下面附源码:
1、pro文件
#-------------------------------------------------
#
# Project created by QtCreator 2019-10-25T14:04:08
#
#-------------------------------------------------
QT += core gui
QT += widgets
#添加串口类
QT += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = LFLCCS
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
2、mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//目录相关
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
//字符串转16进制
void StringToHex(QString str,QByteArray &senddata);
//字符转16进制
unsigned char ByteArrayToHexChar(unsigned char ch);
void on_clrearReceive_btn_clicked();
void on_sendMessage_btn_clicked();
void on_openSerial_btn_clicked();
void readData();
void on_closeSerial_btn_clicked();
void on_clearSend_btn_clicked();
void on_receiveToHex_ceb_stateChanged(int arg1);
void on_sendToHex_ceb_2_stateChanged(int arg1);
void on_receiveToHex_stateChanged(int arg1);
void on_sendToHex_2_stateChanged(int arg1);
void on_sendToHex_ceb_stateChanged(int arg1);
void on_saveReceiveMessage_btn_clicked();
private:
Ui::MainWindow *ui;
QSerialPort serial;
QVBoxLayout receiveVBL;
QString showStr;
int receiveHexState=0;
int sendHexState=0;
QString receiveHexString,sendHexString;
};
#endif // MAINWINDOW_H
3、main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
4、mainwindows.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ ui->setupUi(this);
ui->sendMessage_te->setFont(QFont("Timers",50,QFont::Bold));
//QVBoxLayout receiveVBL= new QVBoxLayout;
// QVBoxLayout.addWidget(receiveMessage_te);
// QVBoxLayout.addWidget(clrearReceive_btn);
}
MainWindow::~MainWindow()
{ delete ui;
}
//发送数据
void MainWindow::on_sendMessage_btn_clicked()
{
// QString str=ui->sendMessage_te->toPlainText();
// QByteArray senddata;
QByteArray senddata;
QString str =ui->sendMessage_te->toPlainText();
//获取字符串的长度
//isEmpty 空字符串返回真,否则返回假
if(!str.isEmpty()) {
int len=str.length();
//如果发送的数据个数为奇数,则在前面最后落单的字符前添加一个0
if(len %2==1)
{ //str=str.insert(len-1,'0');
}
if(sendHexState==1)
{ StringToHex(str,senddata);//将str转化为16进制的形式
serial.write(senddata);
}
else
{
//发送到串口
serial.write(str.toLatin1());
}
}
//11月8日代码
/*
//16进制发送
if(sendHexState==1)
{
// this->StringToHex(str,senddata);
// serial.write(senddata);
serial.write(ui->sendMessage_te->toPlainText().toLatin1());
}
else
{
serial.write(ui->sendMessage_te->toPlainText().toLatin1());
}
*/
}
//打开串口
void MainWindow::on_openSerial_btn_clicked()
{
// int numRead = 0, numReadTotal = 0;
// char buffer[50];
QByteArray receiveBuffer;
QString receiveStr;
serial.setPortName(this->ui->comSelect_cbb->currentText());
//波特率设置
serial.setBaudRate(this->ui->baudSelect_cbb->currentText().toInt());
//数据位
serial.setDataBits(QSerialPort::Data8);
//奇偶校验
serial.setParity(QSerialPort::NoParity);
//停止位
serial.setStopBits(QSerialPort::OneStop);
//流控
serial.setFlowControl(QSerialPort::NoFlowControl);
//打开串口
serial.open(QIODevice::ReadWrite);
//提示打开串口信息
qDebug()<<"open uart";
//开始时参照了QT帮助手册上的例子
/* while(1) {
//获取串口数据,并计算获取的数据数
numRead = serial.read(buffer, 50);
//计算获取的数据总数
numReadTotal += numRead;
if (numRead == 0 && (!serial.waitForReadyRead()))
break;
}
//输出串口信息
qDebug()<=len)
break;
lstr=str[i].toLatin1();
hexdata=ByteArrayToHexChar(hstr);
lowhexdata=ByteArrayToHexChar(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
senddata[hexdatalen]=(char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
}
unsigned char MainWindow::ByteArrayToHexChar(unsigned char ch)
{
if ((ch >= '0') && (ch <= '9'))
return ch - 0x30;
else if ((ch >= 'A') && (ch <= 'F'))
return ch - 'A' + 10;
else if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
//无符号数返回
else return (-1);
}
void MainWindow::readData()
{
QByteArray buf;
QString temp;
static QString str=temp;
buf = serial.readAll();
if((!buf.isEmpty()))
{
qDebug()<<"输出字节buf:";
qDebug()<receiveMessage_te->setTextColor(QColor(0,255,0)); ui->receiveMessage_te->setFont(QFont("Timers",50,QFont::Bold));
if(receiveHexState==1)
{ qDebug()<<"输出16进制字符串str:";
str+=buf.toHex();
qDebug()<receiveMessage_te->append(str);
}
else {
str+=buf;
qDebug()<<"输出字符串str:";
ui->receiveMessage_te->append(str);
// qDebug()<ui->sendMessage_te->clear();
}
//执行清除接收区数据
void MainWindow::on_clrearReceive_btn_clicked()
{
this->ui->receiveMessage_te->clear();
}
void MainWindow::on_receiveToHex_ceb_stateChanged(int arg1)
{
qDebug()<ui->receiveMessage_te->toPlainText();
//进行文件保存
qDebug() << "进行文件保存";
qDebug() << saveString;
//获取系统当前目录
QString curPath=QDir::currentPath();
qDebug() << curPath;
QString dlgTitle="另存为一个文件"; //对话框标题
QString filter="h文件(*.h);;c++文件(*.cpp);;文本文件(*.txt);;所有文件(*.*)"; //文件过滤器
QString aFileName=QFileDialog::getSaveFileName(this,dlgTitle,curPath,filter);
//输出要保存的文件名
qDebug() << "保存文件为:";
qDebug() << aFileName;
if (aFileName.isEmpty())
return;
QFile aFile(aFileName);
if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))
return ;
QByteArray strBytes=saveString.toUtf8();//转换为字节数组 aFile.write(strBytes,strBytes.length()); //写入文件
aFile.close();
//ui->tabWidget->setCurrentIndex(0);
}
5、mainwindow.ui
MainWindow
0
0
837
685
75
true
通用通讯软件V1.0 如有需要联系[email protected]
20
260
100
50
AcadEref
12
75
true
清空接收
20
370
600
150
20
50
600
200
30
10
121
31
AcadEref
20
75
true
接收数据
20
330
121
31
AcadEref
20
75
true
发送数据
30
530
100
50
AcadEref
12
75
true
清空发送
510
270
111
50
AcadEref
12
75
true
16进制显示
510
530
121
50
AcadEref
12
75
true
16进制发送
660
70
156
136
-
AcadEref
12
串口号:
-
12
-
COM1
-
COM2
-
COM3
-
COM4
-
COM5
-
COM6
-
COM7
-
COM8
-
COM9
-
COM10
-
AcadEref
12
75
true
波特率:
-
12
75
true
-
9600
-
115200
-
19200
-
4800
-
2400
-
1200
-
AcadEref
12
75
true
数据位:
-
12
75
true
-
8
-
7
-
6
-
AcadEref
12
75
true
校验位;
-
12
75
true
-
0
-
AcadEref
12
75
true
停止位:
-
12
75
true
-
1
-
2
650
377
78
124
-
AcadEref
12
75
true
打开串口
-
AcadEref
12
75
true
关闭串口
-
AcadEref
12
75
true
发送数据
-
AcadEref
12
75
true
保存数据
0
0
837
23
TopToolBarArea
false
以上为该程序的所有代码,如有问题,请留言或联系[email protected],后面会持续更新请关注!