#include "Radar.h"
#include "ui_Radar.h"
Radar::Radar(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Radar)
{
ui->setupUi(this);
try {
m_radar = new Radar_RW();
m_radar->moveToThread(new QThread());
} catch (std::bad_alloc &e) {
qDebug()<<"omg!!";
}
QObject::connect(m_radar,&Radar_RW::dataReceived,this,&Radar::ondataReceived);
m_radar->start();
try {
QObject::connect(ui->Come_Tiltration,SIGNAL(clicked()),m_radar,SLOT(ComeTiltration_setting()),Qt::QueuedConnection);
} catch (std::exception &e) {
qDebug()<<"Oh,shit!";
}
Dialog_Init();
}
Radar::~Radar()
{
m_radar->quit();
delete ui;
}
#include "Radar_RW.h"
Radar_RW::Radar_RW(QObject *parent):
QThread(parent),
m_ReceiveThreadFlag(true)
{
m_serialPort_Infos = QSerialPortInfo::availablePorts();
qDebug() << "Available serial ports:";
for (const QSerialPortInfo &serialPortInfo : m_serialPort_Infos) {
qDebug() <<serialPortInfo.portName();
}
// 打开串口
m_serialPort.setPortName(m_serialPort_Infos.first().portName()); // 设置串口名
m_serialPort.setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections); // 设置波特率
m_serialPort.setDataBits(QSerialPort::Data8); // 设置数据位
m_serialPort.setParity(QSerialPort::NoParity); // 设置校验位
m_serialPort.setStopBits(QSerialPort::OneStop); // 设置停止位
if (!m_serialPort.open(QIODevice::ReadWrite)) {
qDebug() << "Failed to open serial port!"<<m_serialPort.errorString();
}
else{
qDebug()<<"success!";
}
}
void Radar_RW::run()
{
while (m_ReceiveThreadFlag){
try {
// 等待接收数据
m_serialPort.waitForReadyRead(1000);
// 读取数据
QByteArray responseData = m_serialPort.readAll();
m_data=responseData.toHex();
if(!m_data.isEmpty())
{
qDebug()<<m_data;
}
emit dataReceived(m_data);
} catch (std::exception &e) {
qDebug()<<"sorry!";
}
}
}
void Radar_RW::Receive()
{
// 等待接收数据
QString Data;
QByteArray responseData;
while (true) {
if (m_serialPort.waitForReadyRead(1000)) {
responseData = m_serialPort.readAll();
QString hexString = QString(responseData.toHex());
qDebug()<<hexString;
if (hexString.startsWith("fa323030fb")) {
QString subString = hexString.mid(0, 10);
Data.append(subString);
}
if (Data == "fa323030fb") {
qDebug() << "SET_Success";
break;
}
}
}
Data.truncate(0);
}
void Radar_RW::Enter_setting()
{
m_ReceiveThreadFlag=false;
// 发送控制指令
QByteArray sendData;
sendData.append(0xFA);
sendData.append(0x31);
sendData.append(0x30);
sendData.append(0x30);
sendData.append(0xFB);
qint64 bytesWritten = m_serialPort.write(sendData);
if (bytesWritten == -1) {
qDebug() << "Failed to send control command!";
m_serialPort.close();
}
}
void Radar_RW::Exit_setting()
{
// 发送控制指令
QByteArray sendData;
sendData.append(0xFA);
sendData.append(0x32);
sendData.append(0x30);
sendData.append(0x30);
sendData.append(0xFB);
qint64 bytesWritten = m_serialPort.write(sendData);
if (bytesWritten == -1) {
qDebug() << "Failed to send control command!";
m_serialPort.close();
}
m_ReceiveThreadFlag=true;
}
void Radar_RW::ComeTiltration_setting()
{
Enter_setting();
// 发送控制指令1 触头
QByteArray sendData1;
sendData1.append(0xFA);
sendData1.append(0x33);
sendData1.append(0x31);
sendData1.append(0x30);
sendData1.append(0xFB);
qint64 bytesWritten1 = m_serialPort.write(sendData1);
if (bytesWritten1 == -1) {
qDebug() << "Failed to send control command!";
m_serialPort.close();
}
// 发送控制指令2 来向
QByteArray sendData;
sendData.append(0xFA);
sendData.append(0x37);
sendData.append(0x31);
sendData.append(0x30);
sendData.append(0xFB);
qint64 bytesWritten2 = m_serialPort.write(sendData);
if (bytesWritten2 == -1) {
qDebug() << "Failed to send control command!";
m_serialPort.close();
}
Receive();
Exit_setting();
}
其中的m_radar是子线程,控件是线程的第二个connect函数使用了QueuedConnection处理模式,在主线程闲置的时候为什么没有执行槽函数
希望大家可以帮忙看一下