qt之上位机与激光雷达通信

qt系列文章之上位机通信

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

`提示:以ld07固态线激光雷达为例

例如:随着人工智能的不断发展,上位机这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


`提示:以激光雷达为例,展示qt上位机通信过程。

一、先做好ui界面

如图:
在这里插入图片描述

二、c++具体代码

首先,创建项目名为samp2_8

1.pro文件

主要操作为加入:serialport

#-------------------------------------------------
#
# Project created by QtCreator 2022-07-19T08:41:21
#
#-------------------------------------------------

QT       += core gui serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = samp2_8
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        widget.cpp

HEADERS += \
        widget.h

FORMS += \
        widget.ui

2.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_pushButton_open_clicked(); //打开串口
    void ReadData();  //读取串口数据
    void on_pushButton_send_clicked(); //发送串口数据
//    void _Init();
    void _Init1();
signals:
    void readyRead();

private:
    Ui::Widget *ui;
    QSerialPort *m_port;  //定义一个serialport类型指针,注意先引入头文件QSerialPort
};

#endif // WIDGET_H

3.main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

4.widget.cpp文件

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{

    ui->setupUi(this);
    _Init1();
   connect(m_port,SIGNAL(readyRead()),this,SLOT(ReadData()));
//    _Init();
//    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
//    {
//        QSerialPort m_port;  //初始化一个串口
//        m_port.setPort(info); //
//        if(m_port.open(QIODevice::ReadWrite))  //串口能够打开
//        {
//            ui->comboBox_port->addItem(m_port.portName());  //调用“串口”组件,为“串口”命名为此时串口
//            m_port.close();  //关闭串口
//        }
//    }
    //2.设置波特率下拉菜单默认显示第0项

}

Widget::~Widget()
{
    delete ui;
}


//void Widget::_Init()
//{
//    //初始化

//    //1.查找可用的串口
//    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
//        QSerialPort port;  //初始化一个串口
//        port.setPort(info); //
//        if(port.open((QIODevice::ReadWrite)))  //串口能够打开
//        {
//            ui->comboBox_port->addItem(port.portName());  //调用“串口”组件,为“串口”命名为此时串口
//            port.close();  //关闭串口
//        }
//    }
//    //2.设置波特率下拉菜单默认显示第0项
//    ui->comboBox_baud->setCurrentIndex(0);
//    //3.连接信号槽
//    m_port = new QSerialPort;
//    connect(m_port,SIGNAL(readyRead()),this,SLOT(ReadData()));
//}

void Widget::_Init1()
{
    m_port = new QSerialPort();
    m_port->setPortName("COM3");
//    m_port->setBaudRate(QSerialPort::Baud9600);
    m_port->setBaudRate(921600);
    m_port->setDataBits(QSerialPort::Data8);
    m_port->setParity(QSerialPort::NoParity);
    m_port->setStopBits(QSerialPort::OneStop);
    m_port->setFlowControl(QSerialPort::NoFlowControl);

}

void Widget::on_pushButton_open_clicked()
{
    //打开串口
//    if(ui->pushButton_open->text()==tr("打开串口")) //如果打开串口按钮的内容是“打开串口”
//    {

//        //1.设置串口名
//        m_port->setPortName(ui->comboBox_port->currentText());
//        //2.打开串口,用父类
//        m_port->open(QIODevice::ReadWrite);
//        //3.设置波特率
//        m_port->setBaudRate(ui->comboBox_baud->currentText().toInt());
//        //4.设置数据位,8,7,6,5
//        switch (ui->comboBox_bit->currentIndex())
//        {
//        case 8:
//            m_port->setDataBits(QSerialPort::Data8);
//            break;
//        case 7:
//            m_port->setDataBits(QSerialPort::Data7);
//            break;
//        case 6:
//            m_port->setDataBits(QSerialPort::Data6);
//            break;
//        case 5:
//            m_port->setDataBits(QSerialPort::Data5);
//            break;
//        default:
//            break;
//        }
//        //5.设置校验位,0,无校验位
//        switch (ui->comboBox_parity->currentIndex()) {
//        case 0:
//            m_port->setParity(QSerialPort::NoParity);
//            break;
//        default:
//            break;
//        }
//        //6.设置停止位
//        switch (ui->comboBox_bit->currentIndex()) {
//        case 1:
//            m_port->setStopBits(QSerialPort::OneStop);
//            break;
//        case 2:
//            m_port->setStopBits(QSerialPort::TwoStop);
//            break;
//        default:
//            break;
//        }
//        //7.设置控制流
//        m_port->setFlowControl(QSerialPort::NoFlowControl);

//        //8.关闭菜单,关闭串口,波特率,数据位,校验位,停止位,
//        ui->comboBox_port->setEnabled(false);
//        ui->comboBox_baud->setEnabled(false);
//        ui->comboBox_bit->setEnabled(false);
//        ui->comboBox_parity->setEnabled(false);
//        ui->comboBox_stop->setEnabled(false);
//        ui->pushButton_open->setText(tr("关闭串口"));

        //连接信号槽
//        connect(m_port,SIGNAL(readyRead()),this,SLOT(ReadData()));
        if(m_port->open(QIODevice::ReadWrite))
        {
            //

            qDebug()<<"open succ";

        }
        else
        {
            qDebug()<<"false";
        }
        //串口成功打开后

//    }
//    else  //如果打开串口的按钮内容不是“打开串口”
//    {

//        //9.关闭串口
//        m_port->clear();
//        m_port->close();
//        m_port->deleteLater();

//        //10.恢复菜单
//        ui->comboBox_port->setEnabled(true);
//        ui->comboBox_baud->setEnabled(true);
//        ui->comboBox_bit->setEnabled(true);
//        ui->comboBox_parity->setEnabled(true);
//        ui->comboBox_stop->setEnabled(true);
//        ui->pushButton_open->setText(tr("打开串口"));
//    }
}


void Widget::ReadData()
{
    //读取串口数据

    //1.初始化一个接收数据对象
    QByteArray arr;
    if(m_port->isOpen())
    {
        //2.调用指针读取数据,并赋值给数据接收对象
        arr = m_port->readAll().toHex(); //转换为16进制数据
        ui->textEdit_get->insertPlainText(arr);
        qDebug()<<"read succ";
    }
    else
      {
        qDebug()<<"read failed";
    }

//    //读取数组信息
//    if(!arr.isEmpty())
//    {
//        //初始化一个字符串,调用textEdit组件
//        QString str = ui->textEdit_get->toPlainText();
//        str += tr(arr);
//        ui->textEdit_get->clear();
//        ui->textEdit_get->append(str);
//    }
//    arr.clear();




}


void Widget::on_pushButton_send_clicked()
{
    //发送数据
    unsigned char buf[9]={0xA5,0xA5,0xA5,0xA5,0x00,0x63,0x00,0x00,0x63};
//类型强制转换
    m_port->write(reinterpret_cast<const char*>(buf),9);
//     m_port->write(ui->lineEdit_send->text().t);

}


总结

提示:这里对文章进行总结:

例如:以上就是今天要讲的内容,本文仅仅简单介绍了上位机的使用,而上位机提供了大量能使我们快速便捷地处理激光雷达数据的函数和方法。

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CN-JackZhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值