该项目以前发过一篇博客:https://blog.csdn.net/xx970829/article/details/111997428
在原来的基础上新添加了一些功能,并且打包好,不仅能跨平台运行,还能在其他win10系统下的vs中编译(亲测)
在vs中配置qt插件,然后就可以运行了,中途可能报vs版本的问题,很好解决,直接百度,若报qt问题,尝试将qt_bin目录里的文件复制到主程序文件夹,即qt_bin所在文件夹,里面的控制界面exe可直接运行,快捷方式可以直接拖拽到桌面运行。。。
环境搭建
【直接下载完整项目的话就不需要搭建环境了,只需在vs中安装qt插件】
win10中vs+qt+qgis环境搭建
环境搭建见我另一篇博客:(很详细,免编译)https://blog.csdn.net/xx970829/article/details/109890931
环境搭建常见问题及解决方案:
https://blog.csdn.net/xx970829/article/details/109891607
源代码
main.cpp
#include "QtWidgetsApplicationxx.h"
#include <QtWidgets/QApplication>
///
#include<qgsapplication.h>
#include<qgsvectorlayer.h>
#include<iostream>
#include<qapplication.h>
#include <windows.h>
#include<qgsmapcanvas.h>
#include <qgsproviderregistry.h>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//
QgsApplication b(argc, argv, true);
QgsApplication::setPrefixPath("./qgis", true);//QGIS路径 F://OSGeo4W64/apps/
QgsApplication::initQgis();//初始化QGIS应用
///
QtWidgetsApplicationxx w;
w.show();
return a.exec();
}
QtWidgetsApplicationxx.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplicationxx.h"
#include "WinsockMatTransmission.h"
#include<string>
#include<iostream>
#include <iomanip>
#include <windows.h>
#include<qapplication.h>
#include <io.h>
#include <fstream>
#include<stdio.h>
//qt
#include <qmessagebox.h>
#include <QString>
#include <QFileDialog>
#include <QWidget>
#include <QSerialPort>
#include<QtConcurrent>
#include<QHBoxLayout>
#include<QDebug>
#include <QByteArray>
#include <QApplication>
#include <QSerialPortInfo>
#include <QComboBox>
#include <QPushButton>
#include <QMainWindow>
#include <QtSvg/QGraphicsSvgItem>//QtSvg/QGraphicsSvgItem QGraphicsSvgItem
#include<QKeyEvent>
#include<QtWidgets/QApplication>
#include <QStringList>
#include<QMessageBox>
#include<QObject>
#include <QList>
#include <QFileInfoList>
#include <QDir>
#include <QLibrary>
#include<QTabWidget>
#include<QMutex>
//gis
#include <qgssinglesymbolrenderer.h>
#include <qgsvectordataprovider.h>
#include <qgsrasterlayer.h>
#include<qgsCoordinatetransform.h>
#include<qgsmaplayerref.h>
#include<qgsmaplayerrenderer.h>
#include<qgsauthimportcertdialog.h>
#include<qgsapplication.h>
#include<qgsvectorlayer.h>
#include<qgsmapcanvas.h>
#include<qgsmaplayerstore.h>//qgsmaplayerstore.h qgsmaplayerregistry.h
#include <qgsproviderregistry.h>
#include <qgssymbollayer.h>
#include <qgssymbol.h>
#include <qgsmarkersymbollayer.h>
#include <qgsvectorlayerrenderer.h>
#include <qgsrendercontext.h>
class QtWidgetsApplicationxx : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplicationxx(QWidget *parent = Q_NULLPTR);
~QtWidgetsApplicationxx();
//QtWidgetsClass map;//创建一个窗体,类似于Qt
QStringList getPortNameList();//获取可用的串口列表
void openPort();//打开串口
bool start = true;
//void listenthread();//读取数据
void countxy(double r[8], double *xx, double *yy);
void socketcountxy(double r[10], double *xx, double *yy);
int power(int x, int n);
void readline(const char* strPath, int &line);
void newfile(char filename[200]);
void pushnum(const char* strPath);
//----------------------------------------------------------------------------------------------
void AddLine();
void openMap(QgsMapCanvas *mapCanvas, QgsVectorLayer* newLayer, QgsVectorLayer* newLayer1, QgsVectorLayer* newLayer2, QgsVectorLayer* newLayer3);
QgsVectorLayer* drawfire(QgsVectorLayer* newLayer, double x, double y);
QgsVectorLayer* drawfireline(QgsVectorLayer* newLayer, QString strPath, double x, double y);
QgsVectorLayer* drawplane(QgsVectorLayer* newLayer, double x, double y);
QgsVectorLayer* createLayer(QgsVectorLayer* newLayer);
//QgsVectorLayer* createLayer();QgsRasterLayerQMessageBoxQgsProject
//----------------------------------------------------------------------------------------------
public slots:
void receiveInfo();
protected:
//键盘事件
void keyPressEvent(QKeyEvent *event);
private slots:
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
void on_pushButton_10_clicked();
void on_pushButton_11_clicked();
void on_pushButton_12_clicked();
void on_pushButton_13_clicked();
void on_pushButton_14_clicked();
void on_pushButton_15_clicked();
void on_pushButton_16_clicked();
void on_pushButton_17_clicked();
void on_pushButton_18_clicked();
void on_pushButton_19_clicked();
void on_pushButton_20_clicked();
void on_action_triggered();
void on_action_1_triggered();
void on_action_2_triggered();
void on_action_3_triggered();
private:
Ui::QtWidgetsApplicationxxClass ui;
QSerialPort* m_serialPort; //串口类
QStringList m_portNameList;
QgsVectorLayer *newLayer;
QgsVectorLayer *newLayer1;
QgsVectorLayer *newLayer2;
QgsVectorLayer *newLayer3;
QgsMapCanvas *mapCanvas;
QVBoxLayout *mpMapLayout;
WinsockMatTransmission *socketMat;
};
QtWidgetsApplicationxx.cpp
#include "QtWidgetsApplicationxx.h"
#include<QWidget>
//-----------------------------------------------------------------------
using namespace std;
double fire[32];
double file[2];
char filename[200];//数据存储文件名
FILE *fp;//保存数据
QString firecolor;
int thread_ = 0, thread_4 = 0, thread_5= 0, thread_6= 0, thread_7= 0, m_=0;//控制线程
int k = 0, kk = 0, k_ = 0, kk_ = 0;//控制检测火qDebug
int lei = 0, guan = 0, xiang = 0, ros = 0, bao = 0, m = 0, por = 0;//控制设备
int lei_ = 2, guan_ = 2, xiang_ = 2, bao_ = 2,Reset = 2;
int comp = 0, reboot = 0;//控制工控机
int page = 0;//控制页
QFuture<void> thread;//线程
QFuture<void> thread1;
QFuture<void> thread2;
QFuture<void> thread3;
QFuture<void> thread4;
QFuture<void> thread5;
QFuture<void> thread6;
QFuture<void> thread7;
vector<double> V;
QList<QgsMapLayer*>layerlist;
QtWidgetsApplicationxx::QtWidgetsApplicationxx(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
this->setWindowTitle("serialport");
firecolor = "./fire.svg";
m_serialPort = new QSerialPort();
m_portNameList = getPortNameList();
ui.comboBox->addItems(m_portNameList);
newLayer = new QgsVectorLayer();
newLayer1 = new QgsVectorLayer();
newLayer2 = new QgsVectorLayer();
newLayer3 = new QgsVectorLayer();
mapCanvas = new QgsMapCanvas();
socketMat = new WinsockMatTransmission();
newLayer = createLayer(newLayer);
newLayer1 = createLayer(newLayer1);
newLayer2 = createLayer(newLayer2);
newLayer3 = createLayer(newLayer3);
//ui.widget_4->setStyleSheet("QWidget{border: 2px outset green }");
connect(ui.pushButton_2, &QPushButton::clicked, this, &QtWidgetsApplicationxx::openPort);
connect(ui.pushButton_3, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_3_clicked);//101
connect(ui.pushButton_4, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_4_clicked);//102
connect(ui.pushButton_5, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_5_clicked);//103
connect(ui.pushButton_6, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_6_clicked);//104
connect(ui.pushButton_7, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_7_clicked);//105
connect(ui.pushButton_8, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_8_clicked);
connect(ui.pushButton_9, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_9_clicked);//107
connect(ui.pushButton_10, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_10_clicked);//106
connect(ui.pushButton_11, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_11_clicked);
connect(ui.pushButton_12, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_12_clicked);//222
connect(ui.pushButton_13, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_13_clicked);
connect(ui.pushButton_14, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_14_clicked);//225
connect(ui.pushButton_15, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_15_clicked);//223
connect(ui.pushButton_16, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_16_clicked);
connect(ui.pushButton_17, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_17_clicked);
connect(ui.pushButton_18, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_18_clicked);
connect(ui.pushButton_19, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_19_clicked);
connect(ui.pushButton_20, &QPushButton::click, this, &QtWidgetsApplicationxx::on_pushButton_20_clicked);
connect(ui.actionxx, &QAction::triggered, this, &QtWidgetsApplicationxx::on_action_triggered);
connect(ui.action, &QAction::triggered, this, &QtWidgetsApplicationxx::on_action_1_triggered);
connect(ui.action_2, &QAction::triggered, this, &QtWidgetsApplicationxx::on_action_2_triggered);
connect(ui.action_3, &QAction::triggered, this, &QtWidgetsApplicationxx::on_action_3_triggered);
//---------------------------------------------------------------------------------------------------------
connect(ui.comboBox_2, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[=]() {
if (ui.comboBox_2->currentIndex() == 0)
{
ui.lineEdit->setText("101");
}if (ui.comboBox_2->currentIndex() == 1)
{
ui.lineEdit->setText("102");
}if (ui.comboBox_2->currentIndex() == 2)
{
ui.lineEdit->setText("103");
}if (ui.comboBox_2->currentIndex() == 3)
{
ui.lineEdit->setText("104");
}if (ui.comboBox_2->currentIndex() == 4)
{
ui.lineEdit->setText("105");
}if (ui.comboBox_2->currentIndex() == 5)
{
ui.lineEdit->setText("106");
}if (ui.comboBox_2->currentIndex() == 6)
{
ui.lineEdit->setText("107");
}if (ui.comboBox_2->currentIndex() == 7)
{
ui.lineEdit->setText("201");
}if (ui.comboBox_2->currentIndex() == 8)
{
ui.lineEdit->setText("202");
}if (ui.comboBox_2->currentIndex() == 9)
{
ui.lineEdit->setText("203");
}if (ui.comboBox_2->currentIndex() == 10)
{
ui.lineEdit->setText("222");
}if (ui.comboBox_2->currentIndex() == 11)
{
ui.lineEdit->setText("223");
}if (ui.comboBox_2->currentIndex() == 12)
{
ui.lineEdit->setText("224");
}if (ui.comboBox_2->currentIndex() == 13)
{
ui.lineEdit->setText("225");
}
});
connect(ui.comboBox_4, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[=]() {
if (ui.comboBox_4->currentIndex() == 0)
{
QString f= ui.comboBox_4->currentText();//获取comboBox选项
firecolor = "./" + f+ ".svg";
}if (ui.comboBox_4->currentIndex() == 1)
{
QString f = ui.comboBox_4->currentText();
firecolor = "./fire" + f + ".svg";
}if (ui.comboBox_4->currentIndex() == 2)
{
QString f = ui.comboBox_4->currentText();
firecolor = "./fire" + f + ".svg";
}if (ui.comboBox_4->currentIndex() == 3)
{
QString f = ui.comboBox_4->currentText();
firecolor = "./fire" + f + ".svg";
}
});
}
//----------------------------------------------------------------------------------------------------------
//切换页
void QtWidgetsApplicationxx::on_action_triggered()
{
ui.stackedWidget->setCurrentIndex(0);
ui.textEdit_2->append("//*******************************************************");
}
void QtWidgetsApplicationxx::on_action_1_triggered()
{
ui.stackedWidget->setCurrentIndex(1);
ui.textEdit_2->append("//*******************************************************");
}
void QtWidgetsApplicationxx::on_action_2_triggered()
{
ui.stackedWidget->setCurrentIndex(2);
ui.textEdit_2->append("//*******************************************************");
}
void QtWidgetsApplicationxx::on_action_3_triggered()
{
ui.stackedWidget->setCurrentIndex(3);
ui.textEdit_2->append("//*******************************************************");
}
//打开串口
void QtWidgetsApplicationxx::openPort()
{
if (m_serialPort->isOpen())//如果串口已经打开了 先给他关闭了
{
m_serialPort->clear();
m_serialPort->close();
}
m_serialPort->setPortName(ui.comboBox->currentText());//当前选择的串口名字
if (!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
{
ui.lineEdit_3->setText("Failed to open serial port!");
ui.textEdit_2->append("serialport$:Failed to open serial port!");
return;
}
ui.lineEdit_3->setText("Serial Port Open successfully!");
ui.textEdit_2->append("serialport$:Serial Port Open successfully!");
//QSerialPort::Baud9600
m_serialPort->setBaudRate(QSerialPort::Baud9600, QSerialPort::AllDirections);//设置波特率和读写方向
m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
m_serialPort->setParity(QSerialPort::NoParity); //无奇偶校验位
m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
m_serialPort->open(QIODevice::ReadWrite);
connect(m_serialPort, &QSerialPort::readyRead, this, &QtWidgetsApplicationxx::receiveInfo);
por = 1;//控制串口
comp = 0;//控制工控机
//检测是否开机
QByteArray temp;
temp.resize(1);
temp[0] = 224;
m_serialPort->write(temp);
}
///
QtWidgetsApplicationxx::~QtWidgetsApplicationxx()
{
if (m_serialPort->isOpen())
{
m_serialPort->close();
}
delete m_serialPort;
}
QStringList QtWidgetsApplicationxx::getPortNameList()
{
QStringList m_serialPortName;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
m_serialPortName << info.portName();
//qDebug() << "serialPortName:" << info.portName();
}
return m_serialPortName;
}
//-------------------------------------------------------------------------------------------------
//快捷键设置
void QtWidgetsApplicationxx::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Return)
{
on_pushButton_11_clicked();//发送数据
}if (event->key() == Qt::Key_R)
{
on_pushButton_12_clicked();//复位。。。。
}if (event->key() == Qt::Key_L)
{
on_pushButton_3_clicked();//雷达
}if (event->key() == Qt::Key_G)
{
on_pushButton_4_clicked();//惯导
}if (event->key() == Qt::Key_I)
{
on_pushButton_5_clicked();//红外相机
}if (event->key() == Qt::Key_M)
{
on_pushButton_8_clicked();//地图
}if (event->key() == Qt::Key_T)
{
on_pushButton_9_clicked();//检测火
}if (event->key() == Qt::Key_Delete)
{
on_pushButton_13_clicked();//清空特征
}if (event->key() == Qt::Key_Escape)
{
on_pushButton_14_clicked();
ui.lineEdit->setText("Shutdown ..");//关机
ui.textEdit_2->append("serialport$:Shutdown ..");
}if (event->key() == Qt::Key_F1)
{
on_pushButton_15_clicked();
ui.lineEdit->setText("Reboot ..");//重启Control
ui.textEdit_2->append("serialport$:Reboot ..");
}if (event->key() == Qt::Key_Control)
{
comp = 1;
ui.lineEdit_3->setText("Booting successful!!");//默认开机成功
ui.lineEdit->setText("Booting successful!!");
ui.textEdit_2->append("##:Booting successful!!");
}if (event->key() == Qt::Key_S)
{
por = 1;
ui.textEdit_2->append("##:Serial Port Open successfully!!");//默认串口已打开
}if (event->key() == Qt::Key_Shift)
{
page++;
if (page == 1) { //切换页
on_action_triggered();
}if (page == 2) {
on_action_1_triggered();
}if (page == 3) {
on_action_2_triggered();
}if (page == 4) {
on_action_3_triggered();
page = 0;
}
}
}
//combx设置---------------------------------------------------------------------------------------------------
/*listenthread getPortNameList
void QtWidgetsApplicationxx::print_s()
{
if (ui.comboBox->currentIndex() == 0)
{
ui.lineEdit->setText("101");
}if (ui.comboBox->currentIndex() == 1)
{
ui.lineEdit->setText("102");
}
}*/
//------------------------------------------------------------------------------------------------------
//解析接收到数据
void QtWidgetsApplicationxx::receiveInfo()
{
QByteArray info = m_serialPort->readAll();
double *temp = new double[32];
int j = 0;
QString strDisplay;
QString str = info.toHex().data();
for (int i = 0; i < str.length(); i += 2)
{
QString st = str.mid(i, 2);//返回一个字符串 从指定位置返回n个字符
strDisplay += st;
strDisplay += " ";//每个字节中间的空格
QString str_hex = st; //获取十六进制数
double dec = str_hex.toInt(0, 16);
//qDebug() << "dec==:" << dec;
temp[j] = dec;
j++;
}
//
if (temp[0] == 101 && (k != 2) && lei_ == 1)//激光雷达启动成功
{
lei = 1;
ui.pushButton_3->setText("Lidar ON");
ui.lineEdit_3->setText("Lidar activated!!");
ui.lineEdit->setText("Lidar activated!!");
ui.textEdit_2->append("##:Lidar activated!!");
}if (temp[0] == 201 && (k != 2) && lei_ == 0)//激光雷达已关闭
{
lei = 0;
ui.pushButton_3->setText("Lidar OFF");
ui.lineEdit_3->setText("Lidar is down!!");
ui.lineEdit->setText("Lidar is down!!");
ui.textEdit_2->append("##:Lidar is down!!");
}
if (temp[0] == 102 && (k != 2) && guan_ == 1)//惯导启动成功GNSS/INS
{
guan = 1;
ui.pushButton_4->setText("GNSS/INS ON");
ui.lineEdit_3->setText("GNSS/INS activated!!");
ui.lineEdit->setText("GNSS/INS activated!!");
ui.textEdit_2->append("##:GNSS/INS activated!!");
}if (temp[0] == 202 && (k != 2) && guan_ == 0)//惯导已关闭
{
guan = 0;
ui.pushButton_4->setText("GNSS/INS OFF");
ui.lineEdit_3->setText("GNSS/INS is down!!");
ui.lineEdit->setText("GNSS/INS is down!!");
ui.textEdit_2->append("##:GNSS/INS is down!!");
}
if (temp[0] == 103 && (k != 2) && xiang_ == 1)//红外相机启动成功
{
xiang = 1;
ui.pushButton_5->setText("I-Camera ON");
ui.lineEdit_3->setText("I-Camera activated!!");
ui.lineEdit->setText("I-Camera activated!!");
ui.textEdit_2->append("##:I-Camera activated!!");
}if (temp[0] == 203 && (k != 2) && xiang_ == 0)//红外相机已关闭
{
xiang = 0;
ui.pushButton_5->setText("I-Camera OFF");
ui.lineEdit_3->setText("I-Camera is down!!");
ui.lineEdit->setText("I-Camera is down!!");
ui.textEdit_2->append("##:I-Camera is down!!");
}
if (temp[0] == 104 && ros == 0)//正在记录ROS包
{
ros = 1;
ui.lineEdit_3->setText("Recording packets...");
ui.lineEdit->setText("Recording packets...");
ui.textEdit_2->append("##:Recording packets.....");
}
if (temp[0] == 105 && ros == 1)//ROS包记录结束
{
ros = 0;
ui.lineEdit_3->setText("Package record closed!!");
ui.lineEdit->setText("Package record closed!!");
ui.textEdit_2->append("##:Package record closed!!");
}
if (temp[0] == 106 && bao_ == 1)//开始播放ROS包
{
bao = 1;
k = 2;
ui.lineEdit_3->setText("Playing the Ros package..");
ui.lineEdit->setText("Playing Ros package..");
ui.textEdit_2->append("##:Playing Ros package.....");
}if (temp[0] == 206 && bao_ == 0)//ROS包播放结束
{
bao = 0;
ui.lineEdit_3->setText("End of Ros package!!");
ui.lineEdit->setText("End Ros package!!");
ui.textEdit_2->append("##:End Ros package!!");
}
if (temp[0] == 222 && Reset == 1)//复位
{
Reset = 0;
ui.lineEdit_3->setText("Reset successful!!");
ui.lineEdit->setText("Reset successful!!");
ui.textEdit_2->append("##:Reset successful!!");
}
if (temp[0] == 207 && k_ == 1)
{
k_ = 0;
k = 1;
ui.lineEdit_3->setText("Stop the fire detection!!");
ui.lineEdit->setText("Stop fire detection!!");
ui.textEdit_2->append("##:Stop fire detection!!");
}if (temp[0] == 223)
{
comp = 0;
reboot = 1;
ui.lineEdit_3->setText("Reboot successful!!");//重启成功
ui.lineEdit->setText("Reboot successful!!");
ui.textEdit_2->append("##:Reboot successful!!");
}if (temp[0] == 224)
{
comp = 1;//comp == 1
reboot = 0;
ui.lineEdit_3->setText("Booting successful!!");//开机成功
ui.lineEdit->setText("Booting successful!!");
ui.textEdit_2->append("##:Booting successful!!");
}if (temp[0] == 225)
{
comp = 0;
ui.lineEdit_3->setText("Shutdown successful!!");//关机成功
ui.lineEdit->setText("Shutdown successful!!");
ui.textEdit_2->append("##:Shutdown successful!!");
}
///Booting successful
if (kk == 1 && k == 2 && (k_ != 0) && (temp[0] < 100))
{
//ui.lineEdit_3->setText(strDisplay);
double xx, yy;
countxy(temp, &xx, &yy);
if (yy < 100)
{
//QString s = QString::number(xx);
//ui.lineEdit_3->setText(s);
newLayer1->startEditing();
newLayer1->deleteFeatures(newLayer1->allFeatureIds());//删除特征(清空图层)
newLayer1->commitChanges();
// 更新范围
newLayer1->updateExtents();
drawplane(newLayer1, xx, yy);//qgis 画点--plane
}
/*
if (yy > 100)
{
yy = yy - 100;
drawfire(newLayer2, xx, yy);//qgis 画点--fire
}
*/
} //
if (temp[0] == 107 && k_ == 0)
{
k_ = 1;
k = 2;
ui.lineEdit_3->setText("Start testing the fire!!");
ui.lineEdit->setText("Start testing fire!!");
ui.textEdit_2->append("##:Start testing fire!!");
}
}
//------------------------------------------------------------------------------------------------
//int lei = 0, guan = 0, xiang = 0, ros = 0, rosclose = 0;
//激光雷达
void QtWidgetsApplicationxx::on_pushButton_3_clicked()
{
QByteArray temp;
temp.resize(1);
if (lei == 0 && (k != 2) && comp == 1)
{
lei_ = 1;
temp[0] = 101;//启动激光雷达
ui.lineEdit_3->setText("Turn on the Lidar");
ui.textEdit_2->append("serialport$:Turn on the Lidar");
m_serialPort->write(temp);
}
if (lei == 1 && (k != 2) && comp == 1)
{
lei_ = 0;
temp[0] = 201;//关闭激光雷达
ui.lineEdit_3->setText("Turn off the Lidar");
ui.textEdit_2->append("serialport$:Turn off the Lidar");
m_serialPort->write(temp);
}
if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}
if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//惯导
void QtWidgetsApplicationxx::on_pushButton_4_clicked()
{
QByteArray temp;
temp.resize(1);
if (guan == 0 && (k != 2) && comp == 1)
{
guan_ = 1;
temp[0] = 102;//启动惯导
ui.lineEdit_3->setText("Turn on the GNSS/INS");
ui.textEdit_2->append("serialport$:Turn on the GNSS/INS");
m_serialPort->write(temp);
}if (guan == 1 && (k != 2) && comp == 1)
{
guan_ = 0;
temp[0] = 202;//关闭惯导
ui.lineEdit_3->setText("Turn off the GNSS/INS");
ui.textEdit_2->append("serialport$:Turn off the GNSS/INS");
m_serialPort->write(temp);
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//红外相机
void QtWidgetsApplicationxx::on_pushButton_5_clicked()
{
QByteArray temp;
temp.resize(1);
if (xiang == 0 && (k != 2) && comp == 1)
{
xiang_ = 1;
temp[0] = 103;//启动红外相机
ui.lineEdit_3->setText("Turn on the I-camera");
ui.textEdit_2->append("serialport$:Turn on the I-camera");
m_serialPort->write(temp);
}if (xiang == 1 && (k != 2) && comp == 1)
{
xiang_ = 0;
temp[0] = 203;//关闭红外相机
ui.lineEdit_3->setText("Turn off the I-camera");
ui.textEdit_2->append("serialport$:Turn on the I-camera");
m_serialPort->write(temp);
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//记录ROS包
void QtWidgetsApplicationxx::on_pushButton_6_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 104;//开始记录ROS包
if (ros == 0 && comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Start recording the Ros package");
ui.textEdit_2->append("serialport$:Start recording the Ros package");
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//记录结束
void QtWidgetsApplicationxx::on_pushButton_7_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 105;//记录结束
if (ros == 1 && comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("End of record");
ui.textEdit_2->append("serialport$:End of record");
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//--------------------------------------------------------------------------------------------------------------
//打开地图
void QtWidgetsApplicationxx::on_pushButton_8_clicked()
{
if (m % 2 == 0)//打开地图
{
m_ = 1;
openMap(mapCanvas, newLayer, newLayer1, newLayer2, newLayer3);
//------------------------------------------------------
/*
//将窗口部件布局
mpMapLayout = new QVBoxLayout();
mpMapLayout->addWidget(mapCanvas);
ui.widget->setLayout(mpMapLayout);
setCentralWidget(ui.widget);
*/
//——-------------------------------------------------
ui.pushButton_8->setText("Close Map");
kk = 1;
}if (m % 2 == 1)
{
mapCanvas->close();
m_ = 0;
ui.pushButton_8->setText("Open Map");
kk = 0;
}
m++;
}
//检测火
void QtWidgetsApplicationxx::on_pushButton_9_clicked()
{
//kk == 1 && k == 2 && (k_ != 0) && (temp[0] < 100)
QByteArray temp;
temp.resize(1);
if (k_ == 0 && comp == 1)
{
kk_ = 1;
temp[0] = 107;//开始检测火
ui.lineEdit_3->setText("Start testing the fire");
ui.textEdit_2->append("serialport$:Start testing the fire");
m_serialPort->write(temp);
on_pushButton_16_clicked();
}if (k_ == 1 && comp == 1)
{
temp[0] = 207;//停止检测
kk_ = 0;
ui.lineEdit_3->setText("Stop the fire detection");
ui.textEdit_2->append("serialport$:Stop the fire detection");
m_serialPort->write(temp);
on_pushButton_17_clicked();
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//播放ROS包
void QtWidgetsApplicationxx::on_pushButton_10_clicked()
{
if (kk_ == 1)
{
k = 0;
}
QByteArray temp;
temp.resize(1);//lei_ = 2, guan_ = 2, xiang_ = 2
if (lei_ == 1)
{
ui.lineEdit_3->setText("Please turn off the LIDAR");
ui.textEdit_2->append("[Warning]:Please turn off the LIDAR");
}if (guan_ == 1)
{
ui.lineEdit_3->setText("Please turn off the GNSS/INS");
ui.textEdit_2->append("[Warning]:Please turn off the GNSS/INS");
}if (xiang_ == 1)
{
ui.lineEdit_3->setText("Please turn off the I-camera");
ui.textEdit_2->append("[Warning]:Please turn off the I-camera");
}if ((lei_ != 1) && (guan_ != 1) && (xiang_ != 1) && comp == 1)
{
if (bao == 0)
{
bao_ = 1;
temp[0] = 106;//开始播放ROS包
ui.lineEdit_3->setText("Start playing the Ros package");
ui.textEdit_2->append("serialport$:Start playing the Ros package");
m_serialPort->write(temp);
}if (bao == 1)
{
bao_ = 0;
temp[0] = 206;//停止播放ROS包
ui.lineEdit_3->setText("Stop playing the Ros package");
ui.textEdit_2->append("serialport$:Stop playing the Ros package");
m_serialPort->write(temp);
}
} //
if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}
if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//自定义发送数据
void QtWidgetsApplicationxx::on_pushButton_11_clicked()
{
QString str = ui.lineEdit->text();//自定义发送数据
int dec = str.toInt(0, 10);
QByteArray temp;
temp.resize(1);
temp[0] = dec;
if (comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Sending data now..");
ui.textEdit_2->append("serialport$:Sending data now..");
}if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}
if (por == 0)
{
ui.lineEdit->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//复位
void QtWidgetsApplicationxx::on_pushButton_12_clicked()
{
/*
int k = 0, kk = 0, k_ = 0;//控制检测火
int lei = 0, guan = 0, xiang = 0, ros = 0, bao =0 ;//控制设备
int lei_ = 2, guan_ = 2, xiang_ = 2, bao_ = 2, Reset = 2;
*/
k = 0; //复位
//kk = 0;//打开地图
kk_ = 0;
k_ = 0;
lei = 0;
guan = 0;
xiang = 0;
ros = 0;
bao = 0;
lei_ = 2;
guan_ = 2;
xiang_ = 2;
bao_ = 2;
Reset = 2;
QByteArray temp;
temp.resize(1);
temp[0] = 222;
if (comp == 1)
{
m_serialPort->write(temp);
}
//mapCanvas->close();
ui.pushButton_3->setText("Lidar");//
ui.pushButton_4->setText("GNSS/INS");
ui.pushButton_5->setText("I-Camera");
ui.lineEdit_3->setText("Reset..");
ui.textEdit_2->append("serialport$:Reset..");
if (comp == 0)
{
ui.lineEdit_3->setText("Please turn on the computer");
ui.textEdit_2->append("[Warning]:Please turn on the computer");
}if (por == 0)
{
ui.lineEdit_3->setText("Please open the Serial Port");
ui.textEdit_2->append("[Warning]:Please open the Serial Port");
}
}
//清空图层
void QtWidgetsApplicationxx::on_pushButton_13_clicked()
{
newLayer->startEditing();
newLayer->deleteFeatures(newLayer->allFeatureIds());//删除特征(清空图层)
newLayer->commitChanges();
newLayer->updateExtents();// 更新范围
newLayer1->startEditing();
newLayer1->deleteFeatures(newLayer1->allFeatureIds());
newLayer1->commitChanges();
newLayer1->updateExtents();
newLayer2->startEditing();
newLayer2->deleteFeatures(newLayer2->allFeatureIds());
newLayer2->commitChanges();
newLayer2->updateExtents();
newLayer3->startEditing();
newLayer3->deleteFeatures(newLayer3->allFeatureIds());
newLayer3->commitChanges();
newLayer3->updateExtents();
}
//关机
void QtWidgetsApplicationxx::on_pushButton_14_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 225;//关机
if (comp == 1)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Computer shutdown");
ui.textEdit_2->append("serialport$:Computer shutdown");
}
}
//重启
void QtWidgetsApplicationxx::on_pushButton_15_clicked()
{
QByteArray temp;
temp.resize(1);
temp[0] = 223;//重启
if (comp == 1 && reboot == 0)
{
m_serialPort->write(temp);
ui.lineEdit_3->setText("Reboot the computer");
ui.textEdit_2->append("serialport$:Reboot the computer");
}
}
//--------------------------------------------------------------------------------------------------------------
//打开线程
void QtWidgetsApplicationxx::on_pushButton_16_clicked()
{
start = true;
if (thread_ == 0)
{
thread1 = QtConcurrent::run([=]()//开启一个新线程(创建socket通信)
{
ui.lineEdit_2->setText("please wait...");
ui.textEdit_2->append("socket$:please wait...");
thread_ = 1;//自锁
socketMat->socketConnect_server(7777);
//QString s = "hello new thread!";
ui.lineEdit_2->setText("hello new thread!");
ui.textEdit_2->append("socket$:hello new thread!");
QMutex mutex1;
while (start)
{
char imge[32];
socketMat->receive3(imge);
mutex1.lock();
for (int i = 0; i < 32; i++) {
fire[i] = imge[i];
}
mutex1.unlock();
//Sleep(20);
}
});
thread2 = QtConcurrent::run([=]()//开启一个新线程(画火点)
{
QMutex mutex2;
while (start)
{
mutex2.lock();
if (fire[0] == 77 && fire[11] == 77)
{
double temp[10];
double xx, yy;
for (int i = 1; i < 11; i++) {
int j = i - 1;
temp[j] = fire[i];
}
socketcountxy(temp, &xx, &yy);
fire[0] = 0;
if (yy < 100)
{
file[0] = xx;
file[1] = yy;
drawfire(newLayer2, xx, yy);//qgis 画点--fire
QString qs = QString::number(xx)+" "+ QString::number(yy);
ui.lineEdit_2->setText(qs);
}
}
mutex2.unlock();
//Sleep(20);
}
});
thread3 = QtConcurrent::run([=]()//开启一个新线程(保存火点数据)
{
QMutex mutex3;
newfile(filename);//以日期建立文件名
while (start)
{
mutex3.lock();
fp = fopen(filename, "a+");
if (file[0] != 0)
{
for (int j = 0; j < 2; j++)
{
fprintf(fp, "%12.6f", file[j]);
}
fprintf(fp, "\n");
}
file[0] = 0;
fclose(fp);
mutex3.unlock();
//Sleep(20);
}
});
}
}
//关闭线程
void QtWidgetsApplicationxx::on_pushButton_17_clicked()
{
QString s = "thread is down!";
ui.lineEdit_2->setText(s);
ui.textEdit_2->append("socket$:thread is down!!");
socketMat->socketDisconnect();//关闭socket连接
thread1.cancel();//关闭线程
thread2.cancel();
thread3.cancel();
thread_ = 0;
start = false;
}
//获取TXT文件目录
void QtWidgetsApplicationxx::on_pushButton_18_clicked()
{
QDir dir(QDir::currentPath());//当前文件路径
QString filtername = "*.txt";
QStringList filter;
filter << filtername;
dir.setNameFilters(filter);
QStringList Neuronindex = dir.entryList();
ui.comboBox_3->addItems(Neuronindex);//把列表加载到comboBox_3
QString s = QString::number(Neuronindex.size());//txt文件个数
ui.lineEdit_2->setText(s);
}
//离线火点
void QtWidgetsApplicationxx::on_pushButton_19_clicked()
{
if (m_ == 0)
{
on_pushButton_8_clicked();
}
m_ = 1;
if (thread_4 == 0)
{
thread4.cancel();
}if (thread_5 == 0)
{
thread5.cancel();
}if (thread_6 == 0)
{
thread6.cancel();
}if (thread_7 == 0)
{
thread7.cancel();
}
QString str = ui.comboBox_3->currentText();//获取comboBox选项
char *ch; //将QString转为char
QByteArray ba = str.toLatin1();
ch = ba.data();
const char * filename = ch;
int line = 1;
pushnum(filename);
readline(filename, line);
QString s = QString::number(line);
ui.lineEdit_2->setText(s);
if ((ui.comboBox_4->currentIndex() == 0) && (thread_4 == 0))
{
thread4 = QtConcurrent::run([=]()//开启一个新线程
{
thread_4 = 1;//自锁
QMutex mutex4;
mutex4.lock();
QString f = ui.comboBox_4->currentText();//获取comboBox选项
firecolor = "./" + f + ".svg";
/
vector<double> M4 = V;
V.clear();
mutex4.unlock();
int i = 0;
vector<double>::iterator it;
for (it = M4.begin(); it != M4.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfireline(newLayer, firecolor, x, y);//qgis 画点--fire
i++;
QString qs ="fire"+ QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
M4.clear();
thread_4 = 0;
});
}if ((ui.comboBox_4->currentIndex() == 1) && (thread_5 == 0))
{
thread5 = QtConcurrent::run([=]()//开启一个新线程
{
thread_5 = 1;//自锁
QMutex mutex5;
mutex5.lock();
QString f = ui.comboBox_4->currentText();
firecolor = "./fire" + f + ".svg";
vector<double> M5= V;
V.clear();
mutex5.unlock();
int i = 0;
vector<double>::iterator it;
for (it = M5.begin(); it != M5.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfireline(newLayer1, firecolor, x, y);//qgis 画点--fire
i++;
QString qs = "fire" + QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
M5.clear();
thread_5 = 0;
});
}if ((ui.comboBox_4->currentIndex() == 2) && (thread_6 == 0))
{
QString f = ui.comboBox_4->currentText();
firecolor = "./fire" + f + ".svg";
thread6 = QtConcurrent::run([=]()//开启一个新线程
{
thread_6 = 1;//自锁
QMutex mutex6;
mutex6.lock();
vector<double> M6 = V;
V.clear();
mutex6.unlock();
int i = 0;
vector<double>::iterator it;
for (it = M6.begin(); it != M6.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfireline(newLayer2, firecolor, x, y);
i++;
QString qs = "fire" + QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
M6.clear();
thread_6 = 0;
});
}if ((ui.comboBox_4->currentIndex() == 3) && (thread_7 == 0))
{
QString f = ui.comboBox_4->currentText();
firecolor = "./fire" + f + ".svg";
thread7 = QtConcurrent::run([=]()//开启一个新线程
{
thread_7 = 1;//自锁
QMutex mutex7;
mutex7.lock();
vector<double> M7 = V;
V.clear();
mutex7.unlock();
int i = 0;
vector<double>::iterator it;
for (it = M7.begin(); it != M7.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfireline(newLayer3, firecolor, x, y);//qgis 画点--fire
i++;
QString qs = "fire" + QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
M7.clear();
thread_7 = 0;
});
}
/*
QString str = ui.comboBox_3->currentText();//获取comboBox选项
char *ch; //将QString转为char
QByteArray ba = str.toLatin1();
ch = ba.data();
const char * filename = ch;
int line = 1;
pushnum(filename);
readline(filename, line);
QtConcurrent::run([=]()//开启一个新线程
{
QMutex mutex4;
mutex4.lock();
QString s = QString::number(line);
ui.lineEdit_2->setText(s);
int i = 0;
vector<double> M = V;
V.clear();
vector<double>::iterator it;
if (ui.comboBox_4->currentIndex() == 0)
{
for (it = M.begin(); it != M.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfire(newLayer, x, y);//qgis 画点--fire
i++;
QString qs = QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
}if (ui.comboBox_4->currentIndex() == 1)
{
for (it = M.begin(); it != M.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfire(newLayer1, x, y);//qgis 画点--fire
i++;
QString qs = QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
}if (ui.comboBox_4->currentIndex() == 2)
{
for (it = M.begin(); it != M.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfire(newLayer2, x, y);//qgis 画点--fire
i++;
QString qs = QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
}if (ui.comboBox_4->currentIndex() == 3)
{
for (it = M.begin(); it != M.end(); it++)
{
double x = *it;
it++;
double y = *it;
drawfire(newLayer3, x, y);//qgis 画点--fire
i++;
QString qs = QString::number(line) + " : " + QString::number(i);
ui.lineEdit_2->setText(qs);
Sleep(100);
}
}
M.clear();
mutex4.unlock();
});
*/
}
//清空离线火点图层
void QtWidgetsApplicationxx::on_pushButton_20_clicked()
{
if (ui.comboBox_4->currentIndex() == 0)
{
newLayer->startEditing();
newLayer->deleteFeatures(newLayer->allFeatureIds());//删除特征(清空图层)
newLayer->commitChanges();
newLayer->updateExtents();// 更新范围
}if (ui.comboBox_4->currentIndex() == 1)
{
newLayer1->startEditing();
newLayer1->deleteFeatures(newLayer1->allFeatureIds());
newLayer1->commitChanges();
newLayer1->updateExtents();
}if (ui.comboBox_4->currentIndex() == 2)
{
newLayer2->startEditing();
newLayer2->deleteFeatures(newLayer2->allFeatureIds());
newLayer2->commitChanges();
newLayer2->updateExtents();
}if (ui.comboBox_4->currentIndex() == 3)
{
newLayer3->startEditing();
newLayer3->deleteFeatures(newLayer3->allFeatureIds());
newLayer3->commitChanges();
newLayer3->updateExtents();
}
}
//---------------------------------------------------------------------------------------------------------------
//计算无人机
void QtWidgetsApplicationxx::countxy(double r[8], double *xx, double *yy)
{
double x = r[0] * power(255, 3) + r[1] * power(255, 2) + r[2] * 255 + r[3];
double y = r[4] * power(255, 3) + r[5] * power(255, 2) + r[6] * 255 + r[7];
x = x / 1000000;
y = y / 1000000;
cout << setprecision(6) << fixed << x << endl;
cout << setprecision(6) << fixed << y << endl;
*xx = x;
*yy = y;
}
//计算火点
void QtWidgetsApplicationxx::socketcountxy(double r[10], double *xx, double *yy)
{
double x = r[0] * power(127, 4) + r[1] * power(127, 3)+ r[2] * power(127, 2) + r[3] * 127 + r[4];
double y = r[5] * power(127, 4) + r[6] * power(127, 3) + r[7] * power(127, 2) + r[8] * 127 + r[9];
x = x / 1000000;
y = y / 1000000;
cout << setprecision(6) << fixed << x << endl;
cout << setprecision(6) << fixed << y << endl;
*xx = x;
*yy = y;
}
//x的y次方
int QtWidgetsApplicationxx::power(int x, int n)
{
int val = 1;
while (n--)
val *= x;
return val;
}
//获取当前日期
void QtWidgetsApplicationxx::newfile(char filename[200])
{
time_t nowtime = time(NULL);
struct tm* p = localtime(&nowtime);
sprintf(filename, "afire%d.%d.%d.%d.%d.txt", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min);
cout << filename << endl;
}
//计算txt文件行数
void QtWidgetsApplicationxx::readline(const char* strPath, int &line)
{
ifstream is(strPath);
char c;
while (is.get(c))
{
if (c == '\n')
line++;
}
is.close();
cout << "line=" << line << endl;
}
//将数据压入堆栈
void QtWidgetsApplicationxx::pushnum(const char* strPath)
{
ifstream is(strPath);
double d;
while (is >> d)
{
V.push_back(d);
}
is.close();
}
//--------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//************ 创建图层 **********************************************************************************************
QgsVectorLayer* QtWidgetsApplicationxx::createLayer(QgsVectorLayer*newLayer)
{
QString layerProperties = "Point?"; // 定义了我们创建图层的几何类型,可以是"Point"、"LineString"、"Polygon"、"MultiPoint"、"MultiLineString"、"MultiPolygon"其中之一
layerProperties.append(QString("crs=EPSG:4236&")); //定义图层的参照坐标,如果需要一定灵活性可以参照QGis的方式通过对话框选取,或是根据自己的需求来实现,需要改变的仅仅是"EPSG:4326" 而已
layerProperties.append(QString("field=id:integer&field=name:string(50)&")); //定义的图层字段,多个字段用"&" 进行连接,完整形式为 field=name:type(length,precision) ,从参数看不仅可以定义长度还可以定义其精度
layerProperties.append(QString("index=yes&")); // 定义空间索引,于数据量较大的图层很有用
layerProperties.append(QString( // 通过QUuid创建了一个全局唯一标识符(UUID),Qt中解释主要是用于分布式计算环境中的实体标识,而此处是用于当我们多次创建临时图层时的唯一标识符
"memoryid=%1").arg(QUuid::createUuid().toString()));
newLayer = new QgsVectorLayer(layerProperties, QString("临时点层"), QString("memory"));//开始创建临时图层
//QgsMapLayerRegistry::instance()->addMapLayer(newLayer);
return newLayer;
}
//*************** 获取地图 *******************************************************************************************
/*QgsVectorLayer* */
void QtWidgetsApplicationxx::openMap(QgsMapCanvas *mapCanvas, QgsVectorLayer*newLayer, QgsVectorLayer*newLayer1, QgsVectorLayer*newLayer2, QgsVectorLayer*newLayer3)
{
QString myPluginsDir = "..\\plugins"; //注册插件库
QgsProviderRegistry::instance(myPluginsDir);
//QList<QgsMapLayer*>layer;
//创建地图画布
//QgsMapCanvas*mapCanvas = new QgsMapCanvas();
//mapCanvas = new QgsMapCanvas();
//*************获取网络地图************************
//QgsRasterLayer*rastermap = new QgsRasterLayer("type=xyz&url=https://webst01.is.autonavi.com/appmaptile?style%3D6%26x%3D%7Bx%7D%26y%3D%7By%7D%26z%3D%7Bz%7D&zmax=18&zmin=0","gaode", "wms");
QgsRasterLayer*rastermap = new QgsRasterLayer("type=xyz&url=https://tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0", " ", "wms");
//*************添加shp地图*************************
//QString fileName = QFileDialog::getOpenFileName(this, tr("Open shape file"), "", "*.shp");
//QStringList temp = fileName.split('/');
//QString basename = temp.at(temp.size() - 1);
//QgsVectorLayer*rastermap = new QgsVectorLayer(fileName, basename, "ogr");
if (!rastermap->isValid())
{
QMessageBox::critical(this, "error", QString("This layer is invalid"));
}
else
{
layerlist.append(newLayer);
}
//把图层添加到地图画布并显示
mapCanvas->setExtent(rastermap->extent());// 设置显示范围。Extent:范围、尺度
layerlist.append(newLayer1);//将图层1加载到地图中
QgsProject::instance()->addMapLayers(layerlist);
layerlist.append(newLayer2);
QgsProject::instance()->addMapLayers(layerlist);
layerlist.append(newLayer3);
QgsProject::instance()->addMapLayers(layerlist);
layerlist.append(rastermap);
mapCanvas->setLayers(layerlist);//设置图层集合
mapCanvas->setVisible(false);//设置是否可见
mapCanvas->freeze(false);//解冻对图层的操作
mapCanvas->resize(800, 500);
mapCanvas->refresh();
mapCanvas->show();
//return newLayer1;
}
//*************** 创建点 ******************************************************************************************
QgsVectorLayer* QtWidgetsApplicationxx::drawplane(QgsVectorLayer*newLayer1, double x, double y)
{
//定义坐标转换对象trans,从src向dest转换(CRS转换)
//EPSG:4326指的是WGS84系统..EPSG是最常用的授权机构,它涵盖了全世界范围广泛的坐标系统。
QgsCoordinateReferenceSystem src(4326, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateReferenceSystem dest(3857, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateTransformContext context;
QgsCoordinateTransform trans(src, dest, context);
//转换坐标
QgsPointXY point1 = trans.transform(x, y);
//QgsVectorDataProvider* dateProvider = newLayer1->dataProvider();//创建一个容器
// plane
QgsSvgMarkerSymbolLayer* svgMarker1 = new QgsSvgMarkerSymbolLayer("./plane13.svg");// F:/OSGeo4W64/QtQgisVs
QgsSymbolLayerList symList1;
symList1.append(svgMarker1);
QgsMarkerSymbol* markSym1 = new QgsMarkerSymbol(symList1);
QgsSingleSymbolRenderer* symRenderer1 = new QgsSingleSymbolRenderer(markSym1);
newLayer1->setRenderer(symRenderer1);
svgMarker1->setSize(10);
QgsFeature MyFeature1;
MyFeature1.setGeometry(QgsGeometry::fromPointXY(point1));
MyFeature1.setAttributes(QgsAttributes() << QVariant(2) << QVariant("test"));
//MyFeature1.setc
// 开始编辑
newLayer1->startEditing();
// 添加要素
newLayer1->addFeatures(QgsFeatureList() << MyFeature1);
//plane
// 保存
newLayer1->commitChanges();
// 更新范围
newLayer1->updateExtents();
//newLayer1->s
//mapCanvas->refresh();
//mapCanvas->show();
return newLayer1;
}
/// fire ///
QgsVectorLayer* QtWidgetsApplicationxx::drawfire(QgsVectorLayer*newLayer, double x, double y)
{
//定义坐标转换对象trans,从src向dest转换(CRS转换)
//EPSG:4326指的是WGS84系统..EPSG是最常用的授权机构,它涵盖了全世界范围广泛的坐标系统。
QgsCoordinateReferenceSystem src(4326, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateReferenceSystem dest(3857, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateTransformContext context;
QgsCoordinateTransform trans(src, dest, context);
//转换坐标
QgsPointXY point = trans.transform(x, y);
//QgsVectorDataProvider* dateProvider = newLayer->dataProvider();//创建一个容器
QgsFeature MyFeature;
//fire
QgsSvgMarkerSymbolLayer* svgMarker = new QgsSvgMarkerSymbolLayer(firecolor);// F:/OSGeo4W64/QtQgisVs
QgsSymbolLayerList symList;
symList.append(svgMarker);
QgsMarkerSymbol* markSym = new QgsMarkerSymbol(symList);
QgsSingleSymbolRenderer* symRenderer = new QgsSingleSymbolRenderer(markSym);
newLayer->setRenderer(symRenderer);
svgMarker->setSize(3);
//fire
MyFeature.setGeometry(QgsGeometry::fromPointXY(point));
MyFeature.setAttributes(QgsAttributes() << QVariant(1) << QVariant("test"));
// 开始编辑
newLayer->startEditing();
// 添加要素
newLayer->addFeatures(QgsFeatureList() << MyFeature);
// 保存
newLayer->commitChanges();
// 更新范围
newLayer->updateExtents();
//mapCanvas->refresh();
return newLayer;
}
//*************** 创建线 ******************************************************************************************
QgsVectorLayer* QtWidgetsApplicationxx::drawfireline(QgsVectorLayer*newLayer, QString strPath, double x, double y)
{
//定义坐标转换对象trans,从src向dest转换(CRS转换)
//EPSG:4326指的是WGS84系统..EPSG是最常用的授权机构,它涵盖了全世界范围广泛的坐标系统。
QgsCoordinateReferenceSystem src(4326, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateReferenceSystem dest(3857, QgsCoordinateReferenceSystem::EpsgCrsId);
QgsCoordinateTransformContext context;
QgsCoordinateTransform trans(src, dest, context);
//转换坐标
QgsPointXY point = trans.transform(x, y);
//QgsVectorDataProvider* dateProvider = newLayer->dataProvider();//创建一个容器
QgsFeature MyFeature;
//fire
QgsSvgMarkerSymbolLayer* svgMarker = new QgsSvgMarkerSymbolLayer(strPath);// F:/OSGeo4W64/QtQgisVs
QgsSymbolLayerList symList;
symList.append(svgMarker);
QgsMarkerSymbol* markSym = new QgsMarkerSymbol(symList);
QgsSingleSymbolRenderer* symRenderer = new QgsSingleSymbolRenderer(markSym);
newLayer->setRenderer(symRenderer);
svgMarker->setSize(3);
//fire
MyFeature.setGeometry(QgsGeometry::fromPointXY(point));
MyFeature.setAttributes(QgsAttributes() << QVariant(1) << QVariant("test"));
// 开始编辑
newLayer->startEditing();
// 添加要素
newLayer->addFeatures(QgsFeatureList() << MyFeature);
// 保存
newLayer->commitChanges();
// 更新范围
newLayer->updateExtents();
return newLayer;
}
void QtWidgetsApplicationxx::AddLine()
{
// 创建一个线图层。
QgsVectorLayer *Line_Layer = new QgsVectorLayer("LineString?crs=epsg:4326", "MyLine", "memory");
QgsVectorDataProvider *dataProder = Line_Layer->dataProvider();
//在指定坐标添加点
QgsFeature feature;
QgsGeometry geometry = QgsGeometry::fromPointXY(QgsPointXY(50, 50));
//通过点坐标进行画线
QgsPolyline line;
line << QgsPoint(100.4443, 32.2123) << QgsPoint(130.5557, 50.4445);
geometry = QgsGeometry::fromPolyline(line);
feature.setGeometry(geometry);
dataProder->addFeatures(QgsFeatureList() << feature);
Line_Layer->updateExtents();
//将线图层加载到链表中
layerlist.append(Line_Layer);
QgsProject::instance()->addMapLayers(layerlist);
}
WinsockMatTransmission.h
#pragma once
#ifndef __WINSOCKMATTRANSMISSIONCLIENT_H__
#define __WINSOCKMATTRANSMISSIONCLIENT_H__
#include <stdio.h>
#include <Winsock2.h>
#include<iostream>
#pragma comment(lib,"WS2_32.lib")
//待传输图像默认大小为 640*480,可修改
#define IMG_WIDTH 640 // 需传输图像的宽
#define IMG_HEIGHT 480 // 需传输图像的高
//默认格式为CV_8UC3
#define BUFFER_SIZE 32
struct sentbuf
{
char sbuf[BUFFER_SIZE];
int sflag;
};
struct recvbuf
{
char rbuf[BUFFER_SIZE];
int rflag;
};
class WinsockMatTransmission
{
public:
WinsockMatTransmission(void);
~WinsockMatTransmission(void);
private:
SOCKET sockClient;
SOCKET sockConn;
struct sentbuf sdata;
struct recvbuf rdata;
public:
int buff[100];
// 打开socket连接
// params : IP 服务器的ip地址
// PORT 传输端口
// return : -1 连接失败
// 1 连接成功
int socketConnect_client(const char* IP, int PORT);
int socketConnect_server(int PORT);
// 传输图像
// params : image 待传输图像
// return : -1 传输失败
// 1 传输成功
int transmit(int& a);
int receive(char*image[32]);
int receive2(char &image);//传递数
int receive3(char image[32]);//传递数组
// 断开socket连接
void socketDisconnect(void);
};
#endif
WinsockMatTransmission.cpp
#include "WinsockMatTransmission.h"
WinsockMatTransmission::WinsockMatTransmission(void)
{
}
WinsockMatTransmission::~WinsockMatTransmission(void)
{
}
//建立连接
int WinsockMatTransmission::socketConnect_client(const char* IP, int PORT)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return -1;
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1) {
WSACleanup();
return -1;
}
err = (sockClient = socket(AF_INET, SOCK_STREAM, 0));
if (err < 0) {
printf("create socket error: %s(errno: %d)\n\n", strerror(errno), errno);
return -1;
}
else
{
printf("create socket successful!\nnow connect ...\n\n");
}
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr(IP);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(PORT);
err = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (err < 0)
{
printf("connect error: %s(errno: %d)\n\n", strerror(errno), errno);
return -1;
}
else
{
printf("connect successful!\n\n");
return 1;
}
}
int WinsockMatTransmission::socketConnect_server(int PORT)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
return -1;
}
if (LOBYTE(wsaData.wVersion) != 1 ||
HIBYTE(wsaData.wVersion) != 1)
{
WSACleanup();
return -1;
}
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(PORT);
bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(sockSrv, 5);
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
//套接字处于监听状态时,可以通过 accept() 函数来接收客户端请求
sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
int nRecvBuf = 1024 * 1024 * 10;
setsockopt(sockConn, SOL_SOCKET, SO_RCVBUF, (const char*)&nRecvBuf, sizeof(int));
}
void WinsockMatTransmission::socketDisconnect(void)
{
closesocket(sockClient);
closesocket(sockConn);
WSACleanup();
}
//发送数据
int WinsockMatTransmission::transmit(int& image)
{
sdata.sbuf[8] =12 ;
sdata.sbuf[16] = 127;
sdata.sbuf[31] = 28;
if (send(sockClient, (char *)(&sdata), sizeof(sdata), 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
}
//接收数据
int WinsockMatTransmission::receive(char *image[32])
{
int needRecv = sizeof(recvbuf);
int strLen = recv(sockConn, (char*)(&rdata), needRecv, 0);
*image = rdata.rbuf;
return 0;
}
int WinsockMatTransmission::receive2(char &image)
{
int needRecv = sizeof(recvbuf);
int strLen = recv(sockConn, (char*)(&rdata), needRecv, 0);
//send(sockConn, (char*)(&rdata), strLen, 0); //将数据原样返回
image = rdata.rbuf[1];
//std::cout << "a= " << a << std::endl;
return 0;
}
int WinsockMatTransmission::receive3(char image[32])
{
int needRecv = sizeof(recvbuf);
int strLen = recv(sockConn, (char*)(&rdata), needRecv, 0);
for (int i = 0; i <32; i++) {
image[i] = rdata.rbuf[i];
}
return 0;
}
界面搭建
主要参考原来那篇博客:https://blog.csdn.net/xx970829/article/details/111997428
第一页
第二页
1.下拉框comboBox_2选项加入如下选项
2.菜单栏中建活动用于切换stackedWidget页(当然,也设有快捷键shift)
第三页
第四页->说明
快捷键:
R 复位
L 雷达
G 惯导
I 红外相机
M 地图
T 检测火
F1 重启
Esc 关机
Enter 发送
Delete 清空特征
Shift 切换页码
S 默认串口已打开
Control 默认工控机已开机
控制参数表:
101: 打开激光雷达
102: 打开惯导
103: 打开红外相机
104: 开始记录ROS包
105: ROS包记录结束
106: 播放ROS包
107: 开始检测火
201: 关闭激光雷达
202: 关闭惯导
203: 关闭红外相机
222: 复位
223: 工控机重启
224: 工控机开机验证
225: 工控机关机
说明:
1.只有收到返回的数据才能启动/关闭成
功;
2.除了“记录ROS包、结束记录”外,其他
按钮启动和关闭均由一个键控制;
3.开始检测火点后雷达、惯导、红外相机将
无法点击;
4.复位不会关闭地图或清空特征;
5.223:重启工控机,此时其他发送指令的
按钮将失效,其快捷键为F1,224:验证工
控机是否处于开机状态,225:工控机关
机,其快捷键为Esc。
[注意:]每次打开串口都会检测工控机是否开
机,只有接收到224时才能进行其他操
作,所以脱离工控机调试时,每次点击打
开串口按钮都需要另一设备发送数据
224,或者点击键盘ctrl来默认工控机已
打开。
【其他细节请参考我以前的那篇博客:https://blog.csdn.net/xx970829/article/details/111997428】
运行结果
项目下载
由于环境配在项目里面,所以里面头文件、dll文件这些很多,大约1G
链接:https://pan.baidu.com/s/1-9WZJHFFq6vhG-c5qAOkWA
提取码:elx4