做项目经常会调试一下有变化规律的器件,如果只是打印出来显得观察变化规律不明显,所以自己用QT写了个串口数据变化曲线。
效果图:(点击查看,将就)
使用方法:
串口数据格式 (ASCII): 序号+空格+数据+空格。。。(后面数据不讲究)
如数据:642 22.4600 \r\n
序号为1的时候会重画另一条颜色不同的曲线。
缺点:不支持界面缩放(一开始没布局,差不多完成的时候才发现,屏幕分辩率1920*1080)
源码下载地址:https://download.youkuaiyun.com/download/sudaroot/11149854
源码:(因为程序用了一个第三方的qcustomplot库,下面我就不复制上来了,太大了,网上下载添加编译即可)
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle("Serial Port Curve Debugging");
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
serial(new QSerialPort)
{
ui->setupUi(this);
curveZoom_flag = 1;
fillPortsParameters();
fillPortsInfo();
SerialSetinfo();
connect(ui->refreshButton, &QPushButton::clicked, this, &MainWindow::fillPortsInfo); //按键刷新串口
connect(ui->serialCtrlButton, &QPushButton::clicked, this, &MainWindow::switchSerialPort); //串口开关
connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);
connect(serial, &QSerialPort::errorOccurred, this, &MainWindow::handleError);
connect(ui->clearDataButton, &QPushButton::clicked, this, &MainWindow::clearData);
connect(ui->sendDataButton, &QPushButton::clicked, this, &MainWindow::writeData);
connect(ui->curveZoomButton, &QPushButton::clicked, this, &MainWindow::curveZoom);
connect(ui->curveClearButton, &QPushButton::clicked, this, &MainWindow::curveClear);
connect(ui->allClearButton, &QPushButton::clicked, this, &MainWindow::allClear);
ui->curveShowWidget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom|
QCP :: iSelectPlottables|QCP::iSelectItems|
QCP :: iSelectAxes| QCP::iSelectLegend);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::fillPortsParameters()
{
ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
ui->baudRateBox->addItem(tr("Custom"));
ui->dataBitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5);
ui->dataBitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6);
ui->dataBitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7);
ui->dataBitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8);
ui->dataBitsBox->setCurrentIndex(3);
ui->parityBox->addItem(tr("None"), QSerialPort::NoParity);
ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity);
ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity);
ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity);
ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity);
ui->stopBitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop);
ui->stopBitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
ui->flowControlBox->addItem(tr("None"), QSerialPort::NoFlowControl);
ui->flowControlBox->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
ui->flowControlBox->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl);
}
void MainWindow::fillPortsInfo()
{
QString description, manufacturer, serialNumber;
ui->serialPortInfoListBox->clear();
const auto infos = QSerialPortInfo::availablePorts();
for(const QSerialPortInfo &info : infos)
{
QStringList list;
description = info.description();
manufacturer = info.manufacturer();
serialNumber = info.serialNumber();
list << info.portName()
<< (!description.isEmpty() ? description : blankString)
<< (!manufacturer.isEmpty() ? manufacturer : blankString)
<< (!serialNumber.isEmpty() ? serialNumber : blankString)
<< info.systemLocation()
<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
<<(info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);
ui->serialPortInfoListBox->addItem(list.first(), list.size());
}
ui->serialPortInfoListBox->addItem(tr("Custom"));
}
void MainWindow::SerialSetinfo()
{
//串口名
currentSerialSetings.name = ui->serialPortInfoListBox->currentText();
//波特率
if(ui->baudRateBox->currentIndex() == 4){
currentSerialSetings.baudRate = ui->baudRateBox->currentText().toInt();
}
else{
currentSerialSetings.baudRate = static_cast<QSerialPort::BaudRate>(ui->baudRateBox->itemData(ui->baudRateBox->currentIndex()).toInt());
}
currentSerialSetings.stringBaudRate = QString::number(currentSerialSetings.baudRate);
//数据位
currentSerialSetings.dataBits = static_cast<QSerialPort::DataBits>(ui->dataBitsBox->itemData(ui->dataBitsBox->currentIndex()).toInt());
currentSerialSetings.stringDataBits = ui->dataBitsBox->currentText();
//校验位
currentSerialSetings.parity = static_cast<QSerialPort::Parity>(ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt());
currentSerialSetings.stringParity = ui->parityBox->currentText();
//停止位
currentSerialSetings.stopBits = static_cast<QSerialPort::StopBits>(ui->stopBitsBox->itemData(ui->stopBitsBox->currentIndex()).toInt());
currentSerialSetings.stringStopBits = ui->stopBitsBox->currentText();
//流控
currentSerialSetings.flowControl = static_cast<QSerialPort::FlowControl>(ui->flowControlBox->itemData(ui->flowControlBox->currentIndex()).toInt());
currentSerialSetings.stringFlowControl = ui->flowControlBox->currentText();
}
void MainWindow::openSerialPort()
{
serial->setPortName(currentSerialSetings.name);
serial->setBaudRate(currentSerialSetings.baudRate);
serial->setDataBits(currentSerialSetings.dataBits);
serial->setStopBits(currentSerialSetings.stopBits);
serial->setParity(currentSerialSetings.parity);
serial->setFlowControl(currentSerialSetings.flowControl);
if(serial->open(QIODevice::ReadWrite)){
qDebug() << "OK";
ui->ref