代码如下:
#ifndef MAINETHREAD_H
#define MAINETHREAD_H
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QDebug>
class mainEthread : public QThread
{
Q_OBJECT
public:
mainEthread();
~mainEthread();
void run();
void transaction(const QString &request);
void reaseive1(const QString &baudrate, const QString &checkCode,const QString &checklen);
void openserial();
void check(const QString &s);
bool CRC(const QString &s1,const QString &s2);
unsigned char crc8(unsigned char *ptr,unsigned int len, unsigned char check_code);
unsigned int crc16(unsigned char *ptr,unsigned int len, unsigned int check_code);
unsigned long int crc32(unsigned char *ptr,unsigned int len, unsigned long int check_code);
bool ASCIItoCHAR(QByteArray GET_DATA1,unsigned int GET_DATA_len);
unsigned char ASCIItoCHAR8(QByteArray GET_DATA1, unsigned int GET_DATA_len);
unsigned int ASCIItoCHAR16(QByteArray GET_DATA1, unsigned int GET_DATA_len);
unsigned long int ASCIItoCHAR32(QByteArray GET_DATA1, unsigned int GET_DATA_len);
void CHECK_SET();
signals:
void response();
void responsedata(const QString &s1, const QString &s2);
void responserror();
private slots:
void stopserial();
private:
QString request;
QString baudrate;
QString checkCode;
QString checklen;
int DATA_LEN;
QMutex mutex;
QWaitCondition cond;
bool quit;
bool ok;
int long1;
QString response2;
unsigned char char_8;
unsigned int int_16;
unsigned long int long_32;
};
#endif // MAINETHREAD_H
#ifndef MAINWINDOWS_H
#define MAINWINDOWS_H
#include <QtWidgets/QWidget>
#include <QtWidgets/QTextBrowser>
#include <QtWidgets/QLCDNumber>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QGridLayout>
#include <mainethread.h>
#include <QTimer>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QRadioButton>
class MainWindows : public QWidget
{
Q_OBJECT
public:
MainWindows();
~MainWindows();
void setbaudrate();
void Edit_SET();
void CHECKCODE_CHOSE();
private:
QLabel *label1;
QLabel *label2;
QLabel *label4;
QLabel *label5;
QLabel *label6;
QLabel *label7;
QLabel *label8;
QLabel *label9;
QLabel *label10;
QLabel *label11;
QLabel *CHECK_8;
QLabel *CHECK_16;
QLabel *CHECK_32;
QLabel *DATA_LENGTH;
QLineEdit *edit1;
QLineEdit *edit_8;
QLineEdit *edit_16;
QLineEdit *edit_32;
QLineEdit *edit_length;
QTextBrowser *browser1;
QTextBrowser *browser4;
QTextBrowser *browser2;
QTextBrowser *browser3;
QLCDNumber *lcd1;
QPushButton *button1;
QPushButton *button2;
QPushButton *button3;
QPushButton *button4;
QRadioButton *CHECK8;
QRadioButton *CHECK16;
QRadioButton *CHECK32;
mainEthread *thread;
QComboBox *baudrateCombox;
QGridLayout *layout1;
long int num1;
long int num2;
private slots:
void transaction();
void send1();
void showResponse();
void showdata(const QString &s1, const QString &s2);
void clearwindows();
void showerror();
};
#endif // MAINWINDOWS_H
#include <mainwindows.h>
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc,argv);
MainWindows w;
w.show();
return a.exec();
}
#include "mainethread.h"
#include <QtSerialPort/QSerialPort>
mainEthread::mainEthread()
{
long1 = 0;
quit=false;
}
void mainEthread::transaction(const QString &request)
{
quit=false;
QMutexLocker locker(&mutex);
this->request = request;
if (!isRunning())
start();
else
cond.wakeOne();
}
//接收波特率和校验码
void mainEthread::reaseive1(const QString &baudrate, const QString &checkCode,const QString &checklen)
{
QMutexLocker locker(&mutex);
this->baudrate = baudrate;
this->checkCode = checkCode;
this->checklen = checklen;
DATA_LEN = this->checklen.toInt(&ok