#ifndef MYSERIALPORT_H
#define MYSERIALPORT_H
#include <QObject>
#include <QString>
#include <QSerialPort>
#include <QThread>
#include <QMessageBox>
#include <QtSerialPort/QSerialPort> //提供访问串行端口的功能
#include <QtSerialPort/QSerialPortInfo>
class myserialport : public QObject
{
Q_OBJECT
public:
explicit myserialport(QByteArray stm);
~myserialport();
QByteArray Receivebuff;
QByteArray Sendbuff;
bool connectStaion ;
signals:
void Receive_Data(QByteArray tmp);
void SerialInitInf(QByteArray stm);
void SerialSignal(QByteArray data);
public slots:
void SerialPorat_Init(QString Baudrate);
void SendData(QByteArray data);
void ReceiveData(void);
void Serial_Init(QByteArray tmp);
private:
QSerialPort * serial;
QThread * My_thread;
};
#endif // MYSERIALPORT_H
#include "myserialport.h"
#include <QMessageBox>
#include <QSerialPort>
#include <QDebug>
myserialport::myserialport(QByteArray stm)
{
connect(this,&myserialport::SerialInitInf,this,[=](QByteArray stm){
this->Serial_Init(stm);
});
emit SerialInitInf(stm);
}
myserialport::~myserialport()
{
serial->clear();
serial->close();
serial->deleteLater();
My_thread->quit();
My_thread->wait();
// My_thread->deleteLater();
// qDebug()<<"析构函数执行:关闭串口 删除子线程";
}
void myserialport::SerialPorat_Init(QString name)
{
serial->setPortName(name);
// qDebug()<<"name"<<name;
serial->setBaudRate(256000);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
serial->setParity(QSerialPort::NoParity);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setReadBufferSize(0);
bool satte = serial->open(QIODevice::ReadWrite);
if(!satte)
{
QMessageBox::critical(NULL,"提示","无法打开串口,请检查是否被占用。",QMessageBox::Yes);
connectStaion = false;
return;
}
else
{
QMessageBox::information(NULL,"提示","串口已经连接",QMessageBox::Yes);
connectStaion = true;
}
connect(serial,&QSerialPort::readyRead,this,&myserialport::ReceiveData,Qt::QueuedConnection);
}
void myserialport::SendData(QByteArray data)
{
QString command = QString(data);
QStringList commadList = command.split(' ');
QByteArray datas;
datas.resize(commadList.count());//初始化新数组
bool ok = false;
for(int i = 0; i < commadList.count(); i++)
{
datas[i]=static_cast<char>(commadList.at(i).toInt(&ok,16));
}
datas.toUpper();
serial->write(datas);
// qDebug()<<"串口发送 "<<datas;
// qDebug()<<"串口发送线程ID为:"<<QThread::currentThread();
}
void myserialport::ReceiveData()
{
qint64 bytelen = serial->bytesAvailable();
if(bytelen<1)
{
qDebug()<<"串口无数据接收";
return;
}
Receivebuff = serial->readAll();
qDebug()<<"串口接收"<<Receivebuff;
// qDebug()<<"串口接收线程ID为:"<<QThread::currentThread();
emit Receive_Data(Receivebuff);
}
void myserialport::Serial_Init(QByteArray tmp)
{
QString stmm = tmp;
My_thread = new QThread();
serial = new QSerialPort();
this->moveToThread(My_thread);
serial->moveToThread(My_thread);
My_thread->start();
SerialPorat_Init(stmm);
// qDebug()<<"构造串口线程ID为:"<<QThread::currentThread();
}
主程序调用
void MainWindow::on_strat_clicked()//串口确认
{
name = (ui->cominfobox->currentText()).toUtf8();
Myserial = new SerialportThread(name,this);
Myserial->start();
connect(this,&MainWindow::SendserialportData,Myserial,[=](QByteArray data){
//Myserial->SendData(data);
QMetaObject::invokeMethod(Myserial, "SendData",
Qt::QueuedConnection,Q_ARG(QByteArray,data));//跨线程调用
});
connect(Myserial,&SerialportThread::ReceiveData,this,[=](QByteArray data){
MyserialData.append(data);
},Qt::QueuedConnection);
}