串口源码(可裁剪)
源码 串口 裁剪
2023-09-14 09:07:11 时间
最近在做一个项目需要使用到串口通信,随后自己便编写了一段串口程序。
界面如下图:
源码:
serial.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-08-24T16:40:35
#
#-------------------------------------------------
QT += core gui widgets
QT += serialport
TARGET = serial
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
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
resource.qrc
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QProcess>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
int e = a.exec();
if (e == 777)
{
QProcess::startDetached(qApp->applicationFilePath(),QStringList());
return 0;
}
return e;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_OpenSerialButton_clicked();
void ReadData();
void on_SendButton_clicked();
void on_clearButton_clicked();
void on_clearInputButton_clicked();
void on_quitButton_clicked();
private:
QSerialPort *serial;
QByteArray QString2Hex(QString str);
char ConvertHexChar(char ch);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
this->setWindowFlags(Qt::Dialog);
ui->setupUi(this);
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
QSerialPort serial;
serial.setPort(info);
if (serial.open(QIODevice::ReadWrite))
{
ui->PortBox->addItem(serial.portName());
serial.close();
}
}
ui->BaudBox->setCurrentIndex(1);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_quitButton_clicked()
{
qApp->exit(777);
}
/*
*
* 打开串口槽函数
*
*/
void MainWindow::on_OpenSerialButton_clicked()
{
if (ui->OpenSerialButton->text() == tr("打开串口"))
{
serial = new QSerialPort;
serial->setPortName(ui->PortBox->currentText());
serial->open(QIODevice::ReadWrite);
if (ui->BaudBox->currentIndex() == 0)
serial->setBaudRate(QSerialPort::Baud1200);
else if (ui->BaudBox->currentIndex() == 1)
serial->setBaudRate(QSerialPort::Baud2400);
else if (ui->BaudBox->currentIndex() == 2)
serial->setBaudRate(QSerialPort::Baud4800);
else if (ui->BaudBox->currentIndex() == 3)
serial->setBaudRate(QSerialPort::Baud9600);
else if (ui->BaudBox->currentIndex() == 4)
serial->setBaudRate(QSerialPort::Baud19200);
else if (ui->BaudBox->currentIndex() == 5)
serial->setBaudRate(QSerialPort::Baud38400);
else if (ui->BaudBox->currentIndex() == 6)
serial->setBaudRate(QSerialPort::Baud57600);
else if (ui->BaudBox->currentIndex() == 7)
serial->setBaudRate(QSerialPort::Baud115200);
switch (ui->BitBox->currentIndex())
{
case 8:
serial->setDataBits(QSerialPort::Data8);
break;
default:
break;
}
switch (ui->ParityBox->currentIndex())
{
case 0:
serial->setParity(QSerialPort::NoParity);
break;
default:
break;
}
switch (ui->BitBox->currentIndex())
{
case 1:
serial->setStopBits(QSerialPort::OneStop);
break;
case 2:
serial->setStopBits(QSerialPort::TwoStop);
break;
default:
break;
}
serial->setFlowControl(QSerialPort::NoFlowControl);
ui->PortBox->setEnabled(false);
ui->BaudBox->setEnabled(false);
ui->BitBox->setEnabled(false);
ui->StopBit->setEnabled(false);
ui->ParityBox->setEnabled(false);
ui->OpenSerialButton->setText(tr("关闭串口"));
QObject::connect(serial, &QSerialPort::readyRead, this, &MainWindow::ReadData);
}
else
{
serial->clear();
serial->close();
serial->deleteLater();
ui->PortBox->setEnabled(true);
ui->BaudBox->setEnabled(true);
ui->BitBox->setEnabled(true);
ui->StopBit->setEnabled(true);
ui->ParityBox->setEnabled(true);
ui->OpenSerialButton->setText(tr("打开串口"));
}
}
/*
*
* 串口读数据槽函数
*
*/
void MainWindow::ReadData()
{
int bytelen = serial->bytesAvailable();
if (bytelen <= 0)
return;
qDebug("byteLen=%d\n", bytelen);
QByteArray buf = serial->readAll();
if (this->ui->checkBox_2->isChecked())
{
QString temp = buf.toHex().data();
QString temp1;
for (int i=0; i<temp.length()/2; i++)
{
char s = temp.toLatin1().at(i);
if ( s >= 'a' && s <= 'f')
{
s -= 32;
temp1 += s+temp.mid(i*2+1, 1)+" ";
}
else
temp1 += temp.mid(i*2,2)+" ";
}
ui->textEdit->append(temp1);
}
else
ui->textEdit->append(buf);
}
/*
*
* 清空接收区
*
*/
void MainWindow::on_clearButton_clicked()
{
ui->textEdit->clear();
}
/*
*
* 清空输入区
*
*/
void MainWindow::on_clearInputButton_clicked()
{
ui->textEdit_2->clear();
}
/*
*
* 发送按钮槽函数
*
*/
void MainWindow::on_SendButton_clicked()
{
QString str = ui->textEdit_2->toPlainText();
qDebug(str.toLatin1());
QByteArray bytearray;
if (ui->checkBox->isChecked() == true)
{
bytearray = QString2Hex(str);
serial->write(bytearray);
}
else
serial->write(str.toLatin1());
qDebug(str.toLatin1());
}
/*
*
* 将字符串转为16进制
*
*/
QByteArray MainWindow::QString2Hex(QString str)
{
QByteArray senddata;
qDebug("QString2Hex\n");
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
senddata.resize(len/2);
char lstr, hstr;
for (int i=0; i<len;)
{
hstr = str[i].toLatin1();
if (hstr == ' ')
{
i++;
continue;
}
i++;
if (i >= len)
break;
lstr = str[i].toLatin1();
if (hstr >= 'a' && hstr <= 'f')
hstr -= 32;
if (lstr >= 'a' && lstr <= 'f')
lstr -= 32;
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
if ((hexdata == 16) || lowhexdata == 16)
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
return senddata;
}
/*
*
* 将单个字符转换成16进制
*
*/
char MainWindow::ConvertHexChar(char ch)
{
qDebug("ConvertHexChar\n");
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;
}
相关文章
- ABP源码分析三十三:ABP.Web
- 从github上pull源码
- Hadoop2.x源码-编译剖析
- 源码系列--OkHttp(2)
- 记一次从源码泄露到getshell(二)
- MFC Windows 程序设计[276]之文件枚举例程(附源码)
- MFC Windows 程序设计[189]之滑动窗口页签(附源码)
- ZZNUOJ_用C语言编写程序实现1596:素数2(附完整源码)
- 【Android 异步操作】AsyncTask 异步任务 ( AsyncTask 异步任务执行方法 execute 方法相关源码解析 )
- Go语言精进自学系列 | 提交前使用gofmt格式化源码
- 使用TensorFlow构建面部口罩识别系统【本文源码开箱即用】
- 第二人生的源码分析(二十五)人物行走与服务器同步
- JDK源码详解之List接口
- rk3399 源码下查找交叉编译工具链
- Python基于RCNN&CTC的视频字幕识别系统(源码&部署教程)