QT串口通讯上位机框架,带qcustomplot绘图(DLL集成)有需要的可以自用

一个比较简单的qt串口上位机框架,提供给大家做二次开发

编译器为Qt_6_5_3_MinGW_64

优点1:带一个绘制好的仪表盘,可进行数据显示
优点2:自带qcutomplot,能够进行绘图,并且对应qcutomplot已经进行dll集成,能够极大地提升编译时间
优点3:基础的串口通讯配置都有,有对应的发送接收显示
优点4:带一个简单的信号发生器,能够测试qcutomplot绘图

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
下载地址
https://github.com/watch361/QT/

部分代码
串口初始化

void MainWindow::SerialPortInit()
{
    QStringList portNameList;

    serial = new QSerialPort; //申请内存,并设置父对象
    // 参数配置
    ui->BaudBox->addItem("9600");
    ui->BaudBox->addItem("57600");
    ui->BaudBox->addItem("115200");
    ui->BaudBox->addItem("230400");
    ui->BaudBox->addItem("460800");

    // 获取用户选择的波特率字符串
    QString selectedBaudRate = ui->BaudBox->currentText();

    // 检查用户选择的波特率,并根据不同的波特率设置串口的波特率
    // 检查用户选择的波特率,并根据不同的波特率设置串口的波特率
    if (selectedBaudRate == "9600")
    {
        serial->setBaudRate(QSerialPort::Baud9600);
    }
    if (selectedBaudRate == "57600")
    {
        serial->setBaudRate(QSerialPort::Baud57600);
    }
    if (selectedBaudRate == "115200")
    {
        serial->setBaudRate(QSerialPort::Baud115200);
    }
    // if (selectedBaudRate == "230400")
    // {
    //     serial->setBaudRate(QSerialPort::Baud230400);
    // }
    // if (selectedBaudRate == "460800")
    // {
    //     serial->setBaudRate(QSerialPort::Baud460800);
    // }
    // 校验,校验默认选择无
    ui->ParityBox->addItem("无");
    serial->setParity(QSerialPort::NoParity);

    // 数据位,数据位默认选择8位
    ui->BitBox->addItem("8");
    serial->setDataBits(QSerialPort::Data8);

    // 停止位,停止位默认选择1位
    ui->StopBox->addItem("1");
    serial->setStopBits(QSerialPort::OneStop);


    // 刷新串口
    //  RefreshSerialPort(0);
    ui->PortBox->clear();                                        //关闭串口号
    ui->PortBox->addItem("刷新");                                //添加刷新
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) //添加新串口
    {
        portNameList.append(info.portName());
    }

    ui->PortBox->addItems(portNameList);
    ui->PortBox->setCurrentIndex(1);                             // 当前串口号为COM1
    serial->setPortName(ui->PortBox->currentText());             //设置串口号

      connect(serial,&QSerialPort::readyRead,this,&MainWindow::UartReceiveMessage);


}

仪表盘初始化

void MainWindow::Dashboard_Init()
{
    //仪表盘显示
   // connect(this->ui->horizontalSlider,&QSlider::valueChanged,this,&MainWindow::horizontalSlider_3);
    this->ui->mydial->settitle("速度");
    this->ui->mydial->setunits("r/min");
    this->ui->mydial->setmaxValue(4000);
    this->ui->mydial->setminValue(-4000);
    //this->ui->dialPlate->setValue(value);
    this->ui->mydial->update();//手动调用控件的OnPaint方法
   // connect(this->ui->horizontalSlider_2,&QSlider::valueChanged,this,&MainWindow::horizontalSlider_4);
    this->ui->mydial1->settitle("角度");
    this->ui->mydial1->setunits("°");
    this->ui->mydial1->setmaxValue(360);
    this->ui->mydial1->setminValue(0);
    //this->ui->dialPlate->setValue(value);
    this->ui->mydial1->update();//手动调用控件的OnPaint方法



}

绘图初始化

void MainWindow::QCustomPlot_Init()
{
    //添加一条曲线
    //向绘图区域QCustomPlot(从widget提升来的)添加一条曲线
    ui->myCustomPlot->addGraph();
    //设置画笔
    ui->myCustomPlot->graph(0)->setPen(QPen(Qt::blue));
    //设置画刷,曲线和X轴围成面积的颜色
    ui->myCustomPlot->graph(0)->setBrush(QBrush(QColor(255,255,0)));
    //设置右上角图形标注名称
    ui->myCustomPlot->graph(0)->setName("曲线");
    //设置坐标轴标签名称
    ui->myCustomPlot->xAxis->setLabel("x");
    ui->myCustomPlot->yAxis->setLabel("y");

    //设置坐标轴显示范围,否则我们只能看到默认的范围
    ui->myCustomPlot->xAxis->setRange(0,60);
    ui->myCustomPlot->yAxis->setRange(0,1000);


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

超级馒头神

看看谁给我第一个打赏。太感谢您

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

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

打赏作者

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

抵扣说明:

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

余额充值