#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
#
# Project created by QtCreator 2019-12-23T16:58:18
#
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
QT + = core gui
QT + = serialport
greaterThan ( QT_MAJOR_VERSION, 4 ) : QT + = widgets
TARGET = MyUart
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 \
myuart. cpp
HEADERS + = \
myuart. h
FORMS + = \
myuart. ui
# Default rules for deployment.
qnx: target. path = / tmp/ $${ TARGET} / bin
else : unix: ! android: target. path = / opt/ $${ TARGET} / bin
! isEmpty ( target. path) : INSTALLS + = target
#ifndef MYUART_H
#define MYUART_H
#include <QWidget>
#include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort/QSerialPort>
#include <QComboBox>
#include <QVector>
namespace Ui {
class MyUart ;
}
class MyUart : public QWidget
{
Q_OBJECT
public :
explicit MyUart ( QWidget * parent = nullptr ) ;
~ MyUart ( ) ;
bool openserial ( QComboBox * comboxname, QString comboud) ;
void CloseSerial ( void ) ;
void Seralopen ( void ) ;
void SerialConfig ( void ) ;
void SetWidgetEnable ( bool ) ;
bool SerialIsOpen;
QSerialPort * myserial;
signals:
void serialinfo ( QString tmp) ;
void SendComData ( QString ComData) ;
public slots:
void serialRead ( ) ;
private slots:
void on_DevUartFlash_clicked ( ) ;
void on_DevSerialOpenCloseBtn_clicked ( ) ;
private :
Ui:: MyUart * ui;
} ;
#endif
#include "myuart.h"
#include "ui_myuart.h"
#include <QDebug>
#include <QMessageBox>
QVector< QString> SerialName_Vector3;
MyUart:: MyUart ( QWidget * parent) :
QWidget ( parent) ,
ui ( new Ui:: MyUart)
{
ui- > setupUi ( this ) ;
foreach ( const QSerialPortInfo & info, QSerialPortInfo:: availablePorts ( ) )
{
QSerialPort serial;
serial. setPort ( info) ;
SerialName_Vector3<< info. portName ( ) ;
ui- > DevComPort- > addItem ( info. portName ( ) ) ;
}
ui- > DevComBoud- > setCurrentText ( "38400" ) ;
}
void MyUart:: serialRead ( )
{
QByteArray recvdata = myserial- > readAll ( ) ;
QString RecvData= recvdata. toHex ( ) . data ( ) ;
qDebug ( ) << "收到串口数据:" << RecvData;
uint32_t s32Count = ( uint32_t ) recvdata. count ( ) ;
uint8_t * pu8Data = ( uint8_t * ) recvdata. data ( ) ;
emit SendComData ( RecvData) ;
}
bool MyUart:: openserial ( QComboBox * comboxname, QString comboud)
{
QString comname= comboxname- > currentText ( ) ;
qDebug ( ) << "正在打开串口:%s" << comname<< endl;
myserial = new QSerialPort ( comname) ;
bool res= myserial- > open ( QIODevice:: ReadWrite) ;
if ( res!= true )
{
qDebug ( ) << "打开串口失败:" ;
QMessageBox box ( QMessageBox:: Warning, "提示" , " 串口打开失败\r\n请检查是否已选择端口!" ) ;
box. setStandardButtons ( QMessageBox:: Ok) ;
box. setButtonText ( QMessageBox:: Ok, QString ( "确 定" ) ) ;
box. exec ( ) ;
return false ;
}
qDebug ( ) << "打开串口:%s" << comname<< endl;
if ( comboud== "1200" )
{
myserial- > setBaudRate ( QSerialPort:: Baud1200) ;
}
else if ( comboud== "2400" )
{
myserial- > setBaudRate ( QSerialPort:: Baud2400) ;
}
else if ( comboud== "4800" )
{
myserial- > setBaudRate ( QSerialPort:: Baud4800) ;
}
else if ( comboud== "9600" )
{
myserial- > setBaudRate ( QSerialPort:: Baud9600) ;
}
else if ( comboud== "19200" )
{
myserial- > setBaudRate ( QSerialPort:: Baud19200) ;
}
else if ( comboud== "38400" )
{
myserial- > setBaudRate ( QSerialPort:: Baud38400) ;
}
else if ( comboud== "57600" )
{
myserial- > setBaudRate ( QSerialPort:: Baud57600) ;
}
else if ( comboud== "11520000" )
{
myserial- > setBaudRate ( QSerialPort:: Baud115200) ;
}
else
{
myserial- > setBaudRate ( QSerialPort:: Baud38400) ;
comboud= "38400" ;
qWarning ( "波特率不存在,启用默认波特率38400" ) ;
}
qDebug ( ) << "当前波特率:" << comboud;
myserial- > setDataBits ( QSerialPort:: Data8) ;
myserial- > setParity ( QSerialPort:: NoParity) ;
myserial- > setStopBits ( QSerialPort:: OneStop) ;
myserial- > setFlowControl ( QSerialPort:: NoFlowControl) ;
connect ( myserial, SIGNAL ( readyRead ( ) ) , this , SLOT ( serialRead ( ) ) ) ;
ui- > DevSerialOpenCloseBtn- > setText ( "关闭\n串口" ) ;
return true ;
}
void MyUart:: SerialConfig ( void )
{
if ( ui- > DevSerialOpenCloseBtn- > text ( ) == "打开\n串口" )
{
bool openres= this - > openserial ( ui- > DevComPort, ui- > DevComBoud- > currentText ( ) ) ;
if ( openres== false )
{
qDebug ( ) << "open serial 1 error!" ;
return ;
}
qDebug ( ) << "open serial %s" << ui- > DevComPort- > currentText ( ) << endl;
ui- > DevSerialOpenCloseBtn- > setText ( "关闭\n串口" ) ;
this - > SerialIsOpen= 1 ;
}
}
void MyUart:: CloseSerial ( )
{
if ( myserial- > isOpen ( ) )
{
myserial- > close ( ) ;
}
ui- > DevSerialOpenCloseBtn- > setText ( "打开\n串口" ) ;
this - > SerialIsOpen= 0 ;
}
void MyUart:: SetWidgetEnable ( bool en)
{
ui- > DevComBoud- > setEnabled ( en) ;
ui- > DevComPort- > setEnabled ( en) ;
ui- > DevUartFlash- > setEnabled ( en) ;
ui- > DevSerialOpenCloseBtn- > setEnabled ( en) ;
}
void MyUart:: on_DevUartFlash_clicked ( )
{
SerialName_Vector3. clear ( ) ;
ui- > DevComPort- > clear ( ) ;
foreach ( const QSerialPortInfo & info, QSerialPortInfo:: availablePorts ( ) )
{
QSerialPort serial;
serial. setPort ( info) ;
SerialName_Vector3<< info. portName ( ) ;
ui- > DevComPort- > addItem ( info. portName ( ) ) ;
}
ui- > DevComBoud- > setCurrentText ( "38400" ) ;
}
void MyUart:: on_DevSerialOpenCloseBtn_clicked ( )
{
if ( ui- > DevSerialOpenCloseBtn- > text ( ) == "打开\n串口" )
{
openserial ( ui- > DevComPort, ui- > DevComBoud- > currentText ( ) ) ;
}
else
{
CloseSerial ( ) ;
}
}
MyUart:: ~ MyUart ( )
{
delete ui;
}
#include "myuart.h"
#include <QApplication>
int main ( int argc, char * argv[ ] )
{
QApplication a ( argc, argv) ;
MyUart w;
w. show ( ) ;
return a. exec ( ) ;
}