文章目录 ui设置 创建线程函数 初始串口 run函数接收发送数据 主线程
ui设置
创建线程函数
# include <QObject>
# include <QThread>
# include <QSerialPort>
# include <QSerialPortInfo> class SerialPort : public QThread
{ Q_OBJECT
public : SerialPort ( ) ; ~ SerialPort ( ) ; public : void InitPort ( QString PortName) ; void wirteData ( ) ; void ProcessReceivedData ( QByteArray) ; quint8 CalculateChecksum ( const QByteArray& data) ;
signals: void SendData ( float m_fVol[ 10 ] ) ; protected : void run ( ) ; public slots: void readData ( ) ; void handleBytesWritten ( quint64 bytes) ; public : QSerialPort* port; QStringList serialPortNameList; float m_fVol[ 10 ] ; QByteArray m_receivedBuffer[ 4096 ] ; QByteArray receivedData;
} ;
初始串口
void SerialPort :: InitPort ( QString PortName)
{
port-> setPortName ( PortName) ; port-> setBaudRate ( 19200 , QSerialPort:: AllDirections) ; port-> setDataBits ( QSerialPort:: Data8) ; port-> setFlowControl ( QSerialPort:: NoFlowControl) ; port-> setParity ( QSerialPort:: NoParity) ; port-> setStopBits ( QSerialPort:: OneStop) ; if ( port-> open ( QIODevice:: ReadWrite) ) { qDebug ( ) << "Port have been opened" ; wirteData ( ) ; } else { qDebug ( ) << "open it failed" ; return ; }
}
run函数接收发送数据
void SerialPort :: run ( )
{ connect ( port, & QSerialPort:: bytesWritten, this , & SerialPort:: handleBytesWritten) ; connect ( port, & QSerialPort:: readyRead, this , & SerialPort:: readData) ; exec ( ) ;
}
读取数据
void SerialPort :: readData ( )
{ if ( port-> bytesAvailable ( ) > 0 ) { qint64 nReadSize = 4096 ; if ( port-> isOpen ( ) ) { QByteArray receivedData = port-> read ( nReadSize) ; if ( receivedData. size ( ) >= 26 ) { ProcessReceivedData ( receivedData) ; } } }
}
处理读取的数据
void SerialPort :: ProcessReceivedData ( QByteArray receivedData)
{ if ( receivedData. size ( ) >= 26 ) { if ( receivedData. at ( 0 ) == 'U' && receivedData. at ( receivedData. length ( ) - 1 ) == '\xAA' ) { QByteArray dataContent = receivedData. mid ( 1 , 23 ) ; quint8 receivedChecksum = static_cast < quint8> ( receivedData. at ( 24 ) ) ; quint8 calculatedChecksum = CalculateChecksum ( dataContent) ; if ( receivedChecksum == calculatedChecksum ) { quint16 wT; for ( int j = 0 ; j < 10 ; ++ j) { wT = static_cast < quint8> ( receivedData[ j* 2 + 2 ] ) ; wT <<= 8 ; wT += static_cast < quint8> ( receivedData[ j* 2 + 1 ] ) ; m_fVol[ j] = wT * 5000.0f / 1024 ; emit SendData ( m_fVol) ; } } } else { receivedData. remove ( 0 , 1 ) ; } } } quint8 SerialPort :: CalculateChecksum ( const QByteArray & data)
{ quint8 checksum = 0 ; for ( int i = 0 ; i < data. size ( ) ; ++ i) { checksum += static_cast < quint8> ( data. at ( i) ) ; } return checksum;
}
写入数据
void SerialPort :: handleBytesWritten ( quint64 bytes)
{ if ( bytes > 0 ) { connect ( port, & QSerialPort:: readyRead, this , & SerialPort:: readData) ; } else { wirteData ( ) ; }
}
写入启动的命令
void SerialPort :: wirteData ( )
{ if ( port-> isOpen ( ) ) { QByteArray data; data. append ( char ( 0x55 ) ) ; data. append ( char ( 0x01 ) ) ; data. append ( char ( 0x01 ) ) ; data. append ( char ( 0x00 ) ) ; data. append ( char ( 0x00 ) ) ; data. append ( char ( 0x00 ) ) ; data. append ( char ( 0x02 ) ) ; data. append ( char ( 0xAA ) ) ; port-> clear ( QSerialPort:: Output) ; port-> write ( data) ; }
}
主线程
# include <QWidget>
# include "serialport.h" QT_BEGIN_NAMESPACE
namespace Ui { class Widget ; }
QT_END_NAMESPACEclass Widget : public QWidget
{ Q_OBJECTpublic : Widget ( QWidget * parent = nullptr ) ; ~ Widget ( ) ; private : Ui:: Widget * ui; public slots: void ReadData ( float m_fVol[ 10 ] ) ; public : SerialPort* m_Port; private slots: void on_pushButton_clicked ( ) ;
} ;
Widget :: Widget ( QWidget * parent) : QWidget ( parent) , ui ( new Ui:: Widget)
{ ui-> setupUi ( this ) ; m_Port = new SerialPort ( ) ; ui-> comboBox-> addItem ( "COM1" ) ; ui-> comboBox-> addItem ( "COM2" ) ; ui-> comboBox-> addItem ( "COM3" ) ; ui-> comboBox-> addItem ( "COM4" ) ; ui-> comboBox-> addItem ( "COM5" ) ; ui-> comboBox-> addItem ( "COM6" ) ; ui-> comboBox-> addItem ( "COM7" ) ; ui-> comboBox-> addItem ( "COM8" ) ; } Widget :: ~ Widget ( )
{ delete ui;
} void Widget :: ReadData ( float m_fVol[ 10 ] )
{ QTextCursor tc = ui-> textBrowser-> textCursor ( ) ; tc. movePosition ( QTextCursor:: End) ; for ( int i= 0 ; i< 10 ; i++ ) { tc. insertText ( QString :: number ( m_fVol[ i] ) + "mv\n" ) ; tc. insertText ( "\n" ) ; } tc. insertText ( "----------\n" ) ; ui-> textBrowser-> moveCursor ( QTextCursor:: End) ;
} void Widget :: on_pushButton_clicked ( )
{ if ( ui-> pushButton-> text ( ) == "连接" ) { m_Port-> InitPort ( ui-> comboBox-> currentText ( ) ) ; connect ( m_Port, & SerialPort:: SendData, this , & Widget:: ReadData) ; m_Port-> start ( ) ; ui-> pushButton-> setText ( "断开连接" ) ; } else { m_Port-> port-> close ( ) ; disconnect ( m_Port, & SerialPort:: SendData, this , & Widget:: ReadData) ; ui-> pushButton-> setText ( "连接" ) ; } }